GithubHelp home page GithubHelp logo

async-expect's Introduction

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 = require("async-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.find(ctx.body.username);

    let like = await Post.addLike(user.username);

    let somthingelse = await forSomethingelse();

}catch(err){
    // 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.log(err) // 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.find(ctx.body.username);
}catch(err){
    app.redirect("/err", "invalid user")
}

try{
    // problem1 - user isnt accessible here. let is block scoped
    // have to use var
    // let like = await Post.addLike(user.username);
    var like = await Post.addLike(user.username);
}catch(err){
    app.redirect("/err", "you cant like this post.")
}

// problem2 - readablity issue

With async-expect

const expect = require("async-expect");

try{ 
    let user = await User.find(ctx.body.username);
    expect(user.username).toExists("Username is invalid", "INVALID-USER")

    let like = await Post.addLike(user.username);
    expect(like.status).toBe(true, 
        "User doesn't have permission to see", "PERM-DENIED")

    let somthingelse = await forSomethingelse();
    expect(somethingelse).toBe(Object, "something else is wrong", "SOMETHING-ERROR", somethingelse)

}catch(err){
    switch(err.name){
        case: "INVALID-USER":
            app.redirect("/register", "Please register to continue");
            break;
        case: "PERM-DENIED":
            app.redirect("/user", "You dont have enough permission");
            break;
        case: "SOMETHING-ERROR":
            let somethingelse = err.extra
            doSomething(somethingelse);
            break;
    }
}

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.

expect('something truthy').toExist()

toNotExist

expect(object).toNotExist([error-message, error-name, error-extra])

Asserts the given object is falsy.

expect(null).toNotExist()

toBe

expect(object).toBe(value, [error-message, error-name, error-extra])

Asserts that object is equal to value using ==.

try { 
  let role = await User.getRole();
  expect(role).toBe("admin", "Only admin user is allowed", "NotAdminError");
}catch(err){
  if(err.name == "NotAdminError"){
    app.redirect("/user", {message: "You're not allowed to perform this operation"});
  }
}

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.

expect(new User).toBeA(User)
expect(new Asset).toBeAn(Asset)

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.

expect(2).toBeA('number')

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.

expect(new Asset).toNotBeA(User)
expect(new User).toNotBeAn(Asset)

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.

expect('a string').toNotBeA('number')
expect(2).toNotBeAn('object')

async-expect's People

Contributors

shahidcodes avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.