【问题标题】:Jest encountered an unexpected token nestjsJest 遇到了意外的令牌 nestjs
【发布时间】: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


【解决方案1】:

看起来graphql-moment 代码没有以 commonJS、UMD 或其他兼容格式发布,Jest 无法解析它。您最可能需要做的就是在您的测试中像这样模拟它:

jest.mock('graphql-moment', () => ({
  GraphQLDate: jest.fn(),
  ...etcOtherFunctionsFromTheLib
}));

或者告诉 Jest 在你的 Jest 配置中使用这个在 Jest 运行时转换依赖代码:

  transformIgnorePatterns: [
    '<rootDir>/node_modules/(?!graphql-moment)',
  ],

如果您要测试日期/时间是否符合您的预期,则第二种解决方案可能会更好。

您可以在此处看到代码实际上并未被转译。也许向他们提出问题:

https://github.com/jiexi/graphql-moment/blob/master/index.js

【讨论】:

  • 谢谢@Marty,在这种情况下模拟对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 2019-08-24
相关资源
最近更新 更多