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

Can't run a single test file specified by name via the CLI #3793

Closed
dandv opened this issue Jun 12, 2017 · 24 comments · Fixed by #5054
Closed

Can't run a single test file specified by name via the CLI #3793

dandv opened this issue Jun 12, 2017 · 24 comments · Fixed by #5054

Comments

@dandv
Copy link
Contributor

dandv commented Jun 12, 2017

The docs claim that I should be able to run jest path/to/my-test.js. When I do that, I get this error:

$ ./node_modules/.bin/jest /home/dandv/jest/my-test.js
No tests found
In /home/dandv/jest
  2 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 1 match
  testPathIgnorePatterns: /node_modules/ - 2 matches
Pattern: "/home/dandv/jest/my-test.js" - 0 matches

$ ls my-test.js 
my-test.js

I'm using jest v20.0.4 and zsh. Running ./node_modules/.bin/jest ./my-test.js under bash produces the same No tests found result.

@rogeliog
Copy link
Contributor

Does it work when you pass only my-test.js?

@dandv
Copy link
Contributor Author

dandv commented Jun 19, 2017

@rogeliog: no. That was the first thing I've tried.

@thymikee
Copy link
Collaborator

Can you rename file to my.test.js? Looks like testMatch doesn't match your filename, but I may be wrong on that, looking through my phone

@dandv
Copy link
Contributor Author

dandv commented Jun 19, 2017

@thymikee: If I rename the file to *.test.js that does match the default wildcard and the test runs, but that's the point of the bug - Jest should run a specific test file that's passed to it, regardless of the extension.

@thymikee
Copy link
Collaborator

@dandv I think it would be nice to make it work, but It's not a priority. Feel free to work on this :)

@rogeliog
Copy link
Contributor

Maybe you can change the testMatch in your confit to match that extension

@dandv
Copy link
Contributor Author

dandv commented Jun 21, 2017

I think it would be nice to make it work

The docs claim it works, and every single command-line build tool I've ever used that takes a file parameter, actually uses that particular file. Jest doesn't, which is super counter-intuitive.

Maybe you can change the testMatch in your confit to match that extension

That will only hide the problem above.

but It's not a priority. Feel free to work on this :)

I understand. Not sure I really have the time to dive into yet another new project right now though.

@kkutsner
Copy link

I assume the problem is that Jest is always using regex to match file names. When you are on Windows, and the path separator is \, then passing full path to the test never works. The real use case is when you are using VS Code and there is a launch.json file to debug test:

 {
            "type": "node",
            "request": "launch",
            "name": "Debug Tests",
            "program": "${workspaceRoot}/node_modules/jest-cli/bin/jest",
            "args": ["--runInBand", "--config", "${workspaceRoot}/jest.json", "${file}"],
            "preLaunchTask": "build",
            "outFiles": [
                "${workspaceRoot}/build/**/*"
            ],
            "cwd" : "${workspaceRoot}",
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "console": "internalConsole",
            "sourceMaps": true
        }

The macro ${file} will populate path using windows file separator \ and as a result the whole thing will be incorrect regex expression. I would consider this issue as annoying bug rather than just an enhancement.

@lindgr3n
Copy link
Contributor

lindgr3n commented Oct 4, 2017

Got a similar problem running in windows.

λ node_modules\.bin\jest.cmd -- packages\package1\src\sum.test.js

> jest-single-test@1.0.0 test C:\tmp\jest-single-test
> jest "packages\package1\src\sum.test.js"

No tests found
In C:\tmp\jest-single-test
  5 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 1 match
  testPathIgnorePatterns: \\node_modules\\ - 5 matches
Pattern: packages\package1\src\sum.test.js - 0 matches

Running similar command on Mac the test is found.

$ node_modules/.bin/jest -- packages/package1/src/sum.test.js 
 PASS  packages/package1/src/sum.test.js
  <package1> - sum 
    ✓ Should sum (6ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.863s, estimated 2s
Ran all test suites matching /packages\/package1\/src\/sum.test.js/i.

Made a repo to reproduce it. https://github.com/lindgr3n/jest-single-test

Note: that running following works on windows so you can work around it in this case.

λ node_modules\.bin\jest.cmd -- packages\\package1\\src\\sum.test.js

> jest-single-test@1.0.0 test C:\tmp\jest-single-test
> jest "packages\\package1\\src\\sum.test.js"

 PASS  packages\package1\src\sum.test.js
  <package1> - sum
    √ Should sum (4ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.087s
Ran all test suites matching /packages\\package1\\src\\sum.test.js/i.
C:\tmp\jest-single-test (master) (jest-single-test@1.0.0)
λ node_modules\.bin\jest.cmd -- sum.test.js

> jest-single-test@1.0.0 test C:\tmp\jest-single-test
> jest "sum.test.js"

 PASS  packages\package1\src\sum.test.js
  <package1> - sum
    √ Should sum (4ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.072s
Ran all test suites matching /sum.test.js/i.

@willmruzek
Copy link

I'll take this up.

@willmruzek
Copy link

This appears to be working in v21.2.1 possibly in just v21.x.x...

@thymikee
Copy link
Collaborator

thymikee commented Oct 6, 2017

@dandv can you confirm that in your case?

@andreme
Copy link

andreme commented Nov 10, 2017

@mruzekw @thymikee I think there is still an issue with this, I need to escape the backslash to make it work. (Windows 10, plain Command Prompt)

Not escaped doesn't find the test:

...>yarn test docket\index
yarn run v1.3.2
$ jest --watch --noStackTrace docket\index
No tests found
In C:\Projects\Palletwatch\webapp
  368 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 42 matches
  testPathIgnorePatterns: \\node_modules\\ - 368 matches
Pattern: docket\index - 0 matches

Active Filters: filename /docket\index/
 › Press c to clear filters.

...

This one works:

...>yarn test docket\\index
yarn run v1.3.2
$ jest --watch --noStackTrace docket\\index
 PASS  source\module\docket\index.test.js

Test Suites: 1 passed, 1 total
Tests:       49 passed, 49 total
Snapshots:   0 total
Time:        3.665s, estimated 4s
Ran all test suites matching /docket\\index/i.

Active Filters: filename /docket\\index/
 › Press c to clear filters.

...

@seanpoulter
Copy link
Contributor

I've just run into the same problem which affects teams developing across platforms. Someone's got their test script running jest test/unit/specs which doesn't work on Windows with Jest 21.2.1.

jest a.test.js          # matches
jest test/unit/specs    # no match
jest test\unit\specs    # no match
jest test\\unit\\specs  # matches

If we drop \ as a valid filename character on *nix systems it would make the problem a lot simpler (e.g.: path.normalize).

@Hetachi
Copy link

Hetachi commented Dec 7, 2017

Has anyone come up with a temporary solution to this issue? As couple of new guys in our project with windows laptops are having issues. And it gets quite fast annoying to escape backslash every time you want to run a single test file.

@seanpoulter
Copy link
Contributor

Alright @Hetachi, the thread bump worked. I'll submit a PR after figuring out how to test this.

cpojer pushed a commit that referenced this issue Dec 11, 2017
* Add tests for current testPathPattern behavior

* Escape Windows path separator in testPathPattern CLI arg

This fixes #3793.

* Add PR to CHANGELOG.md

* Insert ';'
@seanpoulter
Copy link
Contributor

@cpojer, could you please reopen this issue? #5054 didn't fix the original issue that #3882 will. We still need to sort out how \ is ambiguous as a path sep and regular expression escape char on Windows though.

The conversation in #5216 brought up the edge case when the Windows \ path separator and regular expression escape can be ambiguous. @butchyyyy provided a great example, dev\sample\d\*\.js. Consider the following directory structure on Windows and --testPathPattern dev\sample\d. The suggested change in #3882 will only run for dev\sample\d and ignore dev\sample<any number of digits> which is a breaking change.

- dev\
  - sample\
    - d\
  - sample1\
  - sample10\

I'd like to make 3 suggestions:

  1. We add to Allow unmatched test paths from CLI #3882 to make sure it doesn't match other files as a regular expression. If it does and we have this silly edge case, we can warn the user that \ is ambiguous and they should use / (or escape \\ on Windows).

  2. If the testPathPattern does not match anything (and it's not a valid path) then we can check if the pattern is using the wrong path separator for the OS. If it is, we can encourage the user to use / (or escape \\ on Windows).

  3. We can update the docs for --testPathPattern and <regexForTestFiles> to explain that:

  • / can be used as a path separator on any OS
  • \ path separators should be escaped on Windows
  • if the patterns match existing paths but nothing else as a regular expression, we'll use it. That'd give Windows users some leniency to run dev\sample10\etc.

@SimenB
Copy link
Member

SimenB commented Feb 13, 2018

#3882

@SimenB SimenB closed this as completed Feb 13, 2018
@mindriven
Copy link

mindriven commented Mar 5, 2018

Thanks for your hard work. I am still experiencing this issue on Win 10 using version 22.1.2. If I use forward slashes pattern like so:

node node_modules\jest\bin\jest.js src/test/test.spec.js

it says

Pattern: "src/test/test.spec.js" - 0 matches

This behavior is observable on both cmd and PS

If I use escaped Windows style (\) it works, but this is not good enough for me (I'm trying to configure VSCode to run tests from current file and I can't escape backward slashes in fileName variable)

@SimenB
Copy link
Member

SimenB commented Mar 5, 2018

Same issue with 22.4.2?

@mindriven
Copy link

this version works as expected! Thanks.
I see now that got jest as a transitive dependency and I am not in explicit control of it's version... sorry for the confusion.

@dandv
Copy link
Contributor Author

dandv commented Jul 15, 2018

This still doesn't seem fully fixed in 23.4.1 - files with other extensions (notably .mjs) aren't found.

$ mkdir jestbug && cd jestbug
$ npm init -y
$ npm install --save-dev jest
$ echo "foo" > test.mjs
$ node_modules/.bin/jest test.mjs
No tests found
In /home/dandv/jestbug
  2 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 matches
  testPathIgnorePatterns: /node_modules/ - 2 matches
Pattern: test.mjs - 0 matches
$ cat test.mjs
foo

I think that regardless of the value of testMatch, if I pass an explicit filename, jest should execute that test.

Passing a matching wildcard to --testMatch still doesn't work:

$ node_modules/.bin/jest --testMatch te*
No tests found
In /home/dandv/jestbug
  1 file checked.
  testMatch: te* - 0 matches
  testPathIgnorePatterns: /node_modules/ - 2 matches
Pattern:  - 0 matches

@johannhuang
Copy link

In case the later comers would also wonder why.

I found out the reason could be because jest is very opinionated. By default, no matter what (file-path-pattern (including the one file path)) you provide to the cli command, jest only accepts those files whose paths match against [ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ].

And of course, one can change the default using CLI option or in config file.
That's it!

@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 May 11, 2021
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.