2016-08-03 78 views
3

我們正在進行關於酶shallow renders的工作討論,以及每次測試重新運行淺試驗的時間。無論是方法,點擊,選擇器長度等,我建議我們的測試可能運行得更快,如果我們淺顯示組件一次之前測試運行與每個時間。酶/反應淺呈現昂貴?

有沒有專家能指出哪種方式更快,哪種方式有缺陷?這些例子正在使用AVA跑步者(爲了討論的緣故略有設計)。

例如,這裏有一個方法(一個)...

import TagBox from '../TagBox'; 
const props = { toggleValue: sinon.spy() }; 
let wrapper = {}; 

test.before(t => { 
    wrapper = shallow(<TagBox />); 
}); 

test('it should have two children', t => { 
    t.is(wrapper.children().length, 2); 
}); 

test('it should safely set props', t => { 
    wrapper.setProps({...props}); 
    t.is(wrapper.children().length, 2); 
}); 

test('it should call when clicked', t => { 
    wrapper.setProps({...props}); 
    wrapper.find({tagX : true}).last().simulate('click'); 
    t.true(props.toggleValue.calledOnce); 
}); 

而這裏的其他()...

import TagBox from '../TagBox'; 

test('it sets value to null ...', t => { 
    const props = {multiple: false}; 
    const wrapper = shallow(<TagBox {...props} />); 
    t.is(wrapper.state('currentValue'), null); 
}); 

test('it sets value to [] if multiple', t => { 
    const props = {multiple: true}; 
    const wrapper = shallow(<TagBox {...props} />); 
    t.deepEqual(wrapper.state('currentValue'), []); 
}); 

test('it does not use value if ...', t => { 
    const props = = {value: 3}; 
    const wrapper = shallow(<TagBox {...props} />); 
    t.is(wrapper.state('currentValue'), null); 
}); 

// etc. etc. 

注意,在測試B,每個測試都有一個新的淺包裝,但基本上什麼都沒有改變,但道具。

在100次測試過程中,您認爲完成時間有何不同?

還有什麼機會淺度渲染一次(測試A)在更高的範圍會污染測試狀態?

回答

2

淺的渲染器被設計得很快,因爲它只渲染單個組件。因此,當您爲每個測試創建新組件時,通常不會遇到任何性能問題。

此外,如果TagBox組件具有內部狀態,則您的示例A可能會不正確地工作。那麼爲什麼例子B是編寫測試的更可取的方式。

0

shallow在這裏可能不是你的問題,因爲它被設計成渲染組件的最快方式,而不用級聯它的所有子渲染。

你可能會考慮改變你的測試運行引擎,然後AVA比Jest慢一些。一年前我做了這個改變,而且速度更快。 Jest還提供了更多有用的東西,如示例的嘲笑功能。

更多這裏:https://facebook.github.io/jest/