Upon running yarn test
, an unexpected token error is encountered:
Jest encountered an unexpected token
This typically indicates that Jest is unable to parse the file being imported, suggesting it's not standard JavaScript.
By default, Jest will use a Babel config to transform files and ignore "node_modules".
Here are some possible fixes:
• If using ECMAScript Modules, refer to https://jestjs.io/docs/en/ecmascript-modules for enabling.
• To transform specific "node_modules" files, specify a custom "transformIgnorePatterns" in your config.
• For custom transformations, define a "transform" option in your config.
• To mock non-JS modules (e.g. binary assets), utilize the "moduleNameMapper" config option.
For more detailed instructions and examples regarding config options, visit:
https://jestjs.io/docs/en/configuration.html
Details:
/home/marouane/projects/manage-landing-page/jest.setup.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "@testing-library/jest-dom/extend-expect";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
After research, it was found that installing jest-svg-transformer
is required.
The necessary lines were added to the jest.config.js
file:
module.exports = {
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
transform: {
'^.+\\.jsx?$': 'babel-jest',
'^.+\\.svg$': 'jest-svg-transformer',
},
};
Despite these efforts, the issue persisted even after trying other solutions available online.
Assistance would be greatly appreciated! Thank you!
edit:
It appears that replacing jsx
with tsx
in the jest.config.js
file resolved the problem... A simple oversight indeed! Oh well!!!