1 回答
TA贡献1835条经验 获得超7个赞
正确的是,回调函数不应该是异步的。它同步定义套件的测试,其范围内的任何异步操作都将被丢弃。describe
以前,Jasmine 和 Jest 允许使用常规函数和 .此功能在Jest中被弃用;公共变量需要由用户定义。this
共享代码可以移动到内部使用 的帮助器函数中, 等:beforeAllbeforeEach
const setupWithTestContext = (testContext = {}) => {
beforeAll(async () => {
const setupData = await setup();
Object.assign(testContext, setupData);
});
return testContext; // sets up a new one or returns an existing
});
const anotherSetupWithTestContext = (testContext = {}) => {
beforeEach(() => {
testContext.foo = 0;
});
return testContext;
});
...
describe('Some group of tests', async () => {
const sharedTestData = setupTestContext();
// or
// const sharedTestData = {}; setupTestContext(sharedTestData);
anotherSetupWithTestContext(sharedTestData);
test('Test1', async () => {
// context is filled with data at this point
const actual = myFunc(sharedTestData.x)
...
}
...
})
添加回答
举报