jest-websocket-mock
jest-websocket-mock copied to clipboard
Jest encountered an unexpected token: jest-websocket-mock.cjs.js
I have installed this package and tried to import the module (as per the examples) in my test file, however, when trying to run the tests, I get the following 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:
/<project-path>/node_modules/jest-websocket-mock/lib/jest-websocket-mock.cjs.js:206
const waitDelay = options?.timeout ?? WAIT_DELAY;
^
SyntaxError: Unexpected token '.'
1 | import {useWebSocket} from "../webSocket";
> 2 | import WS from "jest-websocket-mock";
| ^
3 |
I do not use TypeScript in my project and my jest.config.js file looks like this:
module.exports = {
// Your normal jest config settings
testPathIgnorePatterns: ["frontend/src/cypress/"],
setupFiles: ["./frontend/src/setupTests.js"],
moduleNameMapper: {
"\\.(css|less|scss)$": "<rootDir>/frontend/src/empty-module.js",
},
};
It is worth noting that I use the ?. operator in other parts of my project and it works flawlessly.
I tried adding TypeScript support to both my babel and jest configs but this didn't make a difference.
Any ideas what is causing this issue?
Hi @slatifi !
Optional chaining was added in node 14 - are you able to upgrade your project to node 14 or later? (The previous version, 12, will reach end of life next month)
Hi @romgain, my node version is currently v14 and this issue is still persisting.
Switching to node 14.19.1 (from 12.20.1) solved for me (after I remembered I had to change the node version in the same terminal I was running the tests in).
@slatifi sorry for the delay. Can you confirm this is still happening for you?