【发布时间】:2021-08-10 11:21:06
【问题描述】:
我在使用 jest 在 nest.js 中运行我的 e2e 测试时遇到问题。我不断收到错误
Jest 遇到了意外的令牌
每当我尝试运行我的 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 中的这个脚本运行我的 e2e 测试
"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)
【问题讨论】:
-
你用什么来运行你的测试?
test:e2e在 package.json 中? -
使用这个: "test:e2e": "jest --config ./test/jest-e2e.json" 这是该文件的内容:``` { "moduleFileExtensions": [" js”、“json”、“ts”]、“rootDir”:“../”、“testEnvironment”:“node”、“testRegex”:“.e2e.spec.ts$”、“transform”:{“ ^.+\\.(t|j)s$": "ts-jest" } } ```
-
你能在
./test/jest-e2e.json中显示配置吗? -
{ "moduleFileExtensions": ["js", "json", "ts"], "rootDir": "../", "testEnvironment": "node", "testRegex": " .e2e.spec.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" } }
-
我已经用你要求的内容更新了问题@JayMcDoniel
标签: typescript jestjs nestjs ts-jest