分组测试异步请求,官方文档没有找到相关例子,只能摸着石头过河,踩了很多坑,最后发现,原来正确的姿式是这样纸:javascript
global.fetch = require('node-fetch'); import api from '../../api' let res= {}; beforeAll(async () => { res = await api.getData(); return res;// 目前还不清楚这个return是干什么用的,删了也能够没有任何影响,不删下文又没有任何地方调用,官方文档也是这么写的,多是习惯吧。 }); describe('begin to test filterList',() => { test('test obj', () => { const { obj } = res; const expectData = { key1: "value1" } expect(obj ).toMatchObject(expectData) }); test('test arr', () => { const { arr } = res; const reg_date = /^\d{1,2}\/\d{1,2}\([A-Z]{3}\)$/; arr .map((item,index)=>{ // if(index==3){ item = item+'-' }// make a wrong item expect(item).toMatch(reg_date) }) }); })