开玩笑测试失败,未定义窗口

webpack Webpack

小哥小卤蛋

2020-03-24

我正在尝试开始学习React / Redux的最新Web开发。

现在,我被困在运行测试上。出于某种原因,笑话失败了

Task :frontend:test 
yarn jest v1.0.2
$ "/Users/gunnar/git/app.oakstair.se/frontend/node_modules/.bin/jest" 
FAIL src/containers/App/App.test.js
  ● Test suite failed to run

 ReferenceError: window is not defined

  at Object.<anonymous> (config/polyfills.js:18:1)
  at next (native)
  at process._tickCallback (internal/process/next_tick.js:109:7)

我已经搜索了一段时间没有任何成功...

这是我的开始test.js

这是我的App测试代码

第3369篇《开玩笑测试失败,未定义窗口》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
达蒙神奇 2020.03.24

我遇到了同样的问题,我不认为修改全局变量是解决问题的方法。问题是因为在我的玩笑配置中,我已经testEnvironment设置为node应该设置的时间jsdom对我来说,此设置位于React Starter应用程序定义的package.json中。

Gil伽罗小宇宙 2020.03.24

在你的package.json添加window全球这样的事情

"jest": {
  "verbose": true,
  "preset": "react-native",
  "setupFiles": ["./jest/setup.js"],
  "testRegex": "(/tests/.*|\\.(test|spec))\\.(ts|tsx|js)$",
  "transformIgnorePatterns": [
    "node_modules/(?!(jest-)?react-native|lottie-react-native)"
  ],
  "globals": {
    "window": {}
  }
}

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android