node -v: v10.14.2npm -v: 6.5.0npm ls jest or npm ls react-scripts (if you haven’t ejected):[email protected] C:\code\vscode-jest-issue
-- [email protected]
your vscode-jest settings if customized:
[ "npm run test --", "npm test", "node_modules/.bin/jest", unset ][ "jest.config.js", "./jest.config.js", unset ]iconTheme colorTheme tabSize and telemetry disable.Operating system: Windows 10 Pro 1809 17763.195
[ "npm run test", "npm test", "npx jest", "node_modules/.bin/jest" ] with and without --config jest.config.js all work, test suite passes and coverage is generated.Clone the repro repo.
npm install
npm test
Observe that the test passes. Now open the vscode-jest-issue directory in VS Code with vscode-jest enabled. Observe failure.
> [email protected] test c:\code\vscode-jest-issue
> jest "--json" "--useStderr" "--outputFile" "C:\Users\Me\AppData\Local\Temp/jest_runner.json" "--watchAll" "--no-color" "--config" "jest.config.js"
FAIL src/index.test.js
● Test suite failed to run
Jest encountered an unexpected token
...
Details:
c:\code\vscode-jest-issue\src\index.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { test } from './index';
SyntaxError: Unexpected token {
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.858s
Ran all test suites.
I enabled debug output and opened dev tools. I found that (for one permutation of pathToJest and pathToConfig anyway) the following command was being logged:
npm run test -- --json --useStderr --outputFile C:\Users\Me\AppData\Local\Temp/jest_runner.json --watchAll --no-color --config jest.config.js
This command (and all other commands logged in dev tools) execute correctly (tests pass, no errors) from a terminal.
I could see this being a duplicate of this bug or this bug - apologies for the churn if so, but I'd still like to know definitely if it is the same issue as one of these and if so what we can do to help resolve it.
vscode-jest should output passing tests if every other way I can find to run Jest does so.
There appears to be some disconnect between vscode-jest and babel such that Jest but not vscode-jest is capable of executing tests written in ES6.
Thank you for your time and effort.
I'm having a very similar issue. I can see the command it says it's running in the debug output, and if I run that myself in a terminal it's fine. But when it runs from vscode it doesn't seem to be running babel properly as it starts throwing unexpected token errors just like @ayan4m1's example.
I am using vue 2.5 with jest 23.6 and vscode 1.30.1 with latest extension.
From a terminal I can get a successful jest run with any of the following variants
I tested different settings for jest.pathToJest for all of them, but the result is always the same.
So after restarting everything today, my tests stopped running in console as well from any of the above variants!
I discovered that jest keeps a temporary 'jest' folder which caches the transpilation results! The implication of this is that if vscode-jest runs with the wrong config, then it will cache the wrong results and not be able to recover until the cache is cleared. The converse is also true, if I had a successful run with npm run test:unit then using any of the other variants which were failing before, would appear to work due to using the cache.
There is some more info here: https://github.com/vuejs/vue-cli/issues/1879
You can find the cache in a 'jest' folder here which you can clear by deleting it:
console.log(require('os').tmpdir())
I think you can also use this built in flag to clear it, but I didn't try it:
npx jest --clearCache
So the solution for me was to change my jest.pathToJest to run npm run test:unit -- and then clear the cache! I had to reload vscode after wiping the cache for vscode-jest to restart. Now everything is working great.
Thanks for taking the time to reply @JDeuce - originally, I did try clearing the Jest cache I knew about at %TEMP%, but you mentioned .tmpdir() which is a path under %LocalAppData% on my install. I hadn't had time to try and repro your findings until now. I did the following:
%TEMP% in Explorer, delete any file or folder matching the glob jest*C:\Users\Me\AppData\Local\Temp in Explorer, delete the same"jest.pathToJest": "npm run test --" (I do not have a test:unit script defined)npx jest --clearCache in terminal for good measure (it says it deleted the AppData\Local\Temp\jest directory) In the same directory, npx jest outputs 4/4 passing, as does ./node_modules/.bin/jest. I also skimmed the issue you linked to - again, appreciate the extra info but it doesn't seem to be the same issue I am facing, or at least it isn't resolved the same way.
If I got something wrong, please let me know.
Most helpful comment
Thanks for taking the time to reply @JDeuce - originally, I did try clearing the Jest cache I knew about at
%TEMP%, but you mentioned.tmpdir()which is a path under%LocalAppData%on my install. I hadn't had time to try and repro your findings until now. I did the following:%TEMP%in Explorer, delete any file or folder matching the globjest*C:\Users\Me\AppData\Local\Tempin Explorer, delete the same"jest.pathToJest": "npm run test --"(I do not have a test:unit script defined)npx jest --clearCachein terminal for good measure (it says it deleted theAppData\Local\Temp\jestdirectory)In the same directory,
npx jestoutputs 4/4 passing, as does./node_modules/.bin/jest. I also skimmed the issue you linked to - again, appreciate the extra info but it doesn't seem to be the same issue I am facing, or at least it isn't resolved the same way.If I got something wrong, please let me know.