是否保证在内部 beforeEach
开始之前完成外部 beforeEach
?
let qux;
beforeEach(() => {
//
// other synchronous code here
//
qux = null;
});
describe('Description', () => {
beforeEach(() => {
qux = 0;
//
// other synchronous code here
//
});
it('Predicate', () => {
expect(qux).toEqual(0);
});
});
也就是说,上面的测试能保证通过吗?
最佳答案
是的,外层 beforeEach
保证在内层 beforeEach
开始之前完成:
Jest
通过从定义规范的位置开始并向上遍历父级来查找规范的所有 before
函数 here并返回反向列表 here .
before
函数、测试和after
函数被放入一个数组中 here .
每个函数都由 mapper
包装它返回一个 Promise,该 Promise 在函数完成、测试被取消、发生错误或达到超时之前不会解析,并且生成的 Promise 由 reduce 链接在一起
here .
因此,除非出现错误、超时或测试被取消,否则 beforeEach
函数将按顺序运行直至完成,并且上述测试将通过。
关于javascript - 描述外部的 beforeEach 是否总是在描述内部的 beforeEach 开始之前完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52599141/