如何测试承诺返回功能(how to test promise returning functions

2019-10-28 17:59发布

我有以下异步函数返回的承诺。

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.

我在做什么错了,任何建议将不胜感激。 提前致谢。

Answer 1:

您需要使用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();
});


文章来源: how to test promise returning functions