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.

Confusing "No tests found" message when using testPathIgnorePatterns

See original GitHub issue

🐛 Bug Report

As noted in issue #1047, Jest ignores all tests if testPathIgnorePatterns matches the path to the repo. This seems like a bug but the Jest devs seem to treat this behavior as intentional, so I am just here to complain that there seems to be a bug in the way Jest reports what it is doing to users. In particular, it tells users that testPathIgnorePatterns has 0 matches.

To Reproduce

  • Make a project whose test file will be “accidentally” ignored:

    mkdir ignored cd ignored npm init -f echo test(‘’,function(){}) > my.test.js

  • In package.json, add an ignore pattern matching a parent folder:

    "jest": {
      "testPathIgnorePatterns": ["ignore.*"]
    }
    
  • Run jest and observe unhelpful error message:

    No tests found
    In C:\Dev\ignored
      2 files checked.
      testMatch: **/__tests__/**/*.js?(x),**/?(*.)+(spec|test).js?(x) - 1 match
      testPathIgnorePatterns: ignore.* - 0 matches
    Pattern:  - 0 matches
    

jest --version: 23.4.0

Expected behavior

The message should be clear that testPathIgnorePatterns is causing files to be ignored. Also I suggest mentioning either that testPathIgnorePatterns can match the parent folder and/or that <rootDir> can avoid that problem. Also, why does Jest print Pattern: - 0 matches? That’s not meaningful.

Suggested output:

No tests found
In C:\Dev\ignored
  2 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)+(spec|test).js?(x) - 1 match
  testPathIgnorePatterns: ignore.* - 1 ignored
Caution: testPathIgnorePatterns does not begin with <rootDir> so
  it is potentially matched against the name of a parent directory.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:6

github_iconTop GitHub Comments

7reactions
ashnurcommented, Oct 19, 2018

also would be nice to know how to use this option from CLI, because there any kind of value basically just makes everything to be ignored all the time…

3reactions
TomasBarrycommented, Feb 21, 2020

@qwertie, @ashnur, @Kamilius

I’ve managed to get testPathIgnorePatterns on the CLI.

The docs are not clear on how to use testPathIgnorePatterns in the CLI however the following will work:

jest --testPathIgnorePatterns=\"pattern\"

For example, if I used a .unit.spec.js and .integration.spec.js suffix on my tests to differentiate between integration and unit tests I could run the following:

# Test only the .unit.spec.js tests (or all tests that don't have .integration.spec.js in the file name)
"jest --testPathIgnorePatterns=\".*.integration.spec.js*\""

# Test only the .integration.spec.js tests (or all tests that don't have .unit.spec.js in the file name)
"jest --testPathIgnorePatterns=\".*.unit.spec.js*\""

And if you want to add this to your package.json then it would look like:

...
"scripts": {
    "test": "jest",
    "test:unit": "jest --testPathIgnorePatterns=\".*.integration.spec.js.*\"",
    "test:integration": "jest --testPathIgnorePatterns=\".*.unit.spec.js.*\"",
}
...

Or you could use:

"scripts": {
    "test": "jest",
    "test:unit": "jest \".*.unit.spec.js\"",
    "test:integration": "jest \".*.integration.spec.js\""
}

In short, jest --testPathIgnorePatterns=\".pattern\" works for the Jest CLI.

Note that I am using Jest 23.6.0 that does not document testPathIgnorePatterns but it is present.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest cli testPathIgnorePatterns with jest.config.js results in No ...
The documentation states that testPathIgnorePatterns is an array. Arrays in shell are comma or space separated. This works:
Read more >
wrong test pattern when running single Jest test, 'no tests found'
)?$" as testRegex does not find it when ran through the IDE, but works fine when running through jest. Works fine if you...
Read more >
always No files found with or without jest.config.js before ...
Failed at the jest-2-demo@1.0.0 test script. npm ERR! This is probably not a problem with npm. There is likely additional logging output ...
Read more >
7 things I learned adding Jest to my existing JavaScript ...
I've wanted to try Jest for a while, but I've already got an existing test suite. This is what I found out whilst...
Read more >
Configuring Jest
By default, Jest runs all tests and produces all errors into the console upon ... if no tests exist for this file and...
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