我有以下简化示例,其中我手动模拟依赖项并使其返回已解析的Promise。在我的一个测试中,我然后尝试将其返回类型更改为Promise拒绝。
如果我一起运行所有3个测试,则会失败,因为拒绝测试中的调用实际上已解决。但是,如果我将规范中的第17行修改为it.only('should reject if the client fails to load'
,那么单次测试就会通过。
我做错了什么?
service.js
import client from './client';
const service = (() => {
let instance;
const makeCall = () => {
return true;
};
const init = async () => {
const _client = await client();
return Promise.resolve({
makeCall,
});
};
const getInstance = async () => {
if (!instance) {
instance = await init();
}
return Promise.resolve(instance);
};
return {
getInstance,
};
})();
export default service;
client.js
const client = () => Promise.resolve('Real');
export default client;
__test__/service.spec.js
import client from '../client';
import service from '../service';
jest.mock('../client');
describe('Service', () => {
it('should return a singleton', () => {
expect(service.getInstance()).toEqual(service.getInstance());
});
it('should resolve if the client successfully loads', async () => {
expect.assertions(1);
await expect(service.getInstance()).resolves.toHaveProperty('makeCall');
});
it('should reject if the client fails to load', async () => {
const errorMessage = 'Client could not be initialised.';
expect.assertions(1);
client.mockReturnValueOnce(Promise.reject(new Error(errorMessage)));
await expect(service.getInstance()).rejects.toHaveProperty(
'message',
errorMessage
);
});
});
__mocks__/client.js
const client = jest.fn();
client.mockImplementation(() => {
return Promise.resolve('Mock file');
});
export default client;
答案 0 :(得分:0)
这是因为试图模拟Singleton的依赖关系。
当运行所有三个测试时,使用原始模拟实现(解析),然后不会被拒绝的第二个模拟实现替换。
值得注意的是,由于被拒绝的Promise已创建但因此未被处理,因此会导致未处理的Promise拒绝,并且测试套件崩溃。