async-expect
Expectation function taken from expect.
This is not for writing test but to use it when writing asynchronus code with the help of async-await
. (see below for example use-case)
Usage
Installation
Using npm:
$ npm install --save expect
Then, use as you would anything else:
const expect =
Use Case
When using async-await it becomes hard to catch errors. We have to wrap each await
call to try-catch
block to specifically handle that error. Wrapping multiple await
calls in one try-catch
block will create problem which await
call thrown this error.
Example:
Without async-expect
1. One try-catch block
try let user = await User; let like = await Post; let somthingelse = await ; catcherr // no way to get find which call returned that error. // if those are third party lib function then you'll have rely // on regex to find error type and etc console // nothing you can do at this point.
2. Multiple try-catch block
try // let user = await User.find(ctx.body.username); var user = await User;catcherr app try // problem1 - user isnt accessible here. let block scoped // have to user var // let like = await Post.addLike(user.username); var like = await Post;catcherr app // problem2 - readablity issue
3. Using async-expect
const expect = ; try let user = await User; let like = await Post; let somthingelse = await ; catcherr
Assertions
Currently it contains 7 assertive function which is more than enough to be useful in conjunction with async-await.
Note: [error-message, error-name, error-extra] parameters are optional.
toExist
expect(object).toExist([error-message, error-name, error-extra])
Asserts the given object
is truthy.
toNotExist
expect(object).toNotExist([error-message, error-name, error-extra])
Asserts the given object
is falsy.
toBe
expect(object).toBe(value, [error-message, error-name, error-extra])
Asserts that object
is equal to value
using ==
.
try let role = await User; ;catcherr iferrname == "NotAdminError" app;
toNotBe
expect(object).toNotBe(value, [error-message, error-name, error-extra])
Asserts that object
is not equal to value
using !=
.
toBeA(constructor)
expect(object).toBeA(constructor, [error-message, error-name, error-extra])
expect(object).toBeAn(constructor, [error-message, error-name, error-extra])
Asserts the given object
is an instanceof constructor
.
toBeA(string)
expect(object).toBeA(string, [error-message, error-name, error-extra])
expect(object).toBeAn(string, [error-message, error-name, error-extra])
Asserts the typeof
the given object
is string
.
toNotBeA(constructor)
expect(object).toNotBeA(constructor, [error-message, error-name, error-extra])
expect(object).toNotBeAn(constructor, [error-message, error-name, error-extra])
Asserts the given object
is not an instanceof constructor
.
toNotBeA(string)
expect(object).toNotBeA(string, [error-message, error-name, error-extra])
expect(object).toNotBeAn(string, [error-message, error-name, error-extra])
Asserts the typeof
the given object
is not string
.