使用Jest,当单元测试中的功能中的诺言得到解决时,我如何测试执行的步骤?
示例:
// FUNCTION TO TEST
export function functionToTest(url) {
initSomething().then(() => {
console.log('YEP, CALLED');
storeHistory.push(url);
});
}
// UNIT TEST
import { functionToTest, __RewireAPI__ as rewireUtility } from './funcToTest';
describe('functionToTest', () => {
let pushSpy;
beforeEach(() => {
pushSpy = jest.fn();
rewireUtility.__set__('storeHistory', { push: pushSpy });
rewireUtility.__set__('initSomething', () => Promise.resolve());
});
it('pushes the given url to history after initializing cordova', () => {
functionToTest('/sports');
expect(pushSpy).toHaveBeenCalledTimes(1);
expect(pushSpy).toHaveBeenCalledWith('/sports');
});
});
在这种情况下,pushSpy被调用0次(即使已打印日志)。我该如何正确测试呢?
答案 0 :(得分:0)
已解决:
it.only('pushes the given url to history after initializing cordova', (done) => {
rewireUtility.__Rewire__('history', { push: (route) => {
expect(route).toEqual('/sports');
done();
} });
functionToTest('test://sports');
});