Jest 28:创建JestExpect-类型错误:无法读取未定义的属性(阅读“extend”)

q9rjltbz  于 2022-12-16  发布在  Jest
关注(0)|答案(2)|浏览(365)

我从Jest 27迁移到了28。如果我现在尝试开始测试,我只会得到这个错误:

● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'extend')

      at createJestExpect (node_modules/@jest/expect/build/index.js:35:19)
      at Object.<anonymous> (node_modules/@jest/expect/build/index.js:47:20)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:317:13)
      at runJest (node_modules/@jest/core/build/runJest.js:407:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:338:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:190:3)

Test Suites: 1 failed, 1 total

我甚至不能调试我的测试,因为内部测试设置已经失败了。任何线索,哪里出错了?

w8f9ii69

w8f9ii691#

这是moduleDirectories选项包含"."时出现的错误(请参见here)。
要解决此问题,请将"."更改为"<rootDir>"

moduleDirectories: ["node_modules", "<rootDir>"]

或者,如果使用jest.config.js文件,则可以将"."更改为__dirname

module.exports = {
  moduleDirectories: ["node_modules", __dirname]
};
5jvtdoz2

5jvtdoz22#

我有一个类似的错误使用Angular 与jest。更新jest和Angular 依赖关系到最新版本后,我修复了这个错误在一个稍微不同的方式比Lupina,但使用rootDir而不是__dirname。这是我目前的jest.config.js

jest.config.js
module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
  globalSetup: 'jest-preset-angular/global-setup',
  moduleDirectories: ['node_modules', '<rootDir>'],
  transformIgnorePatterns: ['node_modules/(?!@angular|rxjs)'],
  collectCoverage: true,
  coverageDirectory: '<rootDir>/coverage/',
};

相关问题