question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mraxuscommented, Apr 15, 2018

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

1reaction
mraxuscommented, Apr 13, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

testRegex mask picks wrong files to run tests - Stack Overflow
Currently, I put all my test files under __tests__ folder and name them as "something.test.js". However, if you look at the testRegex above, ......
Read more >
GNU Findutils 4.9.0
locate searches special file name databases for file names that match patterns. ... named in its parent directory, and the . entry inside...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run ......
Read more >
Get parent of current directory using Python - GeeksforGeeks
path* modules include many functions to interact with the file system. OS module provides various ways for getting the parent directory. Some of ......
Read more >
Renaming many files to their parent directory name [duplicate]
I want to recursively change many files named raw.tex to parent-directory-hw.tex . Is there a simple scripting solution to this problem?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found