Javascript Jasmine:如何测试异步函数?
在我的NodeJS应用程序中,我有以下Javascript Jasmine:如何测试异步函数?,javascript,node.js,unit-testing,jasmine,Javascript,Node.js,Unit Testing,Jasmine,在我的NodeJS应用程序中,我有以下heplers.ts文件,其中有一个方法,wait: export const wait = async (ms: number) => { return new Promise(resolve => { setTimeout(resolve, ms); }); }; 我目前正在为此文件编写一个单元测试,这就是我现在拥有的: import { setProcessEnv } from 'spec/helpers'; import
heplers.ts
文件,其中有一个方法,wait
:
export const wait = async (ms: number) => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
我目前正在为此文件编写一个单元测试,这就是我现在拥有的:
import { setProcessEnv } from 'spec/helpers';
import { wait } from '../../util/helpers';
describe('Helper methods', () => {
beforeEach(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
setProcessEnv();
});
it('should wait a specified amount of time', async () => {
const TIMEOUT = 10000;
jasmine.clock().install(); // First install the clock
await wait(TIMEOUT);
jasmine.clock().tick(10000); // waits till 10000 milliseconds
expect(wait).toHaveBeenCalled();
jasmine.clock().uninstall(); // uninstall clock when done
});
});
但我一直在接受
错误:超时-异步功能未在20000ms内完成(已设置
默认值(超时时间间隔)
我已将jasmine的默认超时时间间隔增加到20000毫秒,但仍然不起作用。
如何测试这些异步函数
我已经找到了coupes的例子,但它们在我的案例中不起作用:
更新
这是我的最新版本,没有抛出错误,但问题是它没有包含返回语句行(returnnewpromise(…
):
问题是,通过使用jasmine的自定义时钟,您需要手动调用
.tick()
来向前移动时间。由于您立即等待wait
调用,因此wait
中的设置超时将无法实现。因此,当您调用.tick()时,承诺永远无法解决
等待承诺后
您可以通过以下方式解决此问题:不立即等待从wait
返回的承诺,而是将其分配给变量,然后向前移动时间。然后,等待承诺并检查是否已调用wait
:
describe('Helper methods', () => {
it('should wait a specified amount of time', async () => {
const TIMEOUT = 10000;
jasmine.clock().install(); // First install the clock
const waitPromise = wait(TIMEOUT);
jasmine.clock().tick(10000); // waits till 10000 milliseconds
await waitPromise; // now await the promise
expect(wait).toHaveBeenCalled();
jasmine.clock().uninstall(); // uninstall clock when done
});
});
请注意,在上述代码中,等待
上未设置任何间谍,因此.toHaveBeenCalled
可能会失败。如果需要帮助设置间谍功能,请查看此链接:
要回答更新的问题:您错误地设置了间谍。您需要将其更改为以下内容:
import { setProcessEnv } from 'spec/helpers';
import * as WaitFunction from from '../../util/helpers';
...
it('should wait a specified amount of time', async () => {
const TIMEOUT = 10000;
const waitSpy = spyOn(WaitFunction, 'wait').and.callThrough();
await WaitFunction.wait(TIMEOUT);
expect(waitSpy).toHaveBeenCalled();
expect(waitSpy).toHaveBeenCalledWith(TIMEOUT);
});
您的测试似乎需要超过20秒才能执行,它需要10秒才能执行等待等待(超时);
,另外10秒才能执行jasmine.clock()。勾选(10000)
。当您设置jasmine.DEFAULT\u TIMEOUT\u INTERVAL=22000时会发生什么?抱歉,eol,但是像您提供的解决方案那样做没有任何意义-它不断抛出错误为什么没有意义?“它不断抛出错误”-现在的错误是什么?wait
不是间谍,在我将其更改为间谍后,它没有涵盖整个方法:(我更新了postWellwait
不是间谍正是我在回答的最后一句话中告诉你的。而且你创建的间谍不正确。我更新了我的回答,向你展示如何。。
import { setProcessEnv } from 'spec/helpers';
import * as WaitFunction from from '../../util/helpers';
...
it('should wait a specified amount of time', async () => {
const TIMEOUT = 10000;
const waitSpy = spyOn(WaitFunction, 'wait').and.callThrough();
await WaitFunction.wait(TIMEOUT);
expect(waitSpy).toHaveBeenCalled();
expect(waitSpy).toHaveBeenCalledWith(TIMEOUT);
});