在IntelliJ IDEA中运行Jest测试 [英] Run Jest test in IntelliJ IDEA

查看:279
本文介绍了在IntelliJ IDEA中运行Jest测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a React app with create-react-app version 1.4.0 and opened the resulting project in IntelliJ. Now I am attempting to run the generated test in IntelliJ as well. I get the following output when I do so:

/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中运行此测试,我需要做什么?

I am only running the generated code. I have not modified it or written any of my own code. What do I need to do in order to be able to run this test in IntelliJ IDEA?

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

For reference, here are the important files:

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);
});

App.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;

package.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配置中,选择:

In Jest Run configuration choose:

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

这篇关于在IntelliJ IDEA中运行Jest测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆