Skip to content

testPathPattern is messed up when running test #838

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

Closed
smarschollek opened this issue Feb 18, 2022 · 20 comments
Closed

testPathPattern is messed up when running test #838

smarschollek opened this issue Feb 18, 2022 · 20 comments
Labels
external-issue issues caused by external system ( (no fix needed)

Comments

@smarschollek
Copy link

Environment

  1. vscode-jest version: 4.2.1

  2. node -v: v16.13.1

  3. npm --version: 8.1.2

  4. npm ls jest or npm ls react-scripts (if you haven’t ejected): [email protected]

  5. your vscode-jest settings if customized:

    • jest.jestCommandLine? npm test --
    • jest.autoRun? off
  6. Operating system: Windows 11

Prerequisite

  • are you able to run jest test from the command line? yes
  • how do you run your tests from the command line? (for example: npm run test or node_modules/.bin/jest) npm test --watchAll=false

Steps to Reproduce

  1. create-react-app project
  2. run the "renders learn react link"

Expected Behavior

the tests should run normally

Actual Behavior

[email protected] test
react-scripts test "--testLocationInResults" "--json" "--useStderr" "--outputFile" "C:\Users\smars\AppData\Local\Temp\jest_runner_my_app.json" "--testNamePattern" "renders learn react link" "--no-coverage" "--reporters" "default" "--reporters" "c:\Users\smars\.vscode\extensions\orta.vscode-jest-4.2.1\out\reporter.js" "--colors" "--watchAll=false" "--testPathPattern" "d:\\repos\\my-app\\src\\App\.test\.tsx"

No tests found, exiting with code 1
Run with --passWithNoTests to exit with code 0
In D:\repos\my-app
6 files checked.
testMatch: D:/repos/my-app/src//tests//.{js,jsx,ts,tsx}, D:/repos/my-app/src/**/.{spec,test}.{js,jsx,ts,tsx} - 1 match
testPathIgnorePatterns: \node_modules\ - 6 matches
testRegex: - 0 matches
Pattern: d:\\repos\\my-app\\src\\App\.test\.tsx - 0 matches


for some reason the testPathPattern is messed up. it works fine when i use yarn as the command

@connectdotz
Copy link
Collaborator

looks like this might be caused by the testMatch config uses upper case 'D', while the --testPathPattern used lower case... @smarschollek can you try to change the testMatch to accept lower case or no drive letter at all? This will confirm our hypothesis and help address the issue. thanks.

@smarschollek
Copy link
Author

hi @connectdotz

okay so i tested your id with the upper case 'D' in testMatch and lower case 'd' in the testPathPattern on the console and it does not affect jest there.
but when i just copy the command from above into the console it will not work i had to change the testPathPattern.
In the end this is the command that worked

> [email protected] test
> react-scripts test "--testLocationInResults" "--json" "--useStderr" "--outputFile" 
"C:\\Users\\smars\\AppData\\Local\\Temp\\jest_runner_my_app.json" "--testNamePattern" "renders learn react link" 
"--no-coverage" "--reporters" "default" "--reporters" "c:\\Users\\smars\\.vscode\\extensions\\orta.vscode-jest-4.2.1\\out\\reporter.js" 
"--colors" "--watchAll=false" "--testPathPattern" "d:\\repos\\my-app\\src\\App.test.tsx"

onRunStart
 PASS  src/App.test.tsx
  √ renders learn react link (25 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.889 s, estimated 1 s
Ran all test suites matching /d:\\repos\\my-app\\src\\App.test.tsx/i with tests matching "renders learn react link".
onRunComplete
Test results written to: C:\Users\smars\AppData\Local\Temp\jest_runner_my_app.json

dont know why the extension added the extra \ in the filename but it looks strange that the normal command has "d:\\repos\\my-app\\src\\App\.test\.tsx" instead of "d:\repos\my-app\src\App.test.tsx"

so these are the results for now. if you need something else i am happy to help you.

@AntoninGK
Copy link

AntoninGK commented Feb 23, 2022

Hi!

I just encountered the same problem.
I am using the default testMatch from Jest.

This is the output of vscode-jest:

> [email protected] test C:\Users\agi.KARETIS\Documents\GitHub\kplan\web
> cross-env NODE_ENV=test jest "--testLocationInResults" "--json" "--useStderr" "--outputFile" 
"C:\Users\AGI~1.KAR\AppData\Local\Temp\jest_runner_kplan.json" "--testNamePattern" "simple should be sane" 
"--no-coverage" "--reporters" "default" "--reporters" "c:\Users\agi.KARETIS\.vscode\extensions\orta.vscode-jest-4.2.1\out\reporter.js" 
"--colors" "--watchAll=false" "--testPathPattern" "c:\\Users\\agi\.KARETIS\\Documents\\GitHub\\kplan\\web\\test\\simple\.test\.js"

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\Users\agi.KARETIS\Documents\GitHub\kplan\web
  667 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 28 matches
  testPathIgnorePatterns: \\node_modules\\ - 667 matches
  testRegex:  - 0 matches
Pattern: c:\Users\agi\.KARETIS\Documents\GitHub\kplan\web\test\simple\.test\.js - 0 matches
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `cross-env NODE_ENV=test jest "--testLocationInResults" "--json" "--useStderr" "--outputFile" 
"C:\Users\AGI~1.KAR\AppData\Local\Temp\jest_runner_kplan.json" "--testNamePattern" "simple should be sane" 
"--no-coverage" "--reporters" "default" "--reporters" "c:\Users\agi.KARETIS\.vscode\extensions\orta.vscode-jest-4.2.1\out\reporter.js" 
"--colors" "--watchAll=false" "--testPathPattern" "c:\\Users\\agi\.KARETIS\\Documents\\GitHub\\kplan\\web\\test\\simple\.test\.js"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\agi.KARETIS\AppData\Roaming\npm-cache\_logs\2022-02-23T08_42_59_872Z-debug.log

So, I wanted to test the command line by hand and the only way I found to make it works was to replace all the \\ with / (see example bellow).
I tried in Git Bash and Command Prompt and the result is the same.

npm run test -- "--testLocationInResults" "--json" "--useStderr" "--outputFile" 
"C:\Users\AGI~1.KAR\AppData\Local\Temp\jest_runner_kplan.json" "--testNamePattern" "simple should be sane" 
"--no-coverage" "--reporters" "default" "--reporters" "c:\Users\agi.KARETIS\.vscode\extensions\orta.vscode-jest-4.2.1\out\reporter.js" 
"--colors" "--watchAll=false" "--testPathPattern" "c:/Users/agi\.KARETIS/Documents/GitHub/kplan/web/test/simple\.test\.js"

> [email protected] test C:\Users\agi.KARETIS\Documents\GitHub\kplan\web
> cross-env NODE_ENV=test jest "--testLocationInResults" "--json" "--useStderr" "--outputFile" 
"C:\Users\AGI~1.KAR\AppData\Local\Temp\jest_runner_kplan.json" "--testNamePattern" "simple should be sane" 
"--no-coverage" "--reporters" "default" "--reporters" "c:\Users\agi.KARETIS\.vscode\extensions\orta.vscode-jest-4.2.1\out\reporter.js" 
"--colors" "--watchAll=false" "--testPathPattern" "c:/Users/agi\.KARETIS/Documents/GitHub/kplan/web/test/simple\.test\.js"

 PASS  test/simple.test.js
  simple
    √ should be sane (2 ms)

onRunStart
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.583 s
Ran all test suites matching /c:\\Users\\agi\.KARETIS\\Documents\\GitHub\\kplan\\web\\test\\simple\.test\.js/i with tests matching "simple should be sane".
onRunComplete
Test results written to: ..\..\..\..\..\AGI~1.KAR\AppData\Local\Temp\jest_runner_kplan.json

Let me know if you need further information.


EDIT:

I enabled the autorun when saving files and the tests are running correctly.
I checked the command line and discovered that it is not using the same arguments.

 npm run test -- "--testLocationInResults" "--json" "--useStderr" "--outputFile" 
"C:\Users\AGI~1.KAR\AppData\Local\Temp\jest_runner_kplan.json" 
"C:\Users\agi.KARETIS\Documents\GitHub\kplan\web\test\simple.test.js" "--no-coverage" "--reporters" "default" "--reporters" 
"c:\Users\agi.KARETIS\.vscode\extensions\orta.vscode-jest-4.2.1\out\reporter.js" "--colors" "--findRelatedTests" 
"--watchAll=false"

Adding --testNamePattern at the end as bellow did the trick:

 npm run test -- "--testLocationInResults" "--json" "--useStderr" "--outputFile" 
"C:\Users\AGI~1.KAR\AppData\Local\Temp\jest_runner_kplan.json" 
"C:\Users\agi.KARETIS\Documents\GitHub\kplan\web\test\simple.test.js" "--no-coverage" "--reporters" "default" "--reporters" 
"c:\Users\agi.KARETIS\.vscode\extensions\orta.vscode-jest-4.2.1\out\reporter.js" "--colors" "--findRelatedTests" 
"--watchAll=false" "--testNamePattern" "simple should be sane"

@connectdotz
Copy link
Collaborator

connectdotz commented Feb 24, 2022

this issue is puzzling... I could not reproduce it in windows 10. The --testPathPattern looked right to me. Since jest is expecting a "regex" string, we used \\ to escape windows path separator, and single \ to escape "." as literal. The drive letter case turns out to be irrelevant because jest uses the case-insensitive regex match.

This code has been released for a while, I would expect every windows user would have bumped into this problem if it is a general issue, but it did not seem to be... Are people who have this problem all using windows 11? using react-native x? Did it work before and something recently changed that you think might trigger the issue?

@AntoninGK
Copy link

AntoninGK commented Feb 25, 2022

@connectdotz I'm on Windows 10 with a normal react project.


I started a new project from scratch to test and didn't manage to replicate either.
It must be because of a Jest configuration or a version of an npm package.

Also I finally managed to run the tests from the extension.
My initial .vscode/settings.json was the following:

{
  "jest.rootPath": "web",
  "jest.jestCommandLine": "npm run test --",
  "jest.autoRun": {
    "watch": false
  }
}

I don't know why but by removing the jestCommandLine, the tests run corretly...

@connectdotz
Copy link
Collaborator

connectdotz commented Feb 25, 2022

@AntoninGK very valuable information 👍

Based on your observation, I went looking for hints in npm and found quite a few related issues, such as npm/cli#3680. I was able to reproduce the issue after switching from yarn to npm. It seems npm has changed how it handles the run script parameters since 7.x, There are unmerged PR (npm/run-script#31) that tries to address this issue but unfortunately not merged yet...

I see a few workarounds for this issue:

  1. switch from npm to yarn
  2. use an older version of npm (6.14.16 for example). I used nvm to switch to node 14.19.0, which brought in npm 6.14.16, and all is fine again.
  3. by-pass npm and invoke the script directly, for example: in settings.json add "jest.jestCommandLine": "node_modules\\.bin\\react-scripts.cmd test --env=jsdom"

Let me know if any of the workarounds helped.

@connectdotz connectdotz added the external-issue issues caused by external system ( (no fix needed) label Feb 25, 2022
@AntoninGK
Copy link

@connectdotz thanks for these details!
Unfortunately, the project I'm working on already uses node v14.19.0 so this didn't help and I don't have the possibility to switch to yarn.

However, I did some digging into what you said about the command line not being well escaped.
I compared my project with a fresh new create-react-app and finally figured it out!

My test script of my current project is: cross-env NODE_ENV=test jest and not the the classic react-scripts test.
I discovered that cross-env was not escaping correctly the filename:
filename in cross-env: c:\\Users\\agi\.KARETIS\\Documents\\GitHub\\kplan\\web\\test\\simple\.test\.js
filename in react-scripts: c:\\\\Users\\\\agi\\.KARETIS\\\\Documents\\\\GitHub\\\\kplan\\\\web\\\\test\\\\simple\\.test\\.js

So, switching to react-scripts solved the problem, thank you for your help!

@smarschollek
Copy link
Author

@connectdotz
i switched to yarn and everyting works fine. thanks!

@cosminstirbu
Copy link

We have a similar problem when using craco.

testPathPattern has an in inconsistent number of \, sometimes it has 6, sometimes it has 4.

Please see log below:

craco:  Override started with arguments:  [
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\Repos\\MyCompany\\MyApp\\src\\MyCompany.MyApp.Frontend.UI\\node_modules\\@craco\\craco\\scripts\\test.js',
  '--verbose',
  '--testLocationInResults',
  '--json',
  '--useStderr',
  '--outputFile',
  'C:\\\\Users\\\\cosminst\\\\AppData\\\\Local\\\\Temp\\\\jest_runner_MyCompany_MyApp_frontend_ui.json',
  '--testNamePattern',
  'Pricing history data tests Displays no pricing history when empty',
  '--no-coverage',
  '--reporters',
  'default',
  '--reporters',
  'c:\\\\Users\\\\cosminst\\\\.vscode\\\\extensions\\\\orta.vscode-jest-4.2.1\\\\out\\\\reporter.js',
  '--colors',
  '--watchAll=false',
  '--testPathPattern',
  'c:\\\\\\\\Repos\\\\\\\\MyCompany\\\\\\\\MyApp\\\\\\\\src\\\\\\\\MyCompany\\\\.MyApp\\\\.Frontend\\\\.UI\\\\\\\\src\\\\\\\\test\\\\\\\\PricingHistory\\\\\\\\PricingHistory\\\\.Data\\\\.test\\\\.tsx'
]

@cosminstirbu
Copy link

Interestingly enough, if you Debug (instead of Run) and individual test then it works as expected.

See output below:

craco:  Override started with arguments:  [
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\Repos\\MyCompany\\MyApp\\src\\MyCompany.MyApp.Frontend.UI\\node_modules\\@craco\\craco\\scripts\\test.js',
  '--verbose',
  '--runInBand',
  '--watchAll=false',
  '--testNamePattern',
  'Pricing history data tests Displays no pricing history when empty',
  '--runTestsByPath',
  'C:\\\\Repos\\\\MyCompany\\\\MyApp\\\\src\\\\MyCompany.MyApp.Frontend.UI\\\\src\\\\test\\\\PricingHistory\\\\PricingHistory.Data.test.tsx'
]

@connectdotz
Copy link
Collaborator

@cosminstirbu during Debug it uses --runTestsByPath, while Run uses --testPathPattern (regex string), thus different string escape. However, neither should be more than 2 backslashes unless craco CLI is doing extra escaping? Do you have a sample repo? and what is your npm version and error message? it might be more efficient if you create a new issue with your specific use case/repo and config detail...

@cosminstirbu
Copy link

I have created a separate issue for this, with a repro project #852

@AlMiraiABC
Copy link

Same issue.

> react-app-rewired test "--testLocationInResults" "--json" "--useStderr" "--outputFile" "C:\\Users\\HaoZh\\AppData\\Local\\Temp\\jest_runner_tide_2.json" "--testNamePattern" "get areas" "--no-coverage" "--reporters" "default" "--reporters" "c:\\Users\\HaoZh\\.vscode\\extensions\\orta.vscode-jest-4.4.0\\out\\reporter.js" "--colors" "--watchAll=false" "--testPathPattern" "d:\\\\projects\\\\Tide\\\\tide\\\\src\\\\__tests__\\\\APIs\\\\TideAPI\\.test\\.ts"

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In D:\projects\Tide\tide
  25 files checked.
  testMatch: D:/projects/Tide/tide/src/**/*.(test).{js,jsx,ts,tsx}, D:/projects/Tide/tide/src/**\?(*.)(spec|test).{js,jsx,ts,tsx}, D:/projects/Tide/tide/src/**/__tests__/**/*.{js,jsx,ts,tsx}, D:/projects/Tide/tide/src/**/*.{spec,test}.{js,jsx,ts,tsx} - 3 matches
  testPathIgnorePatterns: \\node_modules\\ - 25 matches
  testRegex:  - 0 matches
Pattern: d:\\\\projects\\\\Tide\\\\tide\\\\src\\\\__tests__\\\\APIs\\\\TideAPI\\.test\\.ts - 0 matches

But successfully to runreact-app-rewired test.

Env:

  1. react-scripts 5.0.1
  2. customize-cra 1.0.0
  3. react-app-rewired 2.2.1
  4. pnpm 7.0.0-rc.8

@connectdotz
Copy link
Collaborator

@AlMiraiABC are you using npm to start the test? In your output the testPathPattern is already being double escaped. I added a new workaround (3) in comments to by-pass npm and invoke react-app-rewired test directly exactly as you experienced. Hopefully one of the suggestions can help.

Also opened a new issue in npm npm/cli#5004 to track this external issue.

@Titsnium
Copy link

Titsnium commented Jul 2, 2022

Has it been resolved yet? I've run into the same problem

@mzietlow
Copy link

mzietlow commented Aug 12, 2022

My issue

I just upgraded to npm version 8.11.0 in the context of sharepoint SPFx.
Which caused vscode-jest to stop working, throwing:
TypeError: Cannot read properties of undefined (reading 'testEnvironmentOptions') at new JSDOMEnvironment (node_modules/jest-environment-jsdom/build/index.js:73:28)

The Journey

This sent me on a journey, trying out all sorts of different jest version. Finally, I tried to set "jest.jestCommandLine": "npm run jest" which caused vscode-jest to not find any tests anymore. This has (fortunately) sent me here

The solution

Finally, connectdotz reply, #838 (comment), led me to set `"jest.jestCommandLine": "node_modules\.bin\jest", solving all my problems.

As a side note, I used the following package versions:

...
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
"@types/jest": "^28.1.6", 
"@jest/types": "^28.1.3",
"babel-jest": "^28.1.3",
...

@WillisShek
Copy link

With cross-env, the double backslash doesn't work.
For example,
cross-env NODE_ENV=test jest --testPathPattern c:\\folder\\test won't work, while
cross-env NODE_ENV=test jest --testPathPattern c:\\\folder\\\test will work.
This is probably the responsibility of cross-env but since cross-env is not in active development so we may have to find some other alternative.

@connectdotz
Copy link
Collaborator

for people who had problems with testPathPattern, i.e. npm miss-interpreting the pass-through arguments: it seems npm @>=8.15 has fixed this issue. 🎉

@Dream4ever
Copy link

With cross-env, the double backslash doesn't work. For example, cross-env NODE_ENV=test jest --testPathPattern c:\\folder\\test won't work, while cross-env NODE_ENV=test jest --testPathPattern c:\\\folder\\\test will work. This is probably the responsibility of cross-env but since cross-env is not in active development so we may have to find some other alternative.

I faced the same issue as you did, but once I removed "cross-env" from the test command, all tests could be executed separately without any errors.

@doppelgunner
Copy link

for people who had problems with testPathPattern, i.e. npm miss-interpreting the pass-through arguments: it seems npm @>=8.15 has fixed this issue. 🎉

This one worked for me thanks: npm install -g npm@latest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external-issue issues caused by external system ( (no fix needed)
Projects
None yet
Development

No branches or pull requests

10 participants