如何将jest测试移动到/ test文件夹并让它们运行?

jco*_*lum 10 javascript reactjs jestjs react-native

Jest希望测试位于__tests__文件夹中或命名为blah.test.js. 我不喜欢这两种模式.我希望我的测试文件存在/test并命名它们test/index.ios.test.js似乎是多余和愚蠢的.我的第一个尝试是将package.json中的jest配置更改为:

"jest": {
    "testPathDirs": ["test"],
    "testRegex": "\\.(js|jsx)$",
    "preset": "react-native"
}
Run Code Online (Sandbox Code Playgroud)

哪个成功找到测试,但都失败了:

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

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:151:17)
  at Object.<anonymous> (node_modules/react-native/jest/setup.js:23:1)
Run Code Online (Sandbox Code Playgroud)

那个功能在node_modules/react-native/Libraries/Core/Devtools/setupDevtools.js.我怎么告诉Jest在哪里找到它?

And*_*rle 14

我通过设置正则表达式解决了这个问题,因此它找到.js了我./test文件夹中的所有文件:

"jest": {
   "preset": "react-native",
    "globals": {
        "__DEV__": true
    },
    "testRegex": "./test/.*.js$",
    "rootDir": "."
 },
Run Code Online (Sandbox Code Playgroud)

(jcollum在这里:我不知道__DEV__实际上在那里做了什么但是如果不是则会抛出错误.jest 17.0.3)


Ult*_*ire 7

我认为设置roots: ['<rootDir>/test/']方法很简单jest.config.js

module.exports = {
 roots: ['<rootDir>/test_unit/'],
 ...
}
Run Code Online (Sandbox Code Playgroud)

这对我来说很有效。


Jie*_*ert 5

我认为解决此问题的最佳方法是配置testMatch。默认为:

[ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ]
Run Code Online (Sandbox Code Playgroud)

因此,您可以更改__tests__tests数组或在数组中添加一个新条目来检查两者。我在package.json中使用以下内容:

  "jest": {
    "testMatch": [ "**/tests/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ]
  },
Run Code Online (Sandbox Code Playgroud)