lorenzofox3 / zora

Lightest, yet Fastest Javascript test runner for nodejs and browsers
MIT License
539 stars 93 forks source link

feat: add rejects assertion #160

Closed panva closed 2 years ago

panva commented 2 years ago

I found this useful assertion missing. Its implementation is inspired by throws from zora.

lorenzofox3 commented 2 years ago

Thanks 👍 , I'll get a look.

panva commented 2 years ago

Must be overlooking something.

While the assert part is correct and works in unit tests, failed or succeeding use of it inside test() doesn't get picked up... Something with onResult and the fact that the assertion result is resolved by a promise, not immediately returned.

lorenzofox3 commented 2 years ago

Must be overlooking something.

While the assert part is correct and works in unit tests, failed or succeeding use of it inside test() doesn't get picked up... Something with onResult and the fact that the assertion result is resolved by a promise, not immediately returned.

Yep, assertion functions should be simple pure functions. The fact it is almost the same as the "throws" operator is also a code smell to me.

It has more to do with the control flow than an actual assertion (throws as well, but it was already part of the library).

try{
 await throwingFunc();
 t.fail('should not get here')
} catch(e){
  t.ok(e instanceof MyCustomError);
}

is probably more verbose, but more explicit of what we test/expect, which is always better in my opinion than saving few lines.

You can always put it in a helper:

export const testAsync = ({assert, fn}) => {
try{
 await fn();
 assert.fail('should not get here')
} catch(e){
  assert.ok(e instanceof MyCustomError);
}
}