Jest: Unexpected test file matching due to parent directory name interfere with `testRegex`

Created on 12 Apr 2018  路  6Comments  路  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?
bug

What is the current behavior?
Using config option testRegex that does not include test/... something, having tests in test/xxx.test.js will still be run.
The example in my case is: { "testRegex": "src/.*\.test\.js$" }

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

See https://github.com/mraxus/jest-always-test-test and use commands yarn test and yarn fail to see expected/unexpected behaviour

What is the expected behaviour?
yarn fail does not pass where it should.

Please provide your exact Jest configuration

module.exports = {
    testEnvironment: 'node',
    testRegex: 'test/unit/.*\\.test\\.js$',
};

vs

module.exports = {
    testEnvironment: 'node',
    testRegex: 'src/.*\\.test\\.js$',
};

Run npx envinfo --preset jest in your project directory and paste the
results here

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
  Binaries:
    Node: 8.11.1
    Yarn: 1.5.1
    npm: 5.8.0
  npmPackages:
    jest:
      wanted: ^22.4.3
      installed: 22.4.3
Documentation good first issue

All 6 comments

@mraxus both of your tests in that repo pass for me:

So after some more troubleshooting with a colleague, I now understand what was going on.
So I have the absolute path as Users/me/src/js/project/. And as he pointed out, the testRegex uses the absolute path, not relative, which is not stated in the docs.

Personally I think the regex should only match the relative path as this is only what is relevant. Otherwise you get unexpected behaviour. Just like this case >p

But using the config: testMatch: ['<rootDir>/src/**/*.test.js'], then I can get the tests running as expected.

So my suggestion would be to either:

  • make testRegex path relative
  • clarify that these regex functions always work with absolute paths.

I will update the issue title

I feel like we got another issue (or PR) for the same thing not too long ago, but I'm unable to locate it...

Using <rootDir> is our standard way to opt into relative so I think that makes sense here

@mraxus would you be able to update the docs?

@rickhanlonii I will check it out and see what I can provide!

Docs updated, thanks @binygal

Was this page helpful?
0 / 5 - 0 ratings