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

"Jest encountered an unexpected token" in vscode-jest output, working everywhere else #415

Open
ayan4m1 opened this issue Dec 21, 2018 · 3 comments

Comments

@ayan4m1
Copy link

commented Dec 21, 2018

Environment

  1. node -v: v10.14.2
  2. npm -v: 6.5.0
  3. npm ls jest or npm ls react-scripts (if you haven’t ejected):
vscode-jest-issue@0.1.0 C:\code\vscode-jest-issue
-- jest@23.6.0
  1. your vscode-jest settings if customized:

    • jest.pathToJest? Tried [ "npm run test --", "npm test", "node_modules/.bin/jest", unset ]
    • jest.pathToConfig? Tried [ "jest.config.js", "./jest.config.js", unset ]
    • anything else that you think might be relevant? Only other settings are iconTheme colorTheme tabSize and telemetry disable.
  2. Operating system: Windows 10 Pro 1809 17763.195

Prerequisite

  • are you able to run jest test from command line? yes
  • how do you run your tests from command line? Tried [ "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.

Steps to Reproduce

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.

> vscode-jest-issue@0.1.0 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.

Relevant Debug Info

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.

Expected Behavior

vscode-jest should output passing tests if every other way I can find to run Jest does so.

Actual Behavior

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.

@JDeuce

This comment has been minimized.

Copy link

commented Jan 9, 2019

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

  • npm run test:unit
  • npx jest
  • .\node_modules.bin\jest
  • npx vue-cli-service test:unit

I tested different settings for jest.pathToJest for all of them, but the result is always the same.

@JDeuce

This comment has been minimized.

Copy link

commented Jan 10, 2019

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: vuejs/vue-cli#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.

@ayan4m1

This comment has been minimized.

Copy link
Author

commented Jan 18, 2019

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:

  • Open %TEMP% in Explorer, delete any file or folder matching the glob jest*
  • Open C:\Users\Me\AppData\Local\Temp in Explorer, delete the same
  • In Code, open settings and set "jest.pathToJest": "npm run test --" (I do not have a test:unit script defined)
  • Close VS Code
  • Execute npx jest --clearCache in terminal for good measure (it says it deleted the AppData\Local\Temp\jest directory)
  • Open VS Code
  • See "Jest: Starting watch mode" at bottom of Code window
  • See "Jest: (4 test suites failed)" with the same errors originally documented

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.