You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
eslint-plugin-jest with its no-large-snapshots option
snapshot-diff
测试应该是确定的(多次测试应该得到相同的结果)
使用描述性快照名称
推荐将最新的snapshot更新到版本管理工具中
Async Mock实例
// user.jsimportrequestfrom'./request';exportfunctiongetUserName(userID){returnrequest('/users/'+userID).then(user=>user.name);}// request.jsconsthttp=require('http');exportdefaultfunctionrequest(url){returnnewPromise(resolve=>{// This is an example of an http request, for example to fetch// user data from an API.// This module is being mocked in __mocks__/request.jshttp.get({path: url},response=>{letdata='';response.on('data',_data=>(data+=_data));response.on('end',()=>resolve(data));});});}// __mocks__/request.jsconstusers={4: {name: 'Mark'},5: {name: 'Paul'},};exportdefaultfunctionrequest(url){returnnewPromise((resolve,reject)=>{constuserID=parseInt(url.substr('/users/'.length),10);process.nextTick(()=>users[userID]
? resolve(users[userID])
: reject({error: 'User with '+userID+' not found.',}),);});}// __tests__/user-test.jsjest.mock('../request');import*asuserfrom'../user';// The assertion for a promise must be returned.it('works with promises',()=>{expect.assertions(1);returnuser.getUserName(4).then(data=>expect(data).toEqual('Mark'));});it('works with resolves',()=>{expect.assertions(1);returnexpect(user.getUserName(5)).resolves.toEqual('Paul');});it('works with async/await',async()=>{expect.assertions(1);constdata=awaituser.getUserName(4);expect(data).toEqual('Mark');});// async/await can also be used with `.resolves`.it('works with async/await and resolves',async()=>{expect.assertions(1);awaitexpect(user.getUserName(5)).resolves.toEqual('Paul');});
// timerGame.js'use strict';functiontimerGame(callback){console.log('Ready....go!');setTimeout(()=>{console.log('Times up -- stop!');callback&&callback();},1000);}module.exports=timerGame;// __tests__/timerGame-test.js'use strict';jest.useFakeTimers();// 验证callback被调用前等待了1stest('waits 1 second before ending the game',()=>{consttimerGame=require('../timerGame');timerGame();expect(setTimeout).toHaveBeenCalledTimes(1);expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function),1000);});// 验证callback在1s后被调用test('calls the callback after 1 second',()=>{consttimerGame=require('../timerGame');constcallback=jest.fn();timerGame(callback);// At this point in time, the callback should not have been called yetexpect(callback).not.toBeCalled();// Fast-forward until all timers have been executedjest.runAllTimers();// Now our callback should have been called!expect(callback).toBeCalled();expect(callback).toHaveBeenCalledTimes(1);});
runOnlyPendingTimers 处理timer嵌套的问题
// infiniteTimerGame.js'use strict';functioninfiniteTimerGame(callback){console.log('Ready....go!');setTimeout(()=>{console.log('Times up! 10 seconds before the next game starts...');callback&&callback();// Schedule the next game in 10 secondssetTimeout(()=>{infiniteTimerGame(callback);},10000);},1000);}module.exports=infiniteTimerGame;// __tests__/infiniteTimerGame-test.js'use strict';jest.useFakeTimers();describe('infiniteTimerGame',()=>{test('schedules a 10-second timer after 1 second',()=>{constinfiniteTimerGame=require('../infiniteTimerGame');constcallback=jest.fn();infiniteTimerGame(callback);// At this point in time, there should have been a single call to// setTimeout to schedule the end of the game in 1 second.expect(setTimeout).toHaveBeenCalledTimes(1);expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function),1000);// Fast forward and exhaust only currently pending timers// (but not any new timers that get created during that process)jest.runOnlyPendingTimers();// At this point, our 1-second timer should have fired it's callbackexpect(callback).toBeCalled();// And it should have created a new timer to start the game over in// 10 secondsexpect(setTimeout).toHaveBeenCalledTimes(2);expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function),10000);});});
advanceTimersByTime(msToRun) 设置向前运行多少毫秒
it('calls the callback after 1 second via advanceTimersByTime',()=>{consttimerGame=require('../timerGame');constcallback=jest.fn();timerGame(callback);// At this point in time, the callback should not have been called yetexpect(callback).not.toBeCalled();// Fast-forward until all timers have been executedjest.advanceTimersByTime(1000);// Now our callback should have been called!expect(callback).toBeCalled();expect(callback).toHaveBeenCalledTimes(1);});
The text was updated successfully, but these errors were encountered:
importReactfrom'react';import{expect}from'chai';import{shallow}from'enzyme';importsinonfrom'sinon';importMyComponentfrom'./MyComponent';importFoofrom'./Foo';describe('<MyComponent />',()=>{it('renders three <Foo /> components',()=>{constwrapper=shallow(<MyComponent/>);expect(wrapper.find(Foo)).to.have.length(3);});it('renders an `.icon-star`',()=>{constwrapper=shallow(<MyComponent/>);expect(wrapper.find('.icon-star')).to.have.length(1);});it('renders children when passed in',()=>{constwrapper=shallow((<MyComponent><divclassName="unique"/></MyComponent>));expect(wrapper.contains(<divclassName="unique"/>)).to.equal(true);});it('simulates click events',()=>{constonButtonClick=sinon.spy();constwrapper=shallow(<FooonButtonClick={onButtonClick}/>);wrapper.find('button').simulate('click');expect(onButtonClick).to.have.property('callCount',1);});});
Example
importReactfrom'react';import{shallow,mount,render}from'enzyme';importFoofrom'../Foo';describe('A suite',function(){it('should render without throwing an error',function(){expect(shallow(<Foo/>).contains(<divclassName="foo">Bar</div>)).toBe(true);});it('should be selectable by class "foo"',function(){expect(shallow(<Foo/>).is('.foo')).toBe(true);});it('should mount in a full DOM',function(){expect(mount(<Foo/>).find('.foo').length).toBe(1);});it('should render to static HTML',function(){expect(render(<Foo/>).text()).toEqual('Bar');});});
Snapshot 测试
一般用于UI测试,配合React Component可以直观地比较UI的变化。
第一次运行会生成一次snapshot,之后再运行,如果UI发生了变化,测试用例就会失败。这时,需要去检查是bug还是UI确实更新了,如果是bug则需要修复,如果是UI更新,可以通过命令
jest --updateSnapshot
来更新。该命令会更新所有的用例UI变化,所以应该在执行该命令前修复相应的bug,或者通过参数--testNamePattern
来更新具体的用例。最佳实践
利用相关工具进行代码美化:
eslint-plugin-jest
with itsno-large-snapshots
option测试应该是确定的(多次测试应该得到相同的结果)
使用描述性快照名称
推荐将最新的snapshot更新到版本管理工具中
Async Mock实例
Timer Mock实例
使用
jest.useFakeTimers()
mocksetTimeout, setInterval, clearTimeout, clearInterval
.runAllTimers
runOnlyPendingTimers 处理timer嵌套的问题
advanceTimersByTime(msToRun) 设置向前运行多少毫秒
The text was updated successfully, but these errors were encountered: