jest: Can't run a single test file specified by name via the CLI

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.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 24 (12 by maintainers)

Commits related to this issue

Most upvoted comments

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.

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

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

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

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.

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.

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

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.

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