Jest changed test directory resulting beforeAll is not defined

时间:2018-06-04 17:10:25

标签: node.js testing jestjs

I used to put all my tests together inside of __test__ directory. But I decided to put each test files into each component's directory with different name convention.

Old Structure:

src/
 __test__/
  example.test.js
  example2.test.js

New Structure:

src/
 components/
  example/
   example-controller.js
   example-model.js
   example-route.js
   example-test.js

As naming and location of a directory changed, I updated all import statement and namings for according files. I was using default feature of Jest but since I changed test file name to example-test from example.test I also updated package.json

"jest": {
          "testRegex": "./src/components/*/.*.-test.js$"
 },

Problem is when I run the project, npm run it throws

beforeAll((0, _asyncToGenerator3.default)( /#PURE/_regenerator2.default.mark(function _callee() {

ReferenceError: beforeAll is not defined

any idea why it is happening?

1 个答案:

答案 0 :(得分:1)

尝试使用文件路径regextester,然后感到沮丧。另外,使用jest查看一些流行的现有javascript项目,并检查其jest配置,以了解社区如何适应项目结构。跟其他人看似很简单,但是大多数软件工程师并不是一个人工作,通常会就好的做法达成共识。

考虑一年后再回到该项目;您正在更改一个简单的过程的默认行为,该过程开箱即用。 git blame之后,有人可能会讨厌您。

如果要做的只是检查所有-test.js文件,请慢慢修改default config直到匹配所需的文件路径。

(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$

(对您来说)应该是:

(.*/src/.*(-test).jsx?)