Jest encountered an unexpected token after update
After updating from v 1.1.1 to most recent 2.1.4 I got this error:
Test suite failed to run
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.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/pawel/moje/serverless-hexagonal-template/node_modules/filter-obj/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export default function filterObject(object, predicate) {
^^^^^^
SyntaxError: Unexpected token 'export'
at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1796:14)
at Object.<anonymous> (../node_modules/aws-testing-library/src/common/dynamoDb.ts:1:1)
I've tried many different solutions fount on the internet but nothing seems to work.
My jest version is 28.1.1 and node is 14.
Hi @serverlesspolska, thanks for opening the issue. This is related to Jest trying to transform packages under node_modules and failing since 2.1.4 uses an es6 module.
You can configure Jest not to do so via https://github.com/erezrokah/aws-testing-library-examples/blob/8cbffeb58725baa168a1d10fa8568369146cf3dc/jest.config.js#L2 and https://github.com/erezrokah/aws-testing-library-examples/blob/8cbffeb58725baa168a1d10fa8568369146cf3dc/jest.config.js#L8
Thank you @erezrokah
Unfortunately, that didn't help. I tried very similar setups already. I'm working on updating Serverless Framework template https://github.com/serverlesspolska/serverless-hexagonal-template
My configuration is a little bit more complex as I have 3 config types, each for different test types: unit, integration, and e2e. I use aws-testing-library in e2e tests only.
The original config that works with v1.1.1 is located here https://github.com/serverlesspolska/serverless-hexagonal-template/blob/main/config/e2e.jest.config.js.
Based on your comment, I modified it as follows:
const { config } = require('dotenv');
const { resolve } = require('path');
const esModules = ['aws-testing-library', 'filter-obj'].join('|');
module.exports = {
testEnvironment: 'node',
roots: ['../__tests__/'],
testMatch: ['**/*.(e2e).js'],
testTimeout: 60000 * 7, // 7 minutes timeout
// Enable this if you're using aws-testing-library
setupFilesAfterEnv: ['../node_modules/aws-testing-library/lib/jest/index.js'],
preset: 'ts-jest/presets/js-with-ts',
transformIgnorePatterns: [`../node_modules/(?!${esModules})`],
}
// Load environment variables generated by serverless-export-env plugin
config({
path: resolve(__dirname, '../.awsenv'),
bail: 1
})
And I also installed ts-jest lib. Still got the same error on node 14 and 16.
To be honest, I am little confused, why I needed it since I use pure JS.
@erezrokah could you please take a look at my project and suggest something?
Hi we fixed it by doing the following
- add Babel jest to the project https://jestjs.io/docs/getting-started#using-babel
- add to your jest config
transformIgnorePatterns: [
`node_modules/(?!aws-testing-library|filter-obj)`,
],
transform: {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "ts-jest",
},
I have the same issue. Locally it works with @RaphaelManke 's fix but in GitHub actions, it somehow doesn't. I have no idea atm.
I have my config in my own config file jest.config.ts not in the package.json
Ok the last issue was because I missed the babel config due to a gitignore for JS files 🙄
It "works" now but I need to introduce babel only for this package and it gives me headaches for some other things like the MongoDB preset. Is there any way to get the aws-testing-library running without babel?
Hi all and thanks for the workaround @RaphaelManke. I think my suggestion in https://github.com/erezrokah/aws-testing-library/issues/635#issuecomment-1167686784 does something similar via a preset: https://github.com/erezrokah/aws-testing-library-examples/blob/8cbffeb58725baa168a1d10fa8568369146cf3dc/jest.config.js#L6 https://github.com/erezrokah/aws-testing-library-examples/blob/8cbffeb58725baa168a1d10fa8568369146cf3dc/jest.config.js#L6
This is a jest issue, see related issues in https://github.com/facebook/jest/issues?q=is%3Aissue+Jest+encountered+an+unexpected+token and specifically https://github.com/facebook/jest/issues/9430.
However I'll try to see if there's anything to be done on aws-testing-library side.
It will probably take me a week or so to look into this
hey @erezrokah,
I just updated to "aws-testing-library": "^4.0.4" and have the same issue as half a year ago :-(
Test suite failed to run
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.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/pawel/moje/cloudpouch-backend/node_modules/filter-obj/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export default function filterObject(object, predicate) {
^^^^^^
SyntaxError: Unexpected token 'export'
at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1449:14)
at Object.<anonymous> (../node_modules/aws-testing-library/src/common/dynamoDb.ts:1:1)
at Object.<anonymous> (../node_modules/aws-testing-library/src/jest/dynamoDb.ts:4:1)
at Object.<anonymous> (../node_modules/aws-testing-library/src/jest/index.ts:6:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.911 s
Ran all test suites matching /send/i.
As others said, I don't want to introduce babel just for the sake of this issue.
Any suggestion on how to fix it?
Any update around this?
try to fix in PR #782