I'm using ignite
to create an expo react native
app. I'm using this guide https://ignitecookbook.com/docs/recipes/GeneratorComponentTests to create component test files
Here is the first test file
import React from "react"
import { fireEvent, render, screen } from "@testing-library/react-native"
import { HelloWorld } from "../HelloWorld"
describe("HelloWorld", () => {
it("renders", () => {
render(<HelloWorld />)
expect(screen.getByText("Hello")).toBeTruthy()
})
})
I followed https://reactnativetesting.io/component/setup/ in order to install @testing-library/react-native
but now I'm getting
● 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/montylennie/AbutFrontend/app/components/specs/HelloWorld.spec.tsx:12
(0, react_native_1.render)(<HelloWorld_1.HelloWorld />);
^
SyntaxError: Unexpected token '<'
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1518:14)
at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)
Here is my jest.config.js
file
const { defaults: tsjPreset } = require("ts-jest/presets")
/** @type {import('@jest/types').Config.ProjectConfig} */
module.exports = {
...tsjPreset,
preset: "jest-expo",
transformIgnorePatterns: [
"<rootDir>/node_modules/(react-clone-referenced-element|@react-native-community|react-navigation|@react-navigation/.*|@unimodules/.*|native-base|react-native-code-push)",
"jest-runner",
],
testPathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/.maestro/", "@react-native"],
testEnvironment: "jsdom",
setupFiles: ["<rootDir>/test/setup.ts"],
setupFilesAfterEnv: ["<rootDir>/jest-setup-after-env.js"],
}
And here is my contents of jest.config.js
import '@testing-library/jest-native/extend-expect';
What am I missing?
You have a few problems with your jest.config.js
file:
jest-expo
for Expo apps and ts-jest
for TypeScript apps. This can cause conflicts or unexpected behavior,
because they might have different settings or transformations. You
should only use one preset at a time, or use the projects option to
separate different sub-projects.transformIgnorePatterns
option to tell Babel to
ignore some modules. But this option only works for the default
transformer, which is babel-jest
. If you are using a different
transformer, like ts-jest
, you need to use the transformerConfig
option to pass the ignore patterns to it.testEnvironment
option to set jsdom
, which is a
browser-like environment for testing web apps. But you are testing a
React Native app, which is a mobile app that runs on native
platforms. You should use the node environment instead, which is the
default for Jest. Or you can use the react-native
environment, which
is provided by the jest-expo
preset.To fix these issues, you need to update your jest.config.js
file and choose the right options for your project. For example, if you are testing an Expo app with TypeScript, you could use something like this:
module.exports = {
preset: "jest-expo",
transform: {
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest",
},
globals: {
"ts-jest": {
transformerConfig: {
transformIgnorePatterns: [
"<rootDir>/node_modules/(react-clone-referenced-element|@react-native-community|react-navigation|@react-navigation/.*|@unimodules/.*|native-base|react-native-code-push)",
"jest-runner",
],
},
},
},
testPathIgnorePatterns: [
"<rootDir>/node_modules/",
"<rootDir>/.maestro/",
"@react-native",
],
testEnvironment: "react-native",
setupFiles: ["<rootDir>/test/setup.ts"],
setupFilesAfterEnv: ["<rootDir>/jest-setup-after-env.js"],
};