Jest(react testing tool) error - Parse Error: Line 1: Illegal import declaration

Jest(react testing tool) error - Parse Error: Line 1: Illegal import declaration

我现在用的是node.js(0.10.x)版本,jest(0.4.x)版本来测试react.js.

在我决定测试我的反应组件之前,我使用了 node.js 0.12.x 版本。通过nvm改成0.10.x.

我重建了所有模块并重新开始测试。

错误如下。

Using Jest CLI v0.4.17
 PASS  __tests__/unit/app.test.js (0.058s)
 PASS  __tests__/unit/preprocessor.js (0.68s)
 FAIL  __tests__/unit/mypage.test.js
Error: /Users/nuko/WebstormProjects/pliky/__tests__/unit/mypage.test.js: /Users/nuko/WebstormProjects/pliky/__tests__/unit/preprocessor.js: Parse Error: Line 1: Illegal import declaration
  at throwError (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/node_modules/jstransform/node_modules/esprima-fb/esprima.js:2813:21)
  at throwErrorTolerant (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/node_modules/jstransform/node_modules/esprima-fb/esprima.js:2825:24)
  at parseSourceElement (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/node_modules/jstransform/node_modules/esprima-fb/esprima.js:6390:17)
  at parseProgramElement (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/node_modules/jstransform/node_modules/esprima-fb/esprima.js:6446:16)
  at parseProgramElements (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/node_modules/jstransform/node_modules/esprima-fb/esprima.js:6478:29)
  at parseProgram (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/node_modules/jstransform/node_modules/esprima-fb/esprima.js:6491:16)
  at Object.parse (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/node_modules/jstransform/node_modules/esprima-fb/esprima.js:7653:23)
  at getAstForSource (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/node_modules/jstransform/src/jstransform.js:251:21)
  at transform (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/node_modules/jstransform/src/jstransform.js:274:11)
  at innerTransform (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/main.js:94:10)
  at Object.module.exports.transform (/Users/nuko/WebstormProjects/pliky/node_modules/react-tools/main.js:20:18)
  at Object.module.exports.process (/Users/nuko/WebstormProjects/pliky/__tests__/unit/preprocessor.js:5:27)
  at Object.readAndPreprocessFileContent (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/src/lib/utils.js:432:33)
  at Loader._execModule (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:209:11)
  at Loader.requireModule (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:914:12)
  at Loader.requireModuleOrMock (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:935:17)
  at /Users/nuko/WebstormProjects/pliky/__tests__/unit/mypage.test.js:4:14
  at Object.runContentWithLocalBindings (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/src/lib/utils.js:485:17)
  at Loader._execModule (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:245:9)
  at Loader.requireModule (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:914:12)
  at jasmineTestRunner (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/src/jasmineTestRunner/jasmineTestRunner.js:292:16)
  at /Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/src/TestRunner.js:376:12
  at tryCatcher (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/node_modules/bluebird/js/main/util.js:26:23)
  at Promise._settlePromiseFromHandler (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/node_modules/bluebird/js/main/promise.js:503:31)
  at Promise._settlePromiseAt (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/node_modules/bluebird/js/main/promise.js:577:18)
  at Promise._settlePromises (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/node_modules/bluebird/js/main/promise.js:693:14)
  at Async._drainQueue (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/node_modules/bluebird/js/main/async.js:123:16)
  at Async._drainQueues (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/node_modules/bluebird/js/main/async.js:133:10)
  at Async.drainQueues (/Users/nuko/WebstormProjects/pliky/node_modules/jest-cli/node_modules/bluebird/js/main/async.js:15:14)
  at process._tickCallback (node.js:448:13)

1 test failed, 2 tests passed (3 total)
Run time: 2.189s
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

我的preprocessor.js代码。(和官方文档脚本一样)

    var ReactTools = require('react-tools');

    module.exports = {
        process: function(src) {
            return ReactTools.transform(src);
        }
    };

我的 mypage.test.js 代码。

jest.dontMock('../../React/mypage.js');

var React = require('react/addons'),
    Mypage = require('../../React/mypage.js'),
    TestUtils = React.addons.TestUtils;

describe('mypage', function() {

    var MypageElement = TestUtils.renderIntoDocument(<Mypage />);

    var list = TestUtils.scryRenderedDOMComponentsWithTag(MypageElement, 'option');


    it('has 3 default items', function() {
        expect(list.props.children.length).toEqual(5);
    });
});

我将这部分添加到 package.json 文件中。

"jest": {
    "scriptPreprocessor": "./__tests__/unit/preprocessor.js",
    "unmockedModulePathPatterns": [
      "./node_modules/react"
    ]
  },

这个错误是从哪里来的,我该如何解决这个问题?请告诉我。谢谢。

大概你的 mypage.js 组件的第一行是这样的 import React from 'react' 或类似的东西。这是 ES6 语法,默认情况下 ReactTools 仅与 ES5 代码兼容,等价于 var React = require('react').

要修复此问题,请在转换期间启用和谐模式。

var ReactTools = require('react-tools');

module.exports = {
    process: function(src) {
        return ReactTools.transform(src, { harmony: true });
    }
};

JSTransform documentation here