配置酶 3.x 适配器

Cod*_*ice 3 javascript testing enzyme

我正在使用 Jest 和 Enzyme 为 React 应用程序编写测试。Enzyme 3.x 引入了适配器以提供不同版本的 React 之间的兼容性。安装文档给出了如何设置的示例:

// setup file
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });
Run Code Online (Sandbox Code Playgroud)

评论中提到的这个“安装文件”是什么?这是一个可以在所有测试之前运行的文件吗?这个文件在 React 项目中的什么位置?它需要一个特定的名称吗?

AKX*_*AKX 5

它不需要特定的名称,是的,它在任何测试之前运行。

你把它挂在你package.json的 sjest节中。

这是我正在做的一个项目的一个例子。

  "jest": {
    // other stuff...
    "setupFiles": [
      "./js/jest-setup.js"
    ],
    // ....
   }
Run Code Online (Sandbox Code Playgroud)

实际js/jest-setup.js文件看起来像这样(即像你的例子)。

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({adapter: new Adapter()});
Run Code Online (Sandbox Code Playgroud)