在 typescript 规范文件中导入 node-openid-client 时,karmajs 无法启动
karmajs fails to launch when node-openid-client is imported in typescript specification file
我正在使用 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 运行ner 正在执行的活动 -
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
这些是 运行 和 LOG_DEBUG
配置时 Karma 生成的日志。
我没有使用 Angular 或任何其他 UI 框架。
基于这些错误我有几个问题 -
Q1 : 请问为什么karma会解析node_modules文件夹下的js文件?我在 tsconfig.json.
中排除了它
根据错误之前执行的活动,我注意到它未能捆绑所需的 openid-client 库文件。但是,如果我 运行 browserify bundle for this library 它会成功。我想我在下面列出的任何文件中都做了一些配置错误。
Q2 : 请帮助我如何确定哪个配置 属性 有问题! 或解决方案本身也将不胜感激!
Q3 : 让我担心的问题 - openid-client 是否与业力测试不兼容 运行ner? 是这样的可能的限制?我在 GitHub 存储库中没有看到任何对应的 issue。
以下是我一直在编辑以解决此错误的相关文件。不过,我觉得我现在碰壁了。
为了专注于问题并清楚地看到问题是否是 openid-client 引起的,我引入了一个针对 jasmine 框架的最小测试用例。规范看起来像这样 -
probe.spec.ts 文件
import { Issuer } from 'openid-client';
describe('Hello', () => {
it('Checks', () => {
expect('hello').toBe('hello');
})
});
现在,在进行实验时,我删除了第一行中的导入。如果这样做了; karma 启动并报告测试用例已成功!
package.json 文件的依赖项(包括开发)
"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
})
}
看来我已经想出(中等可信度)对我的问题的回答。如果这个答案足够合理,请社区投票。
TL;DR - karma-typescript 有一个 known limitation which is causing the error. Even without the karma-typescript plugin; node-openid-client as well is not expected to work in browser as-of now (2-October-2019T16:30UT). So, yes I cannot use node-opened-client with karma (which cannot work without browser even as-of now (2-October-2019T16:30UT) ). Viable 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)pilation 过程中是否支持 async 关键字?
为了验证这个假设,我删除了 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. Apparently this is an open issue,其中的 karma-typescript 还处于开放状态 (2-October-2019T16:30UT)。您可以设置一个未记录的 属性 of karma-typescript,而不是更改 js 文件。但是,这并没有让我更接近我所希望的解决方案!
回答个别问题
Q1:请问karma为什么要解析node_modules文件夹下的js文件?我在 tsconfig.json.
中排除了它
A1: karma 正在将测试脚本的依赖文件捆绑到运行 浏览器.在此过程中,tsconfig.json 中提到的设置无关紧要。
问题 2:请帮我看看是哪个配置 属性 出了问题!
A2:对于这个 SO 问题中引用的错误,karmaTypescriptConfig.bundlerOptions.acorOptions
接受 JSON 并且可以设置它在 karma.conf.js 作为
karmaTypescriptConfig: {
bundlerOptions: {
acornOptions: {
ecmaVersion: 8,
},
transforms: [require("karma-typescript-es6-transform")()]
}
Q3 : 让我担心的问题 - openid-client 不兼容 karma test 运行ner 吗?这样的限制可能吗?我在 GitHub 存储库中没有看到任何相应的问题。
A3: 隐含的是! - openid-client 与 karma 不兼容,因为它使用浏览器,而 node-openid-client 在这种环境下是 not supported 即使我通过评论克服了 karma-typescript 的挑战,我也无法将它与karma 使用基于浏览器的测试 运行ning.
或者我可以使用 mocha 作为测试 运行ner 框架而不是 karma。我检查了; node-openid-client library themselves use mocha 用于测试。 Mocha 是第一个 class nodejs 公民。
我正在使用 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 运行ner 正在执行的活动 -
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
这些是 运行 和 LOG_DEBUG
配置时 Karma 生成的日志。
我没有使用 Angular 或任何其他 UI 框架。
基于这些错误我有几个问题 -
Q1 : 请问为什么karma会解析node_modules文件夹下的js文件?我在 tsconfig.json.
中排除了它根据错误之前执行的活动,我注意到它未能捆绑所需的 openid-client 库文件。但是,如果我 运行 browserify bundle for this library 它会成功。我想我在下面列出的任何文件中都做了一些配置错误。
Q2 : 请帮助我如何确定哪个配置 属性 有问题! 或解决方案本身也将不胜感激!
Q3 : 让我担心的问题 - openid-client 是否与业力测试不兼容 运行ner? 是这样的可能的限制?我在 GitHub 存储库中没有看到任何对应的 issue。
以下是我一直在编辑以解决此错误的相关文件。不过,我觉得我现在碰壁了。
为了专注于问题并清楚地看到问题是否是 openid-client 引起的,我引入了一个针对 jasmine 框架的最小测试用例。规范看起来像这样 -
probe.spec.ts 文件
import { Issuer } from 'openid-client';
describe('Hello', () => {
it('Checks', () => {
expect('hello').toBe('hello');
})
});
现在,在进行实验时,我删除了第一行中的导入。如果这样做了; karma 启动并报告测试用例已成功!
package.json 文件的依赖项(包括开发)
"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
})
}
看来我已经想出(中等可信度)对我的问题的回答。如果这个答案足够合理,请社区投票。
TL;DR - karma-typescript 有一个 known limitation which is causing the error. Even without the karma-typescript plugin; node-openid-client as well is not expected to work in browser as-of now (2-October-2019T16:30UT). So, yes I cannot use node-opened-client with karma (which cannot work without browser even as-of now (2-October-2019T16:30UT) ). Viable 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)pilation 过程中是否支持 async 关键字?
为了验证这个假设,我删除了 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. Apparently this is an open issue,其中的 karma-typescript 还处于开放状态 (2-October-2019T16:30UT)。您可以设置一个未记录的 属性 of karma-typescript,而不是更改 js 文件。但是,这并没有让我更接近我所希望的解决方案!
回答个别问题
Q1:请问karma为什么要解析node_modules文件夹下的js文件?我在 tsconfig.json.
中排除了它A1: karma 正在将测试脚本的依赖文件捆绑到运行 浏览器.在此过程中,tsconfig.json 中提到的设置无关紧要。
问题 2:请帮我看看是哪个配置 属性 出了问题!
A2:对于这个 SO 问题中引用的错误,karmaTypescriptConfig.bundlerOptions.acorOptions
接受 JSON 并且可以设置它在 karma.conf.js 作为
karmaTypescriptConfig: {
bundlerOptions: {
acornOptions: {
ecmaVersion: 8,
},
transforms: [require("karma-typescript-es6-transform")()]
}
Q3 : 让我担心的问题 - openid-client 不兼容 karma test 运行ner 吗?这样的限制可能吗?我在 GitHub 存储库中没有看到任何相应的问题。
A3: 隐含的是! - openid-client 与 karma 不兼容,因为它使用浏览器,而 node-openid-client 在这种环境下是 not supported 即使我通过评论克服了 karma-typescript 的挑战,我也无法将它与karma 使用基于浏览器的测试 运行ning.
或者我可以使用 mocha 作为测试 运行ner 框架而不是 karma。我检查了; node-openid-client library themselves use mocha 用于测试。 Mocha 是第一个 class nodejs 公民。