问题
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