我想为 promise 结果编写测试,我不想在每个 it/pit
部分解决 promise 。
我需要这样的东西:
describe('getData() results test', () => {
return getData().then(response => {
it('foo', () => expect(response.foo).toEqual(1));
it('bar', () => expect(response.bar).toEqual(2));
it('bar', () => expect(response.bar).toEqual(3));
});
});
如果使用 beforeEach
- promise 将被解析的次数与 it
部分的数量一样多。我需要解决一次,然后测试响应。有很多测试用例,所以我想将所有测试分成 it
部分
最佳答案
在 describe 中的所有规范运行之前,beforeAll
函数仅被调用一次。
当返回 promise 时,Jest 将等待 promise 解决,然后再让测试运行。
describe('getData() results test', () => {
let data = null;
beforeAll(() => getData().then(response => {
data = response;
}));
it('foo', () => expect(data.foo).toEqual(1));
it('bar', () => expect(data.bar).toEqual(2));
it('bar', () => expect(data.bar).toEqual(3));
});
关于javascript - 开 Jest - 在测试前解决 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39580689/