使用 Enzyme 与 Mocha

enzyme 最初是为 Mocha 设计的,因此使用 Mocha 运行它应该没有任何问题。只需安装它并开始使用。

npm i --save-dev enzyme
import React from 'react';
import { expect } from 'chai';
import { mount } from 'enzyme';
import { spy } from 'sinon';
import Foo from './src/Foo';

spy(Foo.prototype, 'componentDidMount');

describe('<Foo />', () => {
  it('calls componentDidMount', () => {
    const wrapper = mount(<Foo />);
    expect(Foo.prototype.componentDidMount).to.have.property('callCount', 1);
  });
});