我正在尝试运行 jest 来对我的 es6 模块进行单元测试。尝试运行测试用例时出现以下错误:
TypeError: authentication.test.js: Cannot read property 'value' of undefined
at node_modules/@babel/helper-module-transforms/lib/index.js:92:30
at Array.some (<anonymous>)
at rewriteModuleStatementsAndPrepareHeader (node_modules/@babel/helper-module-transforms/lib/index.js:91:44)
at PluginPass.exit (node_modules/@babel/plugin-transform-modules-commonjs/lib/index.js:148:83)
at newFn (node_modules/@babel/traverse/lib/visitors.js:175:21)
at NodePath._call (node_modules/@babel/traverse/lib/path/context.js:55:20)
at NodePath.call (node_modules/@babel/traverse/lib/path/context.js:42:17)
at NodePath.visit (node_modules/@babel/traverse/lib/path/context.js:101:8)
at TraversalContext.visitQueue (node_modules/@babel/traverse/lib/context.js:116:16)
at TraversalContext.visitSingle (node_modules/@babel/traverse/lib/context.js:85:19)
错误所在行的代码
const hasStrict = path.node.directives.some(directive => {
return directive.value.value === "use strict";
});
这里的directive.value 是未定义的。
知道如何解决这个问题吗?在运行之前是否需要设置一些环境或配置,以便directive.value.value 为“使用严格”。我在上面的行上通过硬编码return true
进行了测试,测试用例完美地工作,没有任何错误。
我的 package.json 有以下内容:
"devDependencies": {
"@babel/core": "^7.13.14",
"@babel/helper-module-transforms": "^7.13.14",
"@babel/preset-env": "^7.13.12",
"axios": "^0.21.1",
"babel-plugin-eslint-disable": "^0.1.2",
"eslint": "^6.6.0",
"gulp-babel": "^8.0.0",
"jest": "^26.6.3",
"webpack": "^5.24.2",
"webpack-cli": "^4.5.0"
},
"type": "module"