简体   繁体   English

使用 create-react-app 为 React 应用配置 Jest 和 Enzyme

[英]Configuring Jest and Enzyme for React app using create-react-app

I have built a sample React application using create-react-app (CRA), I am trying to write the unit testing for the application.我已经使用 create-react-app (CRA) 构建了一个示例 React 应用程序,我正在尝试为该应用程序编写单元测试。

Using Jest and Enzyme, I have installed the required packages as the dev dependencies使用 Jest 和 Enzyme,我已经安装了所需的包作为开发依赖项

    "enzyme": "^3.10.0",
    "enzyme-adapter-react-16": "^1.15.1",
    "jest": "^24.9.0"

jest.config.js - located outside of src folder jest.config.js - 位于src文件夹之外

module.exports = {
    verbose: true,
    setupFilesAfterEnv: ["./src/setupTest.js"],
};

setupTest.js - located inside of src folder root setupTest.js -位于src文件夹根目录中

import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";

configure({ adapter: new Adapter() });

App.test.js应用程序测试.js

import React from "react";
import { shallow } from "enzyme";

import App from "./App";

describe("<App />", () => {
    it("Renders the app component", () => {
        const wrapper = shallow(<App />);
        expect(wrapper).toMatchSnapshot();
    });
});

When I try to do npm run test , it's giving the below error:当我尝试执行npm run test时,出现以下错误:

Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none.
      To configure an adapter, you should call `Enzyme.configure({ adapter: new Adapter() })`
      before using any of Enzyme's top level APIs, where `Adapter` is the adapter
      corresponding to the library currently being tested. For example:

      import Adapter from 'enzyme-adapter-react-15';

      To find out more about this, see http://airbnb.io/enzyme/docs/installation/index.html

   6 | describe("<App />", () => {
   7 |     it("Renders the app component", () => {
>  8 |         const wrapper = shallow(<App />);
     |                         ^
   9 |         expect(wrapper).toMatchSnapshot();
  10 |     });
  11 | });

You must name your setupFilesAfterEnv file as src/setupTests.js or src/setupTests.ts .您必须将setupFilesAfterEnv文件命名为src/setupTests.jssrc/setupTests.ts This cannot be configured using without ejecting from create-react-app.如果不从 create-react-app 中弹出,就无法配置它。

Here is a functional setup file for CRA:这是 CRA 的功能设置文件:

src/setupTests.js

(note that it's setupTests and not setupTest in the file name) (注意文件名中是setupTests而不是setupTest

import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";

configure({ adapter: new Adapter() });

More information 更多信息

I think there is typo in your file name you forgot to add s at the end of file name (setupTests), the expected path for your setup file is src/setupTests.js .我认为您的文件名中有错字,您忘记在文件名末尾添加s (setupTests),您的安装文件的预期路径是src/setupTests.js

You can go with this filename also but then you have to include it in the jest config like below.你也可以使用这个文件名,但是你必须像下面这样将它包含在 jest 配置中。

// package.json
{
    ...,
    "jest": {
        "setupFilesAfterEnv": "<rootDir>/test-setup.js"
    }
}

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM