how to test promise returning functions

独自空忆成欢 提交于 2020-01-06 06:35:26

问题


I have following async function that return Promise.

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

Now, this the unit test that I wrote for it.

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

    assert.exists(accessToken);
});

When run it, it fails the test saying the following error:

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.

What Am I doing wrong, any suggestions will be appreciated. Thanks in advance.


回答1:


You need to use done callback if you test async code

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();
});


来源:https://stackoverflow.com/questions/48824513/how-to-test-promise-returning-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!