如何将笑话测试移动到 /test 文件夹并将它们放到 运行?

How do I move jest tests to a /test folder and get them to run?

edit:我很确定这个问题已经过时了,Jest 从那时起就发生了变化。不要假设答案现在会起作用,甚至是相关的。

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

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

成功找到测试但都失败的:

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)

该函数在 node_modules/react-native/Libraries/Core/Devtools/setupDevtools.js 中。我如何告诉 Jest 在哪里可以找到它?

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

"jest": {
   "preset": "react-native",
    "globals": {
        "__DEV__": true
    },
    "testRegex": "./test/.*.js$",
    "rootDir": "."
 },

(这里是 jcollum:我不知道 __DEV__ 在那里实际做了什么,但如果不是,则会抛出错误。开玩笑 17.0.3)

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

[ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ]

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

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

我认为设置 roots: ['<rootDir>/test/']jest.config.js

中的简单方法
module.exports = {
 roots: ['<rootDir>/test_unit/'],
 ...
}

对我来说效果很好。