Jest: cannot use import statement outside a module inside node_modules

108 Views Asked by At

I have a Vue 3 + webpack 5 project working fine in development and build but I am facing one problem with jest.

There is a component that imports a library which is in node_modules:

<script lang="ts">
import { notification } from '@farm-investimentos/front-mfe-libs-ts/helpers/notify'; 

There is an index.js that imports other files, using import syntax:

import notification from './notificationService';

My jest.config.js is as follows:

module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'jsdom',
    setupFiles: ['<rootDir>/tests/unit/index.ts'],
    modulePaths: ['<rootDir>', 'node_modules'],
    moduleFileExtensions: ['js', 'ts', 'json', 'vue'],
    transform: {
        '^.+\\.vue$': '@vue/vue3-jest',
        '^.+\\.js$': 'babel-jest',
        '^.+\\.ts$': 'ts-jest',
    },
    moduleNameMapper: {
        '^@/(.*)$': '<rootDir>/src/$1',
    },
    testEnvironmentOptions: {
        customExportConditions: ['node', 'node-addons'],
    },
};

and jest runner is throwing the error:

/.../node_modules/@farm-investimentos/front-mfe-libs-ts/helpers/notify/index.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,jest){import notification from './notification'; ^^^^^^ SyntaxError: Cannot use import statement outside a module

As I have mentioned, during webpack serve or build, everything is working fine. I have gooled a lot but I have not fully understand why jest does not work.

Anyone has any idea?

0

There are 0 best solutions below