vscode-jest: Test suite failed to run in VSCode 1.22.2
Environment
node -v
: 9.11.1npm -v
: 5.6.0npm ls react-scripts
(if you haven’t ejected): no- Operating system: Microsoft Windows [Version 10.0.14393]
VSCode
: version 1.22.2
Steps to Reproduce
I was able to reproduce error with this repo https://github.com/karb0f0s/vscode-jest-test/ on Windows. Although this error doesn’t appear on MacOS. I guess it is somehow related to the way VSCode spawns jest
process.
package.json
{
"name": "app",
"private": true,
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"build": "webpack",
"watch": "webpack --watch"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.46",
"@babel/core": "^7.0.0-beta.46",
"@babel/preset-env": "^7.0.0-beta.46",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^22.4.3",
"babel-loader": "^8.0.0-beta.2",
"jest": "^22.4.3",
"webpack": "^4.6.0",
"webpack-cli": "^2.0.15"
},
"jest": {
"verbose": true,
"bail": true,
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)$",
"transform": {
"^.+\\.js?$": "babel-jest"
}
}
}
.babelrc
{
"presets": [
["@babel/preset-env", {
"targets": {
"browsers": ["last 2 versions", "ie >= 10"]
},
"modules": false,
"useBuiltIns": "usage",
"debug": true
}]
],
"env": {
"test": {
"presets": ["@babel/preset-env"]
}
}
}
Expected Behavior
npm test
works as expected, Debug
under the test
in source file works as expected, Debug configuration
vscode-jest-tests
works as expected, with transpilation and all
Actual Behavior
VSCode Jest plugin fails to run. Output contains:
FAIL test\countPrice.test.js
● Test suite failed to run
.\app\test\countPrice.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { getBladeCount, calculateCpuPrice, calculateRamPrice } from "../src/countPrice";
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:316:17)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.23s
Ran all test suites related to changed files.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 8
- Comments: 40 (9 by maintainers)
Commits related to this issue
- Fix Jest errors in VS Code by setting jest.pathToJest https://github.com/jest-community/vscode-jest/issues/313 — committed to shawnaxsom/dotfiles by shawnaxsom 6 years ago
- fix jest extension temporary, see https://github.com/jest-community/vscode-jest/issues/313 — committed to Alexendoo/hpack-explorer by Alexendoo 6 years ago
- Fix jest on VSCode https://github.com/jest-community/vscode-jest/issues/313#issuecomment-394592856 — committed to davps/tic-tac-toe by davps 6 years ago
- Merge pull request #332 from seanpoulter/feature/#313/Fix-regression-in-support-for-react-scripts Fix regression detecting create-react-app — committed to jest-community/vscode-jest by orta 6 years ago
- Merge pull request #332 from seanpoulter/feature/#313/Fix-regression-in-support-for-react-scripts Fix regression detecting create-react-app — committed to legend1202/vscode-jest by orta 6 years ago
Based on the changes from #324 it looks like you can work around this issue by adding a
"jest.pathToJest": "npm test --"
setting in.vscode/settings.json
. The PR closed a few issues that I haven’t had time to digest so I’m not keen to revert the breaking change just yet. If anyone’s got the bandwidth, I’d love some help with a fix or help building a list of what requirements were requested in those closed issues to guide some much-needed unit and integration tests.@seanpoulter to reproduce:
create-react-app my_app
npm test
in a terminal and the tests will passsrc/App.test.js
file and VSCode will show the following error:I don’t know if this is related or deserves a new issue, but I’m getting errors each time I open VS Code on a CRA app, now. The extension works, given the
pathToJest
workaround above, but this error is displayed:In case it isn’t obvious, this also works with yarn:
🎉
Thanks…I can confirm that fix works on two different projects including a fresh out of the box CRA app.
I had JEST extension installed. After uninstalling problem gone)
@MikeSouza solution (changing babel config) worked for me. Using babel 7 and babel-jest…
I’ll third what @robowen5mac & @kylehalleman have said, using Create-React-App (not ejected) and updated to latest VSCode today only to find that jest runner now flagging lots of code as a problem and won’t run the tests. Tests run if done via the command line
@maciej-gurban and @lhz516
I was having the same issue with the latest version, and configuring the
jest.pathToJest
tonpm test --
did not resolve the problem for me either. However, I did figure out a solution…Are you by chance using Babel with a file-relative config (
.babelrc
) located at the root of your workspace/ project?I solved the issue by converting my
.babelrc
(JSON5) to ababel.config.js
(JS) project-wide configuration.NOTE: I am using Babel 7, and when converting to a
babel.config.js
, I had to turn on caching to get builds and tests working again.The same issue persists for me in the latest version. Tried with
"jest.pathToJest": "npm test --"
. Tests run correctly in the terminal thoughThanks for that. And about the issues, i fixed using the
"jest.pathToJest": "npm test --"
mentioned above.From https://docs.npmjs.com/cli/run-script:
Thanks for the follow up. This is a CRA based application, so there is no specific jest configuration other than what CRA gives you by default. I will look into this, but did something change recently in either VSCode or the the extension as this was working a few days ago (I had not updated VSCode for a bit, so am not sure what version I had updated from). The project itself has not changed.
Is there any update on this issue? I am seeing what looks like the same behaviour for VSCode 1.23.1. Essentially, all of my tests fail to run with some variant of the error “Unexpected token …”. As noted above, I can successfully debug the tests and run them via the command line. I have noticed this in multiple project and the problem persists after uninstalling and reinstalling the VSCode extension. Happy to provide more information, but am not sure what is useful.