Currently, I am facing a challenge while testing the code using jest. The issue seems to be related to babel, especially regarding the export of a default class. Here is an example of the code causing the problem...
export default class Test {
get() {
return {}
}
}
The test setup looks like this...
import Test from './test'
describe('test', () => {
it('should', () => {
// [...]
});
});
However, when running the test, I encounter the following error...
node_modules/@babel/runtime-corejs2/helpers/esm/classCallCheck.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){export default function _classCallCheck(instance, Constructor) { ^^^^^^
SyntaxError: Unexpected token export
This project is a vue web app with the following configuration...
// babel.config.js
module.exports = {
presets: ['@vue/app', '@babel/env']
};
// jest.config.js
module.exports = {
collectCoverage: true,
collectCoverageFrom: [
'src/**'
],
coverageDirectory: '.coverage',
moduleFileExtensions: [
'js',
'json',
'vue'
],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.js$': 'babel-jest'
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
testMatch: [
'<rootDir>/src/**/*.spec.js'
],
transformIgnorePatterns: ['<rootDir>/node_modules/']
};
Lastly, here is my test script in the package.json file...
// package.json
[...]
"test": "jest"
[...]
I am struggling to find a solution to this problem, especially since all my .vue files and tests are working correctly. The issue arises only with specific .js files that use the mentioned syntax. Any suggestions on how to resolve this?
Your input would be greatly appreciated.