使用CRA为应用程序配置玩笑和酶

问题描述 投票:0回答:1

我已经使用CRA构建了一个样例react应用程序,我正在尝试为该应用程序编写单元测试。

使用笑话,酶,我已经安装了必需的软件包作为dev依赖项

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

jest.config.js-定位src文件夹的外部

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

setupTest.js-定位src文件夹根]的inside

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

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

App.test.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();
    });
});

当我尝试执行npm运行测试

时,出现以下错误。
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 | });

感谢您的任何帮助

我已经使用CRA构建了一个样例react应用程序,我正在尝试为该应用程序编写单元测试。使用开玩笑,酶,我已经安装了所需的软件包作为dev依赖项“ ...

reactjs jestjs enzyme
1个回答
0
投票

我认为您的文件名中有错字,您忘了在文件名末尾添加s

© www.soinside.com 2019 - 2024. All rights reserved.