Jest - 无法从'setup.js'找到模块'setupDevtools'

时间:2017-07-14 13:27:13

标签: react-native jestjs

我正在学习Jest,我正在尝试将其添加到我的组件=> github.com/bolket/react-native-scrollview-smart。

当我开始测试时,我有这个错误:

$ jest 
 FAIL  lib/ScrollViewSmart.test.js
  ● Test suite failed to run

    Cannot find module 'setupDevtools' from 'setup.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
      at Object.<anonymous> (node_modules/react-native/jest/setup.js:30:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.492s

经过多次尝试后,我已将测试移到__DEV__文件夹中,错误已解决。

但是,如果我再次运行测试,我又错了....

你能告诉我什么是错的吗?

4 个答案:

答案 0 :(得分:1)

我认为有多种情况可能导致相同的错误。就我而言,我将node_modules添加到了ignore_dirs文件的.watchmanconfig中,我将其删除并且错误消失了。尝试运行jest --no-watchman来查看看守人是否是问题的根源。

答案 1 :(得分:0)

需要几个错误修正,我设法使测试运行: *消除了未知的“导出”令牌:“当导入未编译的库时,意外的令牌导出”

"transformIgnorePatterns": [
    "node_modules/?!(react-router)"
],
  • 已删除jsdom-不再模拟浏览器环境以进行本机响应。 “ TypeError:无法通过玩笑设置未定义的属性'_eventListeners'”
  • 已更新:testPathIgnorePatterns会忽略除“ src”以外的所有文件夹,因此我们可以删除根目录:[“ src”],其原因是:“无法从'setup.js'中找到模块'setupDevtools'”

答案 2 :(得分:-1)

几分钟前我遇到了同样的错误。看起来它可能在package.json中的jest配置中。见https://github.com/facebook/jest/issues/1840#issuecomment-251037030

就我而言,它有效。

答案 3 :(得分:-1)

经过数小时的搜索,我在项目中使用以下方法解决了这个问题:

这个笑话配置:

[
  "preset": "react-native",
  "rootDir": "..",
  "roots": [
    "<rootDir>"
  ],
  "setupFiles": [
    "<rootDir>/configuration/jest.setup.js"
  ],
  "transform": {
    "^.+\\.(js)$": "babel-jest",
    ".(ts|tsx)": "ts-jest"
  },
  "testRegex": "<rootDir>/__tests__/.*|\\.(test|spec)\\.(ts|tsx)$",
  "testPathIgnorePatterns": [
    "src/__tests__/.*/behavior",
    "src/__tests__/stubs/"
  ],
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js"
  ],
  "snapshotSerializers": ["enzyme-to-json/serializer"],
  ...
]

我的笑话设置文件:

const enzyme = require('enzyme')
const Adapter = require('enzyme-adapter-react-16')

enzyme.configure({ adapter: new Adapter() })

还有我的packages.json:

"devDependencies": {
  "@types/enzyme": "^3.1.11",
  "@types/jest": "23.1.3",
  "@types/react": "16.4.1",
  "@types/react-native": "0.55.22",
  "babel-jest": "^23.2.0",
  "babel-preset-react-native": "4.0.0",
  "chai": "4.1.2",
  "enzyme": "3.3.0",
  "enzyme-adapter-react-16": "1.1.1",
  "enzyme-to-json": "^3.3.4",
  "jest": "23.2.0",
  "jest-html-reporter": "2.4.0",
  "react-dom": "^16.4.1",
  "remote-redux-devtools": "0.5.12",
  "ts-jest": "22.4.6",
  "typescript": "2.9.2",
  ...
}