GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — assertive-ts/docs/mocha-tutorial.md at main · stackbuilders/assertive-ts

Expand file tree

/

mocha-tutorial.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
57 lines (43 loc) · 1.38 KB
/

mocha-tutorial.md

Copy path

File metadata and controls

57 lines (43 loc) · 1.38 KB
Raw
Copy raw file
Download raw file
Edit and raw actions

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.

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.