首页 > 解决方案 > 如何在 PhpStorm 中为 React 应用程序配置 Jest(create-react-app 风格)

问题描述

我正在尝试从 PhpStorm IDE 中运行测试。但是 IDE 抛出错误并且不接受我的配置为有效。虽然我将其配置为官方 PhpStorm 文档解释了它。

我有标准的 CRA 申请。我想使用 Enzyme 和 Jest 作为测试工具。

我使用jest-enzymejest-environment-enzyme包以及酶适配器反应 16

PhpStorm 文档说:

指定jestreact-scriptsreact-script-tsreact-super-scriptsreact-awesome-scripts包的位置。

我试图同时指定jestreact-scripts。并且这两个选项在 PhpStorm 中都不起作用。

这是我的两个配置:

No.1:开玩笑

IDE 抛出 SyntaxError 是因为它无法识别 React 组件。我认为它不知道如何使用 Babel 或 Webpack。但是 IDE 文档没有提到如何将 IDE 指向 Babel 或其他编译器。

在此处输入图像描述

No.2:反应脚本

IDE 抛出argv.config.match is not a function。它甚至无法开始测试。

在此处输入图像描述

jest-environment-enzyme要求我的jest.config.js如下:

module.exports = {
  "setupFilesAfterEnv": ["jest-enzyme"],
  "testEnvironment": "enzyme",
  "verbose": true,
};

UPD1:

我的setupTests.js文件:

// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'jest-enzyme';

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

标签: reactjstestingjestjsphpstorm

解决方案


推荐阅读