Vscode-jest: Unexpected token import error when using env with babel

Created on 18 May 2017  ยท  4Comments  ยท  Source: jest-community/vscode-jest

I use the the env setting for babel to define the babel config for my tests. It appears that the transpilation is failing due to the default config in .babelrc not existing. Is it possible for a fix/workaround for this?

My project looks like this:

package.json

{
  "scripts": {
    "test": "cross-env BABEL_ENV=jest jest"
  }
}

.babelrc

{
  "env": {
    "jest": {
      "presets": [
        ["es2015"],
        "react"
      ],
      "sourceMaps": "inline",
      "plugins": [
        "transform-object-rest-spread"
      ]
    },
  }
}

The console output is:

  โ— Test suite failed to run

    /Users/user/dev/proj/src/actions/job.test.js:9
    jest.mock('../api/logger');import faker from 'faker';import * as ActionTypes from './ActionTypes';import { executeProcess } from './job';import mockStore from './mockStore';import processKeywords from '../api/process';
                               ^^^^^^
    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:290:17)
      at handle (node_modules/worker-farm/lib/child/index.js:41:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
      at emitTwo (events.js:106:13)

Most helpful comment

Try setting the jest.pathToJest to be cross-env BABEL_ENV=jest jest or yarn test --. As long as there's a command that works that it can add params to, should be ๐Ÿ‘

All 4 comments

Try setting the jest.pathToJest to be cross-env BABEL_ENV=jest jest or yarn test --. As long as there's a command that works that it can add params to, should be ๐Ÿ‘

That took care of it, thank you for the quick response!

I have tried setting my jest.pathToJest to:

cross-env BABEL_ENV=test vue-cli-service test:unit

But I'm still seeing the same issue as the above user. I'm using the new Vue CLI 3, and running tests from powershell with the recommended command and seeing the results I expect:

vue-cli-service test:unit

But the vscode plugin continues to show the same errors for me as the above user. console.log -ing in my setup shows:

NODE_ENV = test
BABEL_ENV = undefined

Update: After disabling/enabling

_Jest Output_

vue-cli-service test:unit \.unit\.js$ "--json" "--useStderr" "--outputFile" "C:\Users\Danny\AppData\Local\Temp/jest_runner.json"

BABEL_ENV = test ๐Ÿ‘

But... Still seeing the unexpected token import issue ๐Ÿค”

I'm not sure why I see it here, but not when I run npm test myself.

@dannyrb I'm also using Vue CLI 3 and am getting the same error. Did you figure out how to fix it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fillon picture fillon  ยท  3Comments

jpokrzyk picture jpokrzyk  ยท  5Comments

orta picture orta  ยท  4Comments

DaniGTA picture DaniGTA  ยท  3Comments

ryanlittle picture ryanlittle  ยท  3Comments