I’m trying to make some testing for a react-native project but jest is encountering this error and I don’t know why:
● 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/j/Downloads/tdd-expo/node_modules/react-redux/dist/react-redux.legacy-esm.js:34
import * as React2 from "react";
^^^^^^
SyntaxError: Cannot use import statement outside a module
12 | import { useEffect } from "react";
13 | import { View } from "react-native";
> 14 | import { Provider } from "react-redux";
| ^
15 |
16 | jest.mock("@/app/(tabs)", () => jest.fn());
17 | jest.mock("@/app/(tabs)/weather", () => jest.fn());
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
at Object.require (__tests__/_layout.test.tsx:14:1)
Here is the configuration for jest:
// package.json
{
// ...
"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
],
"collectCoverage": true
},
// ...
}
And here is the test utils I’m using:
// test-utils.tsx
import { DarkTheme, ThemeProvider } from "@react-navigation/native";
import "@testing-library/jest-native/extend-expect";
import { render } from "@testing-library/react-native";
import React, { PropsWithChildren } from "react";
import { Provider } from "react-redux";
const AllTheProviders = ({ children }: PropsWithChildren) => {
return (
<Provider store={{} as any}>
<ThemeProvider value={DarkTheme}>{children}</ThemeProvider>
</Provider>
);
};
const customRender = (ui: React.ReactElement, options: any) =>
render(ui, { wrapper: AllTheProviders, ...options });
// re-export everything
export * from "@testing-library/react-native";
// override render method
export { customRender as render };
I followed this guide:
https://docs.expo.dev/develop/unit-testing/
And tried this custom render setup:
https://testing-library.com/docs/react-testing-library/setup/#custom-render
I expected jest to run successfully but it actually resulted in a “import” error.
Any idea what’s happening? Thanks in advance ????
reactive is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.