Jest示例不起作用

Jest示例不起作用

本文介绍了React Jest示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试运行Jest测试React代码的示例(来自 https://github.com/facebook/jest/tree/master/examples/react ),出现以下错误:

Trying to run the example of Jest testing React code (from https://github.com/facebook/jest/tree/master/examples/react ), I get the following error:

  > @ test /home/aizquier/jest/examples/react
  > jest

  Using Jest CLI v0.7.1
   FAIL  __tests__/CheckboxWithLabel-test.js
  ● Runtime Error
  SyntaxError: /home/aizquier/jest/examples/react/__tests__/CheckboxWithLabel-test.js: Unexpected token (15:6)
  npm ERR! Test failed.  See above for more details.

测试在文件CheckboxWithLabel-test.js的第15行失败:

The tests fails at line 15 of file CheckboxWithLabel-test.js:

jest.dontMock('../CheckboxWithLabel');

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';

const CheckboxWithLabel = require('../CheckboxWithLabel');

describe('CheckboxWithLabel', function() {

  it('changes the text after click', function() {

    // Render a checkbox with label in the document
    var checkbox = TestUtils.renderIntoDocument(
      <CheckboxWithLabel labelOn="On" labelOff="Off" />
    );

    var checkboxNode = ReactDOM.findDOMNode(checkbox);

    // Verify that it's Off by default
    expect(checkboxNode.textContent).toEqual('Off');

    // Simulate a click and verify that it is now On
    TestUtils.Simulate.change(TestUtils.findRenderedDOMComponentWithTag(checkbox, 'input'));
    expect(checkboxNode.textContent).toEqual('On');
  });

});

显然,处理.jsx的预处理器不起作用. package.json如下:

apparently the preprocessor to handle .jsx is not working. The package.json is as follows:

{
  "dependencies": {
    "react": "~0.14.0",
    "react-dom": "~0.14.0"
  },
  "devDependencies": {
    "react-addons-test-utils": "~0.14.0",
    "babel-jest": "*",
    "jest-cli": "*"
  },
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "scriptPreprocessor": "./node_modules/babel-jest",
    "unmockedModulePathPatterns": [
      "./node_modules/react",
      "./node_modules/react-dom",
      "./node_modules/react-addons-test-utils",
      "./node_modules/fbjs"
    ]
  }
}

我的节点是v4.2.2,npm是3.3.12.

My node is v4.2.2 and npm is 3.3.12.

任何想法?

推荐答案

我遇到了同样的问题.看来这是由Babel 6的最新版本引起的问题.它正在跟踪在此处,希望很快会添加修复程序!

I was running into the same problem. It looks like this is an issue caused by the recent release of Babel 6. It's being tracked here, and hopefully a fix is added soon!

同时,您可以在package.json中返回到babel-jest的早期版本.例如,尝试^5.3.0.

In the meantime, you can go back to an earlier version of babel-jest in your package.json. For example, try ^5.3.0.

这篇关于React Jest示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:35