开玩笑,从 await 中模拟 return,我在控制台中看到了结果,但 expect 仍然失败
Jest, mocking return from await, I see results in console, but expect still fails
我有一个非常简单的模拟工作示例,正确更新值,但我的测试仍然失败。
fetchStuff.js
:
const fetchStuff = async (entity, service) => {
entity = await service.get();
console.log('entity is an expected result!', entity); // this consoles out the expected value correctly.
}
fetchStuff.spec.js
:
it('should...', () => {
const expected = 'this will show up in console';
const service.get = jest.fn().mockImplementation(() => expected);
let entity;
fetchStuff(entity, service);
expect(entity).toEqual(expected) // entity = 'undefined'
});
每次,我都看到函数中的 console.log
打印出预期的结果,但由于某些原因 expect(entity)
总是 undefined
。
我试过 flush-promise
之类的东西,我试过删除 await
。我什至见过 done()
技术,我们将 done
传递给 it
,即 it('should...', done => {...})
不知道为什么我不能得到正确的预期结果,即使控制台显示预期的结果。
PS。我知道这不尊重功能范式或纯功能。请无视。
您正在使用异步函数,但在 promise 解决之前立即进行测试。如果您使用异步调用,例如在主函数中等待,则您的测试中需要一个 async/await。
it('should...', async () => {
const expected = 'this will show up in console';
const service.get = jest.fn().mockImplementation(() => expected);
let entity;
await fetchStuff(entity, service);
expect(entity).toEqual(expected) // entity = 'undefined'
});
但是,根据对您问题的评论,代码可能不会返回您期望的结果,因此 async/await 的这一更改本身不会解决问题。
const fetchStuff = async (entity, service) => {
entity = await service.get();
console.log('entity is an expected result!', entity);
return entity;
}
it('should...', async () => {
const expected = 'this will show up in console';
const FakeService = jest.fn().mockResolvedValue(expected),
const entity = await FakeService();
expect(entity).toBe(expected) ;
});
我有一个非常简单的模拟工作示例,正确更新值,但我的测试仍然失败。
fetchStuff.js
:
const fetchStuff = async (entity, service) => {
entity = await service.get();
console.log('entity is an expected result!', entity); // this consoles out the expected value correctly.
}
fetchStuff.spec.js
:
it('should...', () => {
const expected = 'this will show up in console';
const service.get = jest.fn().mockImplementation(() => expected);
let entity;
fetchStuff(entity, service);
expect(entity).toEqual(expected) // entity = 'undefined'
});
每次,我都看到函数中的 console.log
打印出预期的结果,但由于某些原因 expect(entity)
总是 undefined
。
我试过 flush-promise
之类的东西,我试过删除 await
。我什至见过 done()
技术,我们将 done
传递给 it
,即 it('should...', done => {...})
不知道为什么我不能得到正确的预期结果,即使控制台显示预期的结果。
PS。我知道这不尊重功能范式或纯功能。请无视。
您正在使用异步函数,但在 promise 解决之前立即进行测试。如果您使用异步调用,例如在主函数中等待,则您的测试中需要一个 async/await。
it('should...', async () => {
const expected = 'this will show up in console';
const service.get = jest.fn().mockImplementation(() => expected);
let entity;
await fetchStuff(entity, service);
expect(entity).toEqual(expected) // entity = 'undefined'
});
但是,根据对您问题的评论,代码可能不会返回您期望的结果,因此 async/await 的这一更改本身不会解决问题。
const fetchStuff = async (entity, service) => {
entity = await service.get();
console.log('entity is an expected result!', entity);
return entity;
}
it('should...', async () => {
const expected = 'this will show up in console';
const FakeService = jest.fn().mockResolvedValue(expected),
const entity = await FakeService();
expect(entity).toBe(expected) ;
});