I recently encountered an issue while using jest to run tests on a library. After upgrading the library to export to es6, I started receiving the error message "Cannot use import statement outside a module," even though the tests were working fine before the upgrade.
Here is a screenshot of the error: https://i.sstatic.net/EeHZn.png
Below are the configuration files I'm using:
jest.config.js:
module.exports = {
roots: ['<rootDir>/src/'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.ts?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};
tsconfig.json:
{
"compilerOptions": {
... (omitted for brevity)
}
}
babel.config.js:
module.exports = {
presets: [
... (omitted for brevity)
],
plugins: [
... (omitted for brevity)
],
env: {
development: {},
production: {
... (omitted for brevity)
},
test: {
sourceMaps: 'both',
},
},
};
Despite trying various solutions found online, none of them have resolved the issue. If anyone has a solution or suggestions, it would be greatly appreciated. Thank you in advance!