【发布时间】:2020-10-05 13:59:13
【问题描述】:
我在 React 应用中使用 Jest 和 React-testing-library 进行了测试:
it('promise test', () => {
const promise = new Promise<string>(resolve => setTimeout(() => resolve('foo'), 1000))
expect.assertions(1);
expect(promise).resolves.toBe('foo');
});
如果我运行测试,我会收到消息:
Error: expect.assertions(1)
Expected :1
Actual :0
我的问题是.. 为什么expect(promise).resolves.toBe('foo') 中的代码没有被调用?如何测试这个异步承诺?
【问题讨论】:
-
也许
dataSources.use抛出了一个错误,而你没有发现它 -
没有发生。我尝试将 await Promise 重写为
dataSources.use<string>('test').then(value => console.log('Value', value)).catch(reason => console.log('Reason', reason));仍然没有 -
你可以试试
expect(promise).to.be.resolvedWith('foo')吗?
标签: reactjs jestjs react-testing-library