When running tests with --testPathPattern it should match only the relative path to the project root. Currently, it matches the full path.
It's not usefull to match the full path because all tests have a common prefix.
Checkout my repo to directory like /users/<name>/my-project and run
yarn run jest --testPathPattern=users
Example:

it should match only the relative path.
Instead matching
/Users/sky/work/npm/jest-test-path-pattern-bug/custom.test.js
it should match
custom.test.js
// or with subdirectories
sub-dir/another-subdir/custom.test.js
https://github.com/BetterCallSky/jest-test-path-pattern-bug
npx envinfo --preset jestPaste the results here:
System:
OS: macOS High Sierra 10.13.6
CPU: (12) x64 Intel(R) Xeon(R) CPU E5-1650 v2 @ 3.50GHz
Binaries:
Node: 8.11.3 - ~/.nvm/versions/node/v8.11.3/bin/node
Yarn: 1.13.0 - ~/.nvm/versions/node/v8.11.3/bin/yarn
npm: 6.4.1 - ~/.nvm/versions/node/v8.11.3/bin/npm
npmPackages:
jest: ^24.5.0 => 24.5.0
https://github.com/facebook/jest/blob/master/packages/jest-core/src/SearchSource.ts#L116
Right now path here is an absolute path. I wonder if it's as simple as doing
isMatch: (path: Config.Path) => regex.test(path.replace("/Users", ""),
Reason being, only osx has '/Users'
Edit:
Willing to send in a PR to fix this.
A more generic approach would be to use a relative path.
const path = require('path');
isMatch: p => regex.test(path.relative(basePath, p))
basePath should be a rootPath or pwd
~Not sure how that interaction would play out with the rootDir option.~
Edit: I've had some time to take a look at this and it's not so straight forward. Technically, any filenames or paths you pass as arguments to jest-cli becomes a regex which then gets tested against the absolute path to the test file.
I've realized that the following are equivalent in what they do.
jest ./custom.test.js
jest --testPathPattern=./custom.test.js
I find it to be confusing. @SimenB I guess this should also be included in #7185
I'm not sure what the desired behavior here would be. For example, right now I sometimes use checked-out-repo/test.js to run only the test.js in the repo root. The relative path would be just test.js.
I do agree though that the current behavior is confusing in many cases.
This issue is actually more complicated than I anticipated.
Passing in a value that appears in the absolute path to the project directory causes jest to run __all__ the tests. E.g. running jest users on macos matches all tests since /Users/some/path matches the path.
The definition of what a relative path means is confusing. Echo-ing @jeysal 's example.
testPathPattern is a confusing option since doing jest --testPathPattern=hello and jest hello achieve the same thing.
I ran into the same problem, we were able to get around this by setting the rootDir in the jest.config.js file. It's sub optimal, but it works.
module.exports = {
rootDir: process.env.PWD,
};
This is a big problem. Common folders to match using testPathPatterns would be things like src and test. If 5 directories up from your project, there is a folder with that name, every single file will always match.
This has happened to me multiple times already under different circumstances. Using gopath for all your projects has this issue especially, because of the src folder in there.
Most helpful comment
This is a big problem. Common folders to match using
testPathPatterns would be things likesrcandtest. If 5 directories up from your project, there is a folder with that name, every single file will always match.This has happened to me multiple times already under different circumstances. Using gopath for all your projects has this issue especially, because of the
srcfolder in there.