React + Jest:测试套件无法运行

时间:2020-08-05 06:30:40

标签: javascript reactjs jestjs babel-jest

我正在对我们现有的项目进行一些测试,但遇到了这个错误:

 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/$1'
    },
    "moduleFileExtensions": [
        "ts",
        "tsx",
        "js",
        "jsx",
        "json",
        "node"
    ],
    "globals": {
        "window": true
    },
    // Setup Enzyme
    "snapshotSerializers": ["enzyme-to-json/serializer"],
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupEnzyme.ts",
}

我试图在线搜索一些修补程序,但无法提供任何信息

1 个答案:

答案 0 :(得分:1)

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

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