0.57更新后,带有Typescript和Jest的React Native被破坏了:找不到预设的"module:metro-react-native-babel-preset".相对于目录 [英] React Native with Typescript and Jest is broken after 0.57 Update: Couldn't find preset "module:metro-react-native-babel-preset" relative to directory

查看:156
本文介绍了0.57更新后,带有Typescript和Jest的React Native被破坏了:找不到预设的"module:metro-react-native-babel-preset".相对于目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在新的React版本0.57和TypeScript中将测试与Jest和Enzyme集成在一起,则它们将无法工作.复制步骤如下:

If you integrate test with Jest and Enzyme in the new React Version 0.57 and TypeScript, they won't work. Here are the steps to reproduce:

创建一个新的React Native项目:

Create a new React Native project:

react-native init MyApp -package "com.my.app" --template typescript && node MyApp/setup.js

安装所有与Jest和Enzyne相关的软件包:

Install all Jest and Enzyne related packages:

npm install --save-dev react-dom enzyme enzyme-react-adapter-16 jest-fetch-mock ts-jest

添加玩笑的配置:

"jest": {
  "preset": "react-native",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js"
  ],
  "transform": {
    "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
    "\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  },
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
  "testPathIgnorePatterns": [
    "\\.snap$",
    "<rootDir>/node_modules/"
  ],
  "cacheDirectory": ".jest/cache",
  "setupFiles": [
    "./tests/setup.js"
  ]
}

添加文件tests/setup.js并包括以下配置:

Add a file tests/setup.js and include the following configuration:

import Enzyme from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import { NativeModules } from "react-native";

global.fetch = require("jest-fetch-mock"); // eslint-disable-line no-undef
jest.mock("react-native-config");
Enzyme.configure({ adapter: new Adapter() });

最后但并非最不重要的一点是,添加一个基本测试(App.test.tsx)来检查Jest和Enzyme是否起作用:

Last but not least add a basic test (App.test.tsx) to check if Jest and Enzyme work:

import React from "react";
import { shallow } from "enzyme";
import { View } from "react-native";
import App from "./App";

const createTestProps = props => ({
  ...props
});

describe("App", () => {
  describe("rendering", () => {
    let wrapper;
    let props;
    beforeEach(() => {
      props = createTestProps({});
      wrapper = shallow(<App {...props} />);
    });

    it("should render a <View />", () => {
      expect(wrapper.find(View)).toHaveLength(1);
    });
  });
});

如果您现在尝试运行测试,则收到的错误消息是:

If you now try to run the test, the error message you get is:

 FAIL  app/App.test.tsx
  ● Test suite failed to run

    Couldn't find preset "module:metro-react-native-babel-preset" relative to directory "<Directory"

编辑

这似乎与Babel有关.

Edit

It seems like this has something to do with Babel.

推荐答案

我在本期中找到了答案:

I found the answer here in this issue: https://github.com/facebook/metro/issues/242#issuecomment-421139247

基本上,将其添加到package.json中的笑话"部分:

Basically, add this to your Jest section in package.json:

"transform": { "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js" }

这篇关于0.57更新后,带有Typescript和Jest的React Native被破坏了:找不到预设的"module:metro-react-native-babel-preset".相对于目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆