vscode-jest: Test suite failed to run in VSCode 1.22.2

Environment

  1. node -v: 9.11.1
  2. npm -v: 5.6.0
  3. npm ls react-scripts (if you haven’t ejected): no
  4. Operating system: Microsoft Windows [Version 10.0.14393]
  5. 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

Most upvoted comments

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 a new app: create-react-app my_app
  • Run npm test in a terminal and the tests will pass
  • Navigate to the src/App.test.js file and VSCode will show the following error:
 FAIL  src/App.test.js

  ● Test suite failed to run

    /Users/jorge/Code/tester/src/App.test.js: Unexpected token (7:18)
         5 | it('renders without crashing', () => {
         6 |   const div = document.createElement('div');
      >  7 |   ReactDOM.render(<App />, div);
           |                   ^
         8 |   ReactDOM.unmountComponentAtNode(div);
         9 | });
        10 | 


Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.973s
Ran all test suites.

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:

This extension relies on Jest 20+ features, it will continue to work, but some features may not work correctly.

In case it isn’t obvious, this also works with yarn:

{
  "jest.pathToJest": "yarn test --"
}

🎉

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…

  1. added {“jest.pathToJest”: “yarn test --”} in .vscode/settings.json
{
  "jest.pathToJest": "yarn test --"
}
  1. Converted .babelrc to babel.config.js with api.cache(true)
module.exports = function babelConfig(api){
  api.cache(true)
  return{
    'presets': [
      ['@babel/preset-env', {
        'modules': false
      }],
      '@babel/preset-react'
    ],
    'plugins': [
      '@babel/plugin-proposal-class-properties'
    ],
    'env': {
      'test': {
        'presets': [
          '@babel/preset-env',
          '@babel/preset-react'
        ]
      },
  
    }
  }}

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 to npm 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 a babel.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 though

Thanks for that. And about the issues, i fixed using the "jest.pathToJest": "npm test --" mentioned above.

From https://docs.npmjs.com/cli/run-script:

As of npm@2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script:

npm run test -- --grep="pattern"

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.