I am getting a Jest encountered an unexpected token. I tried a numerous of configuration but was unable to get it to work.
tsconfig.json
{
"extends": "@tsconfig/node18",
"compilerOptions": {
"outDir": "./dist/lib",
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true
},
"exclude": ["cdk.out", "build", "node_modules", "dist"]
}
jest.config.js
module.exports = {
testMatch: ['**/*.test.js'],
preset: 'ts-jest',
testEnvironment: 'node',
};
babel.config.js
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
};
I tried a numerous of different jest.config.js but had no luck
When I do this config, the parsing seems to work but I get a ReferenceError: BWTC is not defined
in another test file.
module.exports = {
testMatch: ['**/*.test.js'],
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\.(ts|tsx)?$': 'ts-jest',
'^.+\.(js|jsx)$': 'babel-jest',
},
transformIgnorePatterns: [],
};
Here are other configs I attempted but failed
module.exports = {
testMatch: ['**/*.test.js'],
preset: 'ts-jest',
testEnvironment: 'node',
transformIgnorePatterns: ['node_modules/(?!(.*\.mjs$|pdfjs-dist))'],
};
module.exports = {
testMatch: ['**/*.test.js'],
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\.(js|ts|tsx)$': 'ts-jest',
},
};
Original error:
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.