2016-09-30 12 views
3

私はjestスナップショットテストを実装しました。私が解決できないのは、私のコンポーネントが自分のCI上で別のスナップショットをレンダリングしているということだけです。CIとローカルでテストするときにJestスナップショットが異なる

/* 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, 
}; 

私はスナップショットを生成するためにenzymeenzyme-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) 

のでoptionComponentvalueComponentは私のローカルスナップショットと比較して、異なる値を持っています。ローカルテストとリモートテストの間にこの不一致が生じる原因は何ですか?

+0

最初はモジュールの違いがありましたが、リモートは新しいモジュールをインストールしていましたが、 'rm -rf node_modules && npm i'はローカルスナップショットを変更しませんでした。 – vkjb38sjhbv98h4jgvx98hah3fef

答えて

7

更新(2016年10月3日):冗談16.0はwith the fixをリリースしました!


これはknown issueあり、冗談のV16(source)に固定されます。 PR #1752で修正されました。

Node.jsが関数名を処理する方法に問題があります。 ローカルマシンとCIのNode.jsとまったく同じバージョンを使用すると問題はありません。

JESTのソリューションは、スナップショットから名前を削除することです。それは次のようになります。

optionComponent={[Function]} 

トリックは/問題でpointedをハック無名関数で関数をラップすることです:

-  onSelect={onSelectHandler} 
+  onSelect={(...args) => onSelectHandler(...args)} 

残念ながら、これはreact-selectライブラリで発生しなければなりません。

+0

ああ!私は自分自身でそれを理解していないだろう、ありがとう! – vkjb38sjhbv98h4jgvx98hah3fef

+1

ありがとうございます!私は自分のスナップがローカルではなくCircleCIで問題を抱えていた。私のノードのバージョンがローカルとCIで一致していることを確認した後、私は最終的に私がローカルマシン上で見ることができなかった失敗したスナップショットを見ることができました。 – Bert

関連する問題