У меня есть приложение React Native. Я хочу добавить юнит-тесты с Jest. Когда я запускаю yarn test
, я получаю следующую ошибку:
FAIL ./App.test.tsx
● app › can render snapshot
ReferenceError: React is not defined
5 | describe("app", () => {
6 | it("can render snapshot", () => {
> 7 | const tree = renderer.create(<App />);
| ^
8 | expect(tree).toMatchSnapshot();
9 | });
10 | });
Вот мои App.tsx
:
import React from "react";
import Amplify from "aws-amplify";
import awsmobile from "./src/aws-exports";
import AppContainer from "./src/navigation/index";
Amplify.configure(awsmobile);
const App = () => {
return (
<AppContainer />
);
};
Вот что у меня в App.test.tsx
:
import "react-native";
import React from "react";
import renderer from "react-test-renderer";
import App from "./App";
describe("app", () => {
it("can render snapshot", () => {
const tree = renderer.create(<App />);
expect(tree).toMatchSnapshot();
});
});
И вот мой jest.config.js
:
// jest.config.js
const {defaults: tsjPreset} = require("ts-jest/presets");
module.exports = {
...tsjPreset,
preset: "react-native",
transform: {
...tsjPreset.transform,
"\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
},
globals: {
"ts-jest": {
babelConfig: true,
},
},
// This is the only part which you can keep
// from the above linked tutorial's config:
cacheDirectory: ".jest/cache",
moduleFileExtensions: [
"ios.js",
"native.js",
"js",
"json",
"jsx",
"node",
"ios.ts",
"native.ts",
"ts",
"tsx",
],
};
ОБНОВЛЕНИЕ:
Вот мой babel.config.js
:
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
Я следовал инструкциям здесь .