Jest: find tests when path includes folder starting with a period

Created on 25 Oct 2018  路  11Comments  路  Source: facebook/jest

馃悰 Bug Report

This is a regression of the bug reported in #3111 that was fixed in #3006 and broke again sometime in the v23 branch.

When a test is inside a folder that has a name starting with period, ., they are ignored by testMatch and by testRegex. It's even more obscure if the entire project is checked out below a dot-folder, in which case all tests are ignored.

To Reproduce

Given the following directory listing:

~/projects/jestbug/.app/__tests__/atest.js
~/projects/jestbug/.app/atest.test.js
~/projects/jestbug/app/atest.test.js
~/projects/jestbug/app/.subfolder/__tests__/atest.js
~/projects/jestbug/app/.subfolder/atest.test.js
~/projects/jestbug/app/__tests__/atest.js
~/projects/jestbug/atest.test.js

Using the following package.json:

{
  "name": "jestbug",
  "devDependencies": {
    "jest": "^23.6.0"
  }
}

None of the tests in a folder staring with . are found:

$ rm -rf node_modules
$ npm install
$ npx jest --listTests --no-cache
~/projects/jestbug/atest.test.js
~/projects/jestbug/app/atest.test.js
~/projects/jestbug/app/__tests__/atest.js

Using the following package.json:

{
  "name": "jestbug",
  "devDependencies": {
    "jest": "^23.6.0"
  },
  "jest": {
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$"
  }
}

None of the tests in a folder staring with . are found:

$ rm -rf node_modules
$ npm install
$ npx jest --listTests --no-cache
~/projects/jestbug/atest.test.js
~/projects/jestbug/app/atest.test.js
~/projects/jestbug/app/__tests__/atest.js

Expected behavior

Using the following package.json:

{
  "name": "jestbug",
  "devDependencies": {
    "jest": "^22.4.4"
  }
}
$ rm -rf node_modules
$ npm install
$ npx jest --listTests --no-cache
~/projects/jestbug/app/atest.test.js
~/projects/jestbug/.app/atest.test.js
~/projects/jestbug/app/.subfolder/atest.test.js
~/projects/jestbug/atest.test.js
~/projects/jestbug/app/.subfolder/__tests__/atest.js
~/projects/jestbug/app/__tests__/atest.js
~/projects/jestbug/.app/__tests__/atest.js

Using the following package.json:

{
  "name": "jestbug",
  "devDependencies": {
    "jest": "^22.4.4"
  },
  "jest": {
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$"
  }
}
$ rm -rf node_modules
$ npm install
$ npx jest --listTests --no-cache
~/projects/jestbug/app/atest.test.js
~/projects/jestbug/.app/atest.test.js
~/projects/jestbug/app/.subfolder/atest.test.js
~/projects/jestbug/atest.test.js
~/projects/jestbug/app/.subfolder/__tests__/atest.js
~/projects/jestbug/app/__tests__/atest.js
~/projects/jestbug/.app/__tests__/atest.js

Link to repl or repo (highly encouraged)

Note that repl.it currently uses Jest v22 and I can't figure out how to make it run under v23.

But here's the demo link where the bug is NOT present because v22 is not broken. https://repl.it/@John_AlbinAlbin/CarpalSoulfulDirectories

Run npx envinfo --preset jest

  System:
    OS: macOS 10.14
    CPU: x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
    Yarn: 1.10.1 - ~/.yarn/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0 

Most helpful comment

this is probably just user error on my end

This bug made me question my sanity at first too.

a co-worker is running my code and not having any issues.

FYI, because of the Jest cache issues, that's exactly what was happening to my team. My co-worker saw the bug and I didn't. Then finally I did see the bug, but after a day it disappeared. Then reappeared. However, we always used Yarn (not npm). AND, the tests that were being skipped were ones that we put in our .storybook folder.

All 11 comments

~Could you set up a quick repository we can pull down and see the error in?~ I copied out your repl to my local machine and can reproduce

The test I said was failing for me in #3111 was actually fixed by me restarting watchman locally...

You can restart watchman by doing watchman watch-del $PWD or watchman watch-del-all (will remove all other projects crawl cache, if they use watchamn).

I can confirm that killing watchman seems to fix this.

Is it a bug in watchman?

I've created https://github.com/JohnAlbin/jest-bug-7267 with the example repo.

I tried watchman watch-del-all and it had no effect on the failure of npx jest --no-cache --listTests to find all 7 tests; it only finds 3 tests.

With your repo:

image

What happens if you do jest --listTests --no-watchman?

npx jest --listTests --no-cache only finds 3 tests (as stated before)

But npx jest --listTests --no-watchman --no-cache finds all 7 tests!

FYI, Jest's cache was giving me inconsistent results; if I installed Jest v22, ran the tests, and then re-installed v23, the tests would all be found until I used the --no-cache option.

I'm not sure if this is related, but I'm experiencing an issue when running jest via yarn.

Given:

"scripts": {
  "jest": "jest .storybook"
}

Then yarn jest fails, while npm run jest runs just fine.

No tests found
In /Users/ewolfe/Projects/<myProject>
  2020 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)+(spec|test).js?(x) - 245 matches
  testPathIgnorePatterns: /node_modules/ - 2020 matches
Pattern: .storybook - 0 matches

Update: this is probably just user error on my end, a co-worker is running my code and not having any issues.

this is probably just user error on my end

This bug made me question my sanity at first too.

a co-worker is running my code and not having any issues.

FYI, because of the Jest cache issues, that's exactly what was happening to my team. My co-worker saw the bug and I didn't. Then finally I did see the bug, but after a day it disappeared. Then reappeared. However, we always used Yarn (not npm). AND, the tests that were being skipped were ones that we put in our .storybook folder.

Hello, me and two other students are trying to fix this issue for a course in college. However, it's our first time working with jest (and any open source project really) so we are not so sure how to work this. We've done an UML components diagram for the whole system but still don't understand the flow of the program and are having a hard time finding the files directly and indirectly involved in this issue. Another problem we are having is in replicating and possibly testing our solution. If you could please give us some guidelines we would be very grateful!

@joanasmramos thats so cool! I'd love to see the UML diagram at some point :D

This video may be helpful for you for understanding how the jest architecture works

That being said, this may not be a good first issue to work on because it sounds like the issue may not be in Jest but in a tool we use called Watchman. You can search our issues with the Good First Issue label to find issues good for people intro'ing to the code base 馃憣

Was this page helpful?
0 / 5 - 0 ratings