React + Jest:测试套件未能 运行

React + Jest: Test suite failed to run

我正在修复我们现有项目的一些测试,我 运行 遇到了这个错误:

 FAIL  src/components/changelog/__test__/ChangeLogOverView.test.tsx
  ● Test suite failed to run

    TypeError: Cannot create property '__packages__' on boolean 'true'

      at Object.setVersion (node_modules/@uifabric/set-version/src/setVersion.ts:7:51)
      at Object.<anonymous> (node_modules/@uifabric/set-version/src/index.ts:4:1)
      at Object.<anonymous> (node_modules/office-ui-fabric-react/src/version.ts:3:3)

这是我的 jest.config.js:

module.exports = {
    "name": "",
    // Setup Jest
    "roots": [
        "<rootDir>/src"
    ],
    "testEnvironment": "node",
    "transformIgnorePatterns": ["/node_modules/"],
    "transform": {
        "^.+\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\.|/)(test|spec))\.tsx?$",
    "moduleNameMapper": {
        'office-ui-fabric-react/lib/(.*)$': 'office-ui-fabric-react/lib-commonjs/'
    },
    "moduleFileExtensions": [
        "ts",
        "tsx",
        "js",
        "jsx",
        "json",
        "node"
    ],
    "globals": {
        "window": true
    },
    // Setup Enzyme
    "snapshotSerializers": ["enzyme-to-json/serializer"],
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupEnzyme.ts",
}

我试图在网上搜索一些修复程序,但找不到任何东西

我想问题是您的 globals.window 配置设置 true 因为代码有时会尝试修改您的 window 对象。尝试将其设置为文字对象,如下所示:

"globals": {
  "window": {}
},