Jest 遇到了一个意外的令牌 nestjs
Jest encountered an unexpected token nestjs
我在使用玩笑 运行 在 nest.js 中进行端到端测试时遇到问题。我一直收到错误
Jest encountered an unexpected token
任何时候我尝试 运行 我的 e2e 测试。所有其他测试套件 运行 成功但未成功。我在网上一一梳理,无果。
这是我在 package.json
中的笑话配置
"jest": {
"preset": "ts-jest",
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": "(?<!.e2e).spec.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
下面是我的tsconfig.json文件
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2019",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"esModuleInterop": true,
"lib": ["es2019"]
}
}
我用的是 ts-jest 而不是 babel
我 运行 我在 package.json
中用这个脚本进行了端到端测试
"test:e2e": "jest --config ./test/jest-e2e.json",
以下是我的 jest-e2e.json 文件
中的配置
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": "../",
"testEnvironment": "node",
"testRegex": ".e2e.spec.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
}
}
最后,下面是控制台中的实际错误:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/Users/michaelowolabi/Desktop/YoungM/sz/sz-graphql-api/node_modules/graphql-moment/lib/factory.coffee:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){{GraphQLScalarType} = require 'graphql'
^
SyntaxError: Unexpected token '='
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (node_modules/graphql-moment/index.js:2:17)
看起来 graphql-moment
代码未使用 commonJS、UMD 或其他一些兼容格式发布,Jest 无法解析它。您最有可能需要做的是像这样在您的测试中模拟它:
jest.mock('graphql-moment', () => ({
GraphQLDate: jest.fn(),
...etcOtherFunctionsFromTheLib
}));
或者告诉 Jest 在 Jest 运行时用你的 Jest 配置转译依赖代码:
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!graphql-moment)',
],
如果您要测试 date/time 是否符合您的预期,则第二种解决方案可能更好。
你可以在这里看到代码实际上并没有被转译。也许向他们提出问题:
https://github.com/jiexi/graphql-moment/blob/master/index.js
我在使用玩笑 运行 在 nest.js 中进行端到端测试时遇到问题。我一直收到错误
Jest encountered an unexpected token
任何时候我尝试 运行 我的 e2e 测试。所有其他测试套件 运行 成功但未成功。我在网上一一梳理,无果。
这是我在 package.json
中的笑话配置"jest": {
"preset": "ts-jest",
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": "(?<!.e2e).spec.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
下面是我的tsconfig.json文件
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2019",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"esModuleInterop": true,
"lib": ["es2019"]
}
}
我用的是 ts-jest 而不是 babel
我 运行 我在 package.json
中用这个脚本进行了端到端测试"test:e2e": "jest --config ./test/jest-e2e.json",
以下是我的 jest-e2e.json 文件
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": "../",
"testEnvironment": "node",
"testRegex": ".e2e.spec.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
}
}
最后,下面是控制台中的实际错误:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/Users/michaelowolabi/Desktop/YoungM/sz/sz-graphql-api/node_modules/graphql-moment/lib/factory.coffee:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){{GraphQLScalarType} = require 'graphql'
^
SyntaxError: Unexpected token '='
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (node_modules/graphql-moment/index.js:2:17)
看起来 graphql-moment
代码未使用 commonJS、UMD 或其他一些兼容格式发布,Jest 无法解析它。您最有可能需要做的是像这样在您的测试中模拟它:
jest.mock('graphql-moment', () => ({
GraphQLDate: jest.fn(),
...etcOtherFunctionsFromTheLib
}));
或者告诉 Jest 在 Jest 运行时用你的 Jest 配置转译依赖代码:
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!graphql-moment)',
],
如果您要测试 date/time 是否符合您的预期,则第二种解决方案可能更好。
你可以在这里看到代码实际上并没有被转译。也许向他们提出问题:
https://github.com/jiexi/graphql-moment/blob/master/index.js