【问题标题】:React + Jest: Test suite failed to runReact + Jest:测试套件无法运行
【发布时间】:2020-11-25 07:14:30
【问题描述】:

我正在修复我们预先存在的项目的一些测试,我遇到了这个错误:

 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",
}

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

【问题讨论】:

  • 你能分享你的笑话配置吗?
  • @tmhao2005 我把它添加到我的问题中
  • 你用布尔值替换了全局窗口对象,你想做什么?

标签: javascript reactjs jestjs babel-jest


【解决方案1】:

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

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

【讨论】:

  • 好的,这使错误消失了,但是它创建了更多失败的测试用例。这正常吗?
  • 我认为我之前的开发人员由于 uiFabric 错误而添加了此配置
  • 不太确定其他情况是什么,但我认为将 window 设置为 true 根本不是一个好主意 :) 我们可能不得不修复其他情况
  • 另外,你们还必须知道要针对节点/浏览器测试哪个环境,以便进行相应的模拟,而不是让窗口保持真实:)
  • 是的,我将与其他开发人员聊天,看看解决这个问题的最佳方法是什么。感谢您的帮助!
猜你喜欢
  • 2022-01-05
  • 2019-10-21
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 2015-07-31
  • 1970-01-01
相关资源
最近更新 更多