DEV Community

Horia Coman
Horia Coman

Posted on • Originally published at horia141.com on

Raynor-Chai

This post is about raynor-chai, a nifty extension/helper for chai, the JavaScript assertions library. It allows one to use the methods of raynor to check that certain objects behave in the way you like.

Using it looks something like this:

import * as chai from 'chai'
import { raynorChai } from 'raynor-chai'

class User {
    @MarshalWith(StringMarshaller)
    name: string;
    @MarshalWith(ArrayOf(IntegerMarshaller))
    scoresByDay: number[];

    totalScore(): number {
        return this.scoresByDay.reduce((a,b) => a + b, 0);
    }
}

chai.use(raynorChai);

const user = new User();
user.name = 'Raynor';
user.scoresByDay = [10, 20, 30];

chai.expect(user).to.be.raynor(new (MarshalFrom(User))()); // Assertion passes

const badUser = new User();
badUser.name = 'Raynor';
badUser.scoresByDay = [10, 20.5, 30];

chai.expect(badUSer).to.not.be.raynor(new (MarshalFrom(User))()); // Assertion passes
Enter fullscreen mode Exit fullscreen mode

Just like raynor it’s supposed to go beyond simple type checking you’d get from typescript and instead focus on deeper properties of objects. It’s especially useful to test that objects constructed as part of a larger processes are the way they’re supposed to be. So if you’re testing your REST API you can use the same entity definitions you’re using in your application code to see that the API does what it’s supposed to do and outputs the things it’s supposed to.

There’s just one release right now, but I’ll try to evolve it as I use it and as Raynor evolves itself.

Top comments (0)