I'm trying to make some testing for a react-native project but jest is encountering this error and I don't know why:
FAIL __tests__/(tabs)/weather.test.tsx
● 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
3 | import { render } from "@testing-library/react-native";
4 | import React, { PropsWithChildren } from "react";
> 5 | import { Provider } from "react-redux";
| ^
6 |
7 | const AllTheProviders = ({ children }: PropsWithChildren) => {
8 | return (
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
at Object.require (test-utils.tsx:5:1)
at Object.require (/Users/j/Downloads/tdd-expo/__tests__/(tabs)../../../../weather.test.tsx:2:1)
This is what it seems to be failing:
Details:
/Users/j/Downloads/tdd-expo/node_modules/react-redux/dist/react-redux.legacy-esm.js:34
import * as React2 from "react";
^^^^^^
And hence, is failing to import it in the test-utils.tsx
file:
// 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"; // Is failing to import here
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 };
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
},
// ...
}
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 😊