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

Test suite failed to run: Jest encountered an unexpected token "import" #6

Open
LevYas opened this issue Nov 20, 2019 · 5 comments
Open

Comments

@LevYas
Copy link

LevYas commented Nov 20, 2019

I faced the same issue in my project and tried this one as a reference. Surprisingly, this reference project produced the same error. I use updated packages in my projects, but error is the same.
So, I cloned this repo, opened it in VS Code, then put in terminal:

npm install
npm run test

Here is the result:

Executing task: npm run test <

vue-test-utils-typescript-example@0.1.0 test d:\Projects\vue-test-utils-typescript-example
npm run test:unit

vue-test-utils-typescript-example@0.1.0 test:unit d:\Projects\vue-test-utils-typescript-example
jest

FAIL src/components/tests/HelloWorld.spec.ts
● Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

D:\Projects\vue-test-utils-typescript-example\src\components\__tests__\HelloWorld.spec.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import 'jest';
                                                                                         ^^^^^^

SyntaxError: Unexpected token import

  at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-cli/node_modules/jest-runtime/build/script_transformer.js:403:17)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.551s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! vue-test-utils-typescript-example@0.1.0 test:unit: jest
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the vue-test-utils-typescript-example@0.1.0 test:unit script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\LEVYAS.TRANSAS\AppData\Roaming\npm-cache_logs\2019-11-20T08_35_07_810Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! vue-test-utils-typescript-example@0.1.0 test: npm run test:unit
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the vue-test-utils-typescript-example@0.1.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

What could be the reason and how to fix it?

P.S. There is PR to this repo, I tried this one too and it doesn't produce that error. I tried to figure out the defining differences all the previous workday, but, unfortunately, with no success.

@LevYas
Copy link
Author

LevYas commented Nov 20, 2019

JS ecosystem is a kind of dark magic...
I updated packages with npm update in this repo today and the HelloWorld test passed without errors.

Ultimately, I found the reason why my project failed. In my package.json I have a "jest" section with moduleFileExtensions setting. The jest documentation says

We recommend placing the extensions most commonly used in your project on the left

so, since I used TS, my moduleFileExtensions looked like this:
"moduleFileExtensions": [ "ts", "vue", "js", "json" ],

and I got error

Test suite failed to run

Jest encountered an unexpected token

...
Details:

D:\Projects\MyAwesomeProject\src\components\TheMap\LodControl.vue:2
import _classCallCheck from "d:\\Projects\\MyAwesomeProject\\node_modules\\@babel\\runtime-corejs2/helpers/esm/classCallCheck";
^^^^^^

SyntaxError: Unexpected token import

When I changed moduleFileExtensions to
"moduleFileExtensions": [ "js", "ts", "json", "vue" ],

the error disappeared.

This issue is still valid, I can make a PR with this update if needed.

...
Oh, no, it still fails on my build-machine...

@LevYas LevYas changed the title Test suite failed to run: Jest encountered an unexpected token Test suite failed to run: Jest encountered an unexpected token "import" Nov 20, 2019
@LevYas
Copy link
Author

LevYas commented Nov 21, 2019

As for issue in my project:
Phew, I was able to reproduce fail on build-machine on my workstation and fix it.
To perform "clean run" and then start the tests I used the command
npm -i --force install && npm run test:unit
I had a working configuration for TS and I wanted to add tests for the .vue components.

I tried a lot of solutions, and minimum worked solution for me is:

  1. npm install -D @babel/core @babel/plugin-transform-modules-commonjs babel-core@^7.0.0-bridge.0 babel-jest (I already had jest, @types/jest, and vue-jest)
  2. add "vue" to jest's "moduleFileExtensions"
  3. add "^.+\\.jsx?$": "babel-jest" to jest's "transform"
  4. add "node_modules/(?!(@babel)/)" to jest's "transformIgnorePatterns"
  5. add this to babel.config.js:
    test: {
      plugins: [
        "@babel/plugin-transform-modules-commonjs",
      ]
    }
  },

Some of my components contained imports for the .css files, so I:

  1. Added styleMock.js with module.exports = {};,
  2. added setting "allowJs": true, in my tsconfig.json,
  3. changed jest preset from "preset": "ts-jest", to "preset": "ts-jest/presets/js-with-ts",\
  4. added "\\.(css|less)$": "<rootDir>/src/__mocks__/styleMock.js" to jest's "moduleNameMapper" section.

Also, I added testSetup.js to initialize vuetify there.
Ultimately, I solved this problem. My jest config looks like this:

    "preset": "ts-jest/presets/js-with-ts",
    "setupFiles": [
      "<rootDir>/src/testSetup.js"
    ],
    "moduleFileExtensions": [
      "js",
      "ts",
      "json",
      "vue"
    ],
    "transform": {
      "^.+\\.ts$": "ts-jest",
      ".*\\.(vue)$": "vue-jest",
      "^.+\\.jsx?$": "babel-jest"
    },
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/src/$1",
      "^vuetify/lib$": "vuetify",
      "\\.(css|less)$": "<rootDir>/src/__mocks__/styleMock.js"
    },
    "reporters": [
      "default",
      "jest-junit"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!(@babel)/)"
    ],
    "testURL": "http://localhost/"
  },

As for this project, just the update is not a solution.
After npm update and npm audit fix I tried npm -i --force install && npm run test:unit and saw an error that babel is not found. I installed babel packages and it worked without additional configuration. I'm not sure is it the right way to solve the problem, that's why I did not make a PR.

@mohsenuss91
Copy link

@LevYas I'm getting this on windows10 (VueJS/Jest):


Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    C:\Users\PC01\Desktop\\Project01\tests\features\Unit.spec.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { shallowMount } from '@vue/test-utils';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1086:14)

@warun-01
Copy link

● Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

/home/coducer/Desktop/projects/alarabia/alarabia-backend/node_modules/axios/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import axios from './lib/axios.js';
                                                                                  ^^^^^^

SyntaxError: Cannot use import statement outside a module

@Ar1stotele
Copy link

Hello, any update on this ticket?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants