Reputation: 422
For e2e testing, I use Protractor with Jasmine and Control Flow is disabled. I am aware that Protractor uses jasminewd2 an adapter for Jasmine. And that it enhances expect so that it automatically unwraps promises before performing the assertion. So both the statements below has the same effect.
expect(await someFn()).toBe..
await expect(someFn()).toBe..
Is there any situation where above statements will behave differently? What is the recommended option? Example spec here follows the second approach of putting await first. Is there any advantage in doing this?
[Edit 1] One difference I found is the way in which Promise reject is handled. Try catch will work in both cases, but second option allows catch-ing Promise way.
it('should throw error 1', async () => {
try {
expect(await someFn()).toBe(1);
} catch (err) {
expect(err).toEqual('Unable to calculate');
}
});
it('should throw error 2', async () => {
await expect(someFn()).toBe(1)
.catch((err) => expect(err).toEqual('Unable to calculate'));
});
Upvotes: 0
Views: 75
Reputation: 801
You are right with your explanations about jasminewd2.
I would go with option 1, putting the await
inside the expect()
function, because there lies the actual Promise to be awaited.
I don't see any advantage for the wrappers, now that the control flow in Protractor has been replaced by async/await.
Upvotes: 1