Assertive.ts

A type-safe fluent assertion library

Usage with Mocha

Let’s set up a project with Mocha and assertive-ts.

First, let’s install the dependencies:

npm install --save-dev typescript mocha @types/mocha ts-node @assertive-ts/core

This library is meant to be used with TypeScript, so to have better results we encourage you to use TypeScript in your project.

Let’s create a tsconfig.json file:

npx tsc -init

Let’s also create a Mocha configuration file:

.mocharc.json

{
  "extension": ["ts"],
  "spec": "tests/**/*.test.ts",
  "require": [
    "ts-node/register"
  ]
}

As the config includes the TypeScript transpilation hook ts-node/register it does not require pre-compilation before running.

For more information about Mocha configuration please refer to the official documentation

We are going to test a simple function:

src/mathUtils.ts

export const sum = (a: number, b: number): number => {
  return a + b;
}

Now let’s write a test for that function. Make sure to import expect from the @assertive-ts/core module.

tests/mathUtils.test.ts

import { expect } from "@assertive-ts/core";
import { sum } from "../src/mathUtils";

describe("sum", () => {
  it("adds two numbers", () => {
    expect(sum(1, 2)).toBeEqual(3);
  })
});

And that’s it! Now you can run your tests as usual with your test script.