【问题标题】:karmajs fails to launch when node-openid-client is imported in typescript specification file在 typescript 规范文件中导入 node-openid-client 时,karmajs 无法启动
【发布时间】:2020-01-31 10:14:36
【问题描述】:

我正在使用node-openid-client 通过 OpenID 提供程序执行基于 OpenIDConnect 的身份验证。

当我尝试为这个程序编写测试用例时,我现在遇到了问题。当应用程序从节点 CLI 运行时,它可以正常工作。即它获取代码,我也可以使用它来获取令牌!

我遇到的错误是

ERROR [source-reader.karma-typescript]: Error parsing code: Unexpected token (24:2)
in C:\VSCode\Projects\openid-client-test\javascript\node_modules\openid-client\lib\errors.js 
at line 24, column 2:

... );
if (response) {
     Object.defineProperty(th ...

此错误上方是 Karma 跑步者正在执行的活动 -

INFO [compiler.karma-typescript]: Compiled 1 files in 3029 ms.

DEBUG [bundler.karma-typescript]: Project has 2 import/require statements, code will be bundled

DEBUG [es6-transform.karma-typescript]: Transforming C:\VSCode\Projects\openid-client-test\javascript\node_modules\openid-client\lib\index.js

这些是 Karma 在使用 LOG_DEBUG 配置运行时生成的日志。

我没有使用 Angular 或任何其他 UI 框架

基于这些错误,我有几个问题 -

Q1 : 我想知道为什么 karma 会解析 node_modules 文件夹中的 js 文件?我在 tsconfig.json 中排除了它。

根据错误之前执行的活动,我注意到它无法捆绑 openid-client 库的所需文件。但是,如果我为这个库运行 browserify 包,它会成功。我想我在下面列出的任何文件中都做了一些错误的配置。

Q2请帮助我确定哪个配置属性有问题!,或者解决方案本身也将不胜感激!

Q3:让我担心的问题 - openid-client 与 karma 测试运行器不兼容吗? 这样的限制可能吗?我在 GitHub 存储库中没有看到任何对应的issue

以下是我一直在编辑以解决此错误的感兴趣的文件。不过,我想我现在已经碰壁了。

为了专注于问题并以黑白方式查看 openid-client 是否导致问题,我引入了一个针对 jasmine 框架的最小测试用例。规范看起来像这样 -

probe.spec.ts 文件

import { Issuer } from 'openid-client';
describe('Hello', () => {
  it('Checks', () => {
    expect('hello').toBe('hello');
  })
});

现在在进行实验时,我删除了第一行的导入。如果这样做了; karma 启动并报告测试用例成功!

package.json 文件的依赖项(包括 dev)

"dependencies": {
  "@types/node": "^12.7.5",
  "amazon-cognito-auth-js": "^1.3.2",
  "atob": "^2.1.2",
  "openid-client": "^3.7.2",
  "typescript": "^3.6.3",
  "xmlhttprequest": "^1.8.0"
},
"devDependencies": {
  "@types/jasmine": "^3.4.1",
  "jasmine-core": "^3.5.0",
  "karma": "^4.3.0",
  "karma-chrome-launcher": "^3.1.0",
  "karma-coverage-istanbul-reporter": "^2.1.0",
  "karma-jasmine": "^2.0.1",
  "karma-jasmine-html-reporter": "^1.4.2",
  "karma-spec-reporter": "0.0.32",
  "karma-typescript": "^4.1.1",
  "karma-typescript-es6-transform": "^4.1.1"
}

tsconfig.json 看起来像这样 -

{
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compilerOptions": {
  "module": "commonjs",
  "target": "es2016",
  "noImplicitAny": false,
  "strictNullChecks": true,
  "moduleResolution": "node",
  "sourceMap": true,
  "importHelpers": true,
  "outDir": "output",
  "baseUrl": ".",
  "typeRoots": [
    "node_modules/@types"
  ],
  "types": [
    "@types/jasmine",
    "@types/node"
  ],
  "lib": [
    "es2017",
    "dom",
    "es2015.generator",
    "es2015.iterable",
    "es2015.promise",
    "es2015.symbol",
    "es2015.symbol.wellknown",
    "esnext.asynciterable"
  ]}
}

karma.conf.js 看起来像这样 -

module.exports = (config) => {
  config.set({
    frameworks: ['jasmine', 'karma-typescript'],
    plugins: [
      'karma-jasmine',
      'karma-typescript',
      'karma-chrome-launcher',
      'karma-spec-reporter',
      'karma-typescript-es6-transform'
    ],
    karmaTypescriptConfig: {
      tsconfig: "./tsconfig.json",
      compilerOptions: {
        allowJs: true
      },
      bundlerOptions: {
        entrypoints: /\.spec\.(ts|tsx)$/,
        addNodeGlobals: true,
        transforms: [require("karma-typescript-es6-transform")()]
      }
    },
    files: [{ pattern: 'src/**/*.+(js|ts)' }],
    preprocessors: {
      'src/**/*.+(js|ts)': ['karma-typescript']
    },
    client: {
      clearContext: false
    },
    reporters: ['spec', 'karma-typescript'],
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: true
  })
}

【问题讨论】:

    标签: typescript karma-jasmine node-modules openid-connect


    【解决方案1】:

    看来我已经找到了(中等信心水平)对我的问题的回答。如果这是一个足够合理的答案,请让社区投票。

    TL;DR - karma-typescript 有一个导致错误的known limitation。即使没有 karma-typescript 插件; node-openid-client 也是not expected to work in browser 现在(2-October-2019T16:30UT)。所以,是的,我不能将 node-opened-client 与业力一起使用(它不能工作 without browser 甚至 as-of now (2-October-2019T16:30UT) )。可行的alternate是用mocha代替karma。

    上述结论的详细说明

    文件 error.js 第 24,2 行附近的区域看起来像这样

    
        Line 16:    Object.assign(
        Line 17:        this,
        Line 18:        {error},
        Line 19:        (error_description && {error_description}),
        Line 20:        (error_uri && {error_uri}),
        Line 21:        (state && {state}),
        Line 22:        (scope && {scope}),
        Line 23:        (session_state && {session_state}),
        Line 24:    );
    

    第 24,2 行对应于 Object.assign 调用的右大括号。如果您注意到上面的行有一个额外的逗号。我将其删除以查看错误消失。可能是库 node-opened-client 错误使用的转译器。修复后出现以下错误

    
        ERROR [source-reader.karma-typescript]: Error parsing code: Unexpected token (72:8)
        in C:\VSCode\Projects\openid-client-test\javascript\node_modules\openid-client\lib\issuer.js
        at line 72, column 8:
    
        ... keystore(reload = false) {
            assertIssuerConfigu ...
    

    文件 issuer.js 第 72 行附近的区域如下所示

    
        Line 68    /**
        Line 69    * @name keystore
        Line 70    * @api public
        Line 71    */
        Line 72    async keystore(reload=false) {
        Line 73        assertIssuerConfiguration(this, 'jkws_uri');
    
    

    第 72 行使用 async 关键字,直接的问题是(com/trans)编译过程是否支持 async 关键字?

    为了验证这个假设,我删除了 async 关键字,并注意到错误位置移到了下一个异步位置。

    我在 node-openid-client 库中找不到太多错误。但是在捆绑文件的 karma-typescript 插件中,出现了严重的问题。在他们的存储库中搜索会产生很少的提示。在其中我认识到可能导致问题的配置。

    文件 configuration.ts 第 113 行周围的区域看起来像这样 -

        ...
        Line 111    const defaultBundlerOptions: BundlerOptions = {
        Line 112        acornOptions: {
        Line 113            ecmaVersion : 7,
        Line 114            sourceType : "module"
        Line 115        },
        ...
    

    如果我在 node_modules 文件夹中的本地副本中更改此版本的 ecmaVersion 7,我的问题得到解决,但我遇到了另一个错误,但这次使用的是数组的扩展语法。

    通过集中搜索,我找到了 SO 问题 karma-typescript: import JS file with Async keyword。显然这是一个open issue,带有 karma-typescript,但仍处于打开状态(2-October-2019T16:30UT)。您可以设置 karma-typescript 的未记录属性,而不是更改 js 文件。然而,这并没有让我更接近我希望的解决方案!

    回答个别问题

    Q1 : 我想知道为什么 karma 会解析 node_modules 文件夹中的 js 文件?我在 tsconfig.json 中排除了它。

    A1:karma 正在捆绑依赖文件,以便测试脚本在 浏览器 中运行。在这个过程中,tsconfig.json 中提到的设置是无关紧要的。

    Q2:请帮助我确定哪个配置属性有问题!

    A2:对于这个 SO 问题中引用的错误,karmaTypescriptConfig.bundlerOptions.acorOptions 接受 JSON,可以在 karma.conf.js 中将其设置为

        karmaTypescriptConfig: {
            bundlerOptions: {
                acornOptions: {
                ecmaVersion: 8,
            },
            transforms: [require("karma-typescript-es6-transform")()]
        }
    

    Q3:对我来说令人担忧的问题 - openid-client 与 karma 测试运行器不兼容吗?这样的限制可能吗?我在 GitHub 存储库中没有看到任何相应的问题。

    A3:暗示是的! - openid-client 与 karma 不兼容,因为它使用浏览器,并且 node-openid-client 对于这种环境是 not supported 即使我通过评论克服了 karma-typescript 的挑战,我将无法将它与 karma 一起使用使用基于浏览器的测试运行。

    或者,我可以使用 mocha 作为测试运行器框架,而不是 karma。我检查了; node-openid-client library themselves use mocha 用于测试。 Mocha 是 nodejs 的一等公民。

    【讨论】:

      猜你喜欢
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多