Jest: Confusing "No tests found" message when using testPathIgnorePatterns

Created on 13 Jul 2018  路  3Comments  路  Source: facebook/jest

馃悰 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.

Most helpful comment

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...

All 3 comments

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...

Any progress on this one? Seems like it's not possible to use testPathIgnorePatterns from CLI, as it basically continues to ignore everything

@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.

Was this page helpful?
0 / 5 - 0 ratings