I'm using jest to run some tests on a lib and I've been getting the error "Cannot use import statement outside a module" after I upgraded a lib that now exports to es6, before the upgrade the tests worked just fine.
The jest.config.js file that I'm using:
module.exports = {
roots: ['<rootDir>/src/'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.ts?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};
The tsconfig.json file:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": ["es6", "dom"],
"types": ["jest"],
"sourceMap": true,
"jsx": "react",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"strict": true,
"emitDecoratorMetadata": true,
"importHelpers": true,
"noEmitHelpers": true,
"noFallthroughCasesInSwitch": true,
"strictFunctionTypes": false,
"pretty": true,
"removeComments": false,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"experimentalDecorators": true,
"baseUrl": "./"
}
}
And lastly, the babel.config.js file:
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
ie: '11',
},
},
],
'@babel/typescript',
],
plugins: [
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-transform-object-assign',
'@babel/plugin-transform-runtime',
],
env: {
development: {},
production: {
plugins: ['transform-dev-warning'],
ignore: ['**/test/'],
},
test: {
sourceMaps: 'both',
},
},
};
I've tried pretty much every solution that I could find online but nothing seems to work. If anybody could help me that would be great, thanks in advance!
