DEV Community

Horia Coman
Horia Coman

Posted on • Originally published at horia141.com on

2

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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay