javascript - 失败的 Jest 单元测试

标签 javascript reactjs unit-testing jestjs

我有一个组件,我在其中编写了测试。它工作得很好,但现在出了点问题,我不知道是什么。

这是一个简单的组件,它接受两个数字并返回它们的和:

import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/css/bootstrap-theme.css';
import React from 'react';
import ReactDOM from 'react-dom';
import { LogOutButton } from './LogOutButton.js';

class Home extends React.Component {
displayName = Home.name;

state = {
  result: 0,
  val1: 0,
  val2: 0,
};

handleChangeOne = event => {
  this.setState({ val1: event.target.value });
};

handleChangeTwo = event => {
  this.setState({ val2: event.target.value });
};

add = () => {
  this.setState({ 
    result: parseInt(this.state.val1) + parseInt(this.state.val2)
  });
};

onLogoutClick = () => {
  window.location.href = 'https://www.MICROSOFT.com';
}

render() {
  return (
    <div>
      <h1>Hello world! The result is: {this.state.result}</h1>
      <input type="text" onChange={this.handleChangeOne} />
      +
      <input type="text" onChange={this.handleChangeTwo} />
      = <br />
      <button onClick={this.add}>Add</button>
      <br/><br/> 
      <LogOutButton onLogout={this.onLogoutClick} /> 
    </div>
  );
}
}

  export default Home;

这是曾经非常有效的测试:

import React from 'react';
import { shallow } from 'enzyme'
import ReactDOM from 'react-dom';
import App from './App';
import { Home } from './components/Home';

describe('Home />', () => {
  it('Renders a sum', () => {
      const home = shallow(<Home />);
      var first_value = home.state().val1;
      var second_value = home.state().val2;
      var result = first_value + second_value;
      expect(result).toBe(0);

      const inputs = home.find('input');
      inputs.at(0).simulate('change', {target: {value: 5} } );
      inputs.at(1).simulate('change', { target: { value: 8 } });
      home.find('button').simulate('click');
      home.update();
      expect(home.state().result).toBe(13);
  });
});

这是我得到的错误:

 FAIL  src/Home.test.js
  ● Test suite failed to run

C:/Users/Itay/Documents/Experiments/WebApplication1/WebApplication1/ClientApp/src/components/Home.js: Unexpected token (8:12)

  Jest encountered an unexpected token
  This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
  By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
  Here's what you can do:
   • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
   • If you need a custom transformation specify a "transform" option in your config.
   • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
  You'll find more details and examples of these config options in the docs:
  https://jestjs.io/docs/en/configuration.html
  Details:
     6 |
     7 | class Home extends React.Component {
  >  8 | displayName = Home.name;
       |             ^
     9 |
    10 |     state = {
    11 |       result: 0,

这是怎么回事?我已经尝试了几件事,但到目前为止没有任何帮助。

最佳答案

为了让您的测试运行,我必须在 package.json 中添加:

"enzyme-adapter-react-16": "1.6.0"

这在测试中:

import Enzyme, { shallow } from 'enzyme';
import Adapter from "enzyme-adapter-react-16";

Enzyme.configure({ adapter: new Adapter() });

然后测试通过。

请注意,我在 CRA 2.1 环境中对此进行了测试。 https://github.com/facebook/create-react-app

关于javascript - 失败的 Jest 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53103299/

相关文章:

javascript - Chart.js 移除堆叠

javascript - 如何从 react-bootstrap 复选框获取值/属性?

unit-testing - 具有复杂目录结构的单元测试

javascript - 如何扩展或合并 Styled Components 主题

javascript - 如何在没有不安全的内联 JavaScript/CSS 代码的情况下使用 React?

javascript - 无法在 mocha 测试的上下文中调用 sinon spy ,方法是使用 promises 调用方法

python - 在单元测试中使用 pyodbc

javascript - 依赖注入(inject)非实例化对象的优点和缺点

javascript - 从javascript中的原型(prototype)函数访问构造函数名称

Javascript NaN 错误