Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--testPathPattern should only match relative path #8226

Closed
lstkz opened this issue Mar 27, 2019 · 11 comments
Closed

--testPathPattern should only match relative path #8226

lstkz opened this issue Mar 27, 2019 · 11 comments

Comments

@lstkz
Copy link
Contributor

lstkz commented Mar 27, 2019

🐛 Bug Report

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.

To Reproduce

Checkout my repo to directory like /users/<name>/my-project and run

yarn run jest --testPathPattern=users

Example:
image

Expected behavior

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

Link to repl or repo (highly encouraged)

https://github.com/BetterCallSky/jest-test-path-pattern-bug

Run npx envinfo --preset jest

Paste 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 
@natealcedo
Copy link

natealcedo commented Mar 27, 2019

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.

@lstkz
Copy link
Contributor Author

lstkz commented Mar 27, 2019

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

@natealcedo
Copy link

natealcedo commented Mar 27, 2019

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

@jeysal
Copy link
Contributor

jeysal commented Mar 30, 2019

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.

@natealcedo
Copy link

This issue is actually more complicated than I anticipated.

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

  2. The definition of what a relative path means is confusing. Echo-ing @jeysal 's example.

  3. testPathPattern is a confusing option since doing jest --testPathPattern=hello and jest hello achieve the same thing.

@rmclaughlin-nelnet
Copy link

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,
};```

@kasvtv
Copy link

kasvtv commented Feb 26, 2020

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.

victorges added a commit to livepeer/studio that referenced this issue Jun 3, 2021
This is what really fixed the test run in the end.
For details: jestjs/jest#8226
victorges added a commit to livepeer/studio that referenced this issue Jun 3, 2021
* Ignore test and test data files from babel

This was one of the ways of fixing the jest issue,
to avoid having test files under the dist package.

It did not fix it completely because we still have
a `test.js` file on the root of the package (lmk if
we should remove it too)

It also doesn't fix the root cause of the issue,
which I found later.

* Fix test files regex sent as jest argument

This is what really fixed the test run in the end.
For details: jestjs/jest#8226

* Create coverage script in api package.json

Simply calls the regular test with an additional arg.
This makes the root `yarn coverage` start working again,
since it looks for a script with that exact name in the
internal packages.

* Fix swagger schema tags descriptions

This is unrelated to the other changes but
really small so I felt I could just include
it here.

Just what seems like a typo having the tags
with inverted descriptions.

* Fix build: revert rm of --help on docker:build validation

I probably removed it in some of my tests and actually
forgot to undo it later.

This is was what was breaking the build since it tried
to really run the API and didn't manage to reach the
postgresql running locally.
@lquanx
Copy link

lquanx commented Jan 17, 2022

Here is a workaround worked for me.
yarn run jest --testPathPattern="$PWD/users"
or just
yarn run jest "$PWD/users"

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 17, 2023
@github-actions
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2023
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants