如何测试承诺返回功能

时间:2018-02-16 10:16:37

标签: typescript asynchronous promise mocha chai

我有以下异步函数返回Promise。

static getAccessToken(env: DeploymentEnv, username: string, password: string): Promise<AccessToken>;

现在,这是我为它写的单元测试。

it("should be able to get access token",async ()=>{
    let accessToken = await IModelHubServiceBusClient.getAccessToken('QA',
                      'abc@xyz.com',
                      'abc')!;

    assert.exists(accessToken);
});

运行时,它未通过测试说出以下错误:

should be able to get access token:
 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我做错了什么,任何建议都将受到赞赏。 提前致谢。

1 个答案:

答案 0 :(得分:0)

如果您测试异步代码

,则需要使用done回调
it("should be able to get access token",async (done)=>{
    let accessToken = await IModelHubServiceBusClient.getAccessToken('QA',
                      'bistroDEV_pmadm1@mailinator.com',
                      'pmadm1')!;

    assert.exists(accessToken);
    done();
});