当在异步测试用例中得到"SPEC HAS NO EXPECTATIONS"消息时,这通常是因为测试用例没有正确地处理异步操作。下面是几种解决方法:
it('should test async code', async () => {
// 异步操作
await asyncFunction();
// 断言
expect(...).toBe(...);
});
it('should test async code', (done) => {
asyncFunction().then(() => {
// 断言
expect(...).toBe(...);
done();
});
});
jest.setTimeout(10000); // 设置超时时间为10秒
it('should test async code', async () => {
// 异步操作
await asyncFunction();
// 断言
expect(...).toBe(...);
});
确保按照上述方法之一来处理异步操作,这样你就不会再收到"SPEC HAS NO EXPECTATIONS"消息了。
上一篇:不会突出显示实体