繁体   English   中英

在 IntelliJ IDEA 中运行 Jest 测试

[英]Run Jest test in IntelliJ IDEA

我使用create-react-app版本 1.4.0 创建了一个 React 应用程序,并在 IntelliJ 中打开了生成的项目。 现在我也尝试在 IntelliJ 中运行生成的测试。 当我这样做时,我得到以下输出:

/usr/bin/node /home/l/src/hello-react/node_modules/jest/bin/jest --config {\"rootDir\":\"/home/l/src/hello-react\",\"transformIgnorePatterns\":[\"/node_modules/\",\"^/home/l/bin/idea-IU-172.3544.35/plugins/JavaScriptLanguage/helpers\"],\"unmockedModulePathPatterns\":[\"^/home/l/bin/idea-IU-172.3544.35/plugins/JavaScriptLanguage/helpers\"]} --colors --setupTestFrameworkScriptFile /home/l/bin/idea-IU-172.3544.35/plugins/JavaScriptLanguage/helpers/jest-intellij/lib/jest-intellij-jasmine.js --testPathPattern ^/home/l/src/hello\-react/src/App\.test\.js$ --testNamePattern "^(test )?renders without crashing$"
 FAIL  src/App.test.js
  ● Test suite failed to run

    /home/l/src/hello-react/src/App.test.js: Unexpected token (7:18)
        5 | it('renders without crashing', () => {
        6 |   const div = document.createElement('div');
      > 7 |   ReactDOM.render(<App />, div);
          |                   ^
        8 | });
        9 | 

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.73s
Ran all test suites matching /^/home/l/src/hello\-react/src/App\.test\.js$/ with tests matching "^(test )?renders without crashing$".

Process finished with exit code 1
Empty test suite.

我只运行生成的代码。 我没有修改它或编写任何我自己的代码。 我需要做什么才能在 IntelliJ IDEA 中运行此测试?

作为参考,以下是重要文件:

App.test.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
});

应用程序.js

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

包.json

{
  "name": "hello-react",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-scripts": "1.0.13"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

在 Jest Run 配置中选择:

jest-package: react-scripts
jest options: --env=jsdom 

PS不要使用 /src 作为您的工作目录,如下图所示

在此处输入图片说明

暂无
暂无

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

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