2017-09-27 192 views
0

我創建了一個版本爲1.4.0的create-react-app的React應用程序,並在IntelliJ中打開了生成的項目。現在我試圖在IntelliJ中運行生成的測試。當我這樣做時,我得到以下輸出:在IntelliJ IDEA中運行Jest測試

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

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" 
    } 
} 

回答

1

開玩笑運行配置選擇:

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

感謝您的答案。我今天正在做別的事情。希望下週我會回來。我會盡可能地發送upvotes/accept,因此請耐心等待;-) –

+0

當我選擇react-scripts作爲Jest包時,我得到錯誤ReferenceError:document is not defined。 –

+0

這兩個選項奏效! –

相關問題