Skip to content

SyntaxError: Cannot use import statement outside a module #317

@Mikilll94

Description

@Mikilll94

I have the following bug when running jest unit tests.

 FAIL  src/__tests__/App.spec.js
  ● 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:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • 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:

    /Users/xyz/Desktop/vue-webpack-test/src/App.vue:8
    import HelloComponent from './components/HelloComponent.vue';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { mount } from '@vue/test-utils';
    > 2 | import App from '../App';
        | ^
      3 |
      4 | test('App has a .center-content class', () => {
      5 |     const app = mount(App);

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (src/__tests__/App.spec.js:2:1)

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

Here is the repro project:
vue-webpack-test.zip

To run unit tests in the project just execute:

npm install
npm run test

As you can see in the jest.config.js I am using vue-jest transform.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions