我实施了jest快照测试,效果很好。唯一我无法解决的是我的组件在我的CI上呈现不同的快照。我的测试是:当通过CI与本地测试时Jest snapshot不同时
/* eslint-env jest */
/* eslint import/no-extraneous-dependencies: "off" */
import React from 'react';
import { shallow } from 'enzyme';
import { shallowToJson } from 'enzyme-to-json';
import Combobox from '../Combobox';
describe('<Combobox />',() => {
it('renders correctly',() => {
const wrapper = shallow(
<Combobox
items={[]}
placeholder=""
valueKey=""
labelKey=""
/>
);
expect(shallowToJson(wrapper)).toMatchSnapshot();
});
});
和组件是:
import React, { PropTypes } from 'react';
import Select from 'react-select';
export default class Combobox extends React.Component {
constructor(props) {
super(props);
this.state = {
currentValue: null,
};
this.updateValue = this.updateValue.bind(this);
}
updateValue(newValue) {
this.setState({
currentValue: newValue,
});
}
render() {
return (
<Select
placeholder={this.props.placeholder}
valueKey={this.props.valueKey}
labelKey={this.props.labelKey}
options={this.props.items}
value={this.state.currentValue}
onChange={this.updateValue}
/>
);
}
}
Combobox.propTypes = {
items: PropTypes.array.isRequired,
placeholder: React.PropTypes.string.isRequired,
valueKey: React.PropTypes.string.isRequired,
labelKey: React.PropTypes.string.isRequired,
};
我使用enzyme
和enzyme-to-json
生成快照。该组件本身是围绕react-select
的包装。本地测试时一切正常,但在我的CI它的错误与:
FAIL src/components/__tests__/Combobox.test.jsx
● <Combobox /> › renders correctly
Received value does not match the stored snapshot 1.
- Snapshot
+ Received
<Select
// ...
- optionComponent={[Function anonymous]}
+ optionComponent={[Function Constructor]}
// ...
- valueComponent={[Function anonymous]}
+ valueComponent={[Function Constructor]}
valueKey="" />
at Object.<anonymous> (src/components/__tests__/Combobox.test.jsx:20:82)
at process._tickCallback (internal/process/next_tick.js:103:7)
所以optionComponent
和valueComponent
相比,我的本地快照具有不同的值。什么可能导致我的本地和我的远程测试之间的这种差异?
起初我虽然是模块的区别,但是远程安装的是较新的模块,但'rm -rf node_modules && npm i'没有改变我的本地快照。 – vkjb38sjhbv98h4jgvx98hah3fef