Functional written code is usually easier to test with a unit test. A unit test is an automated test that checks whether a small piece of code is working. We will use the ava
test runner, which we can install using NPM:
npm install ava --save-dev npm install ava -g
At the time of writing, ava
does not have type definitions bundled. We will quickly add some types for it in lib/test/ava.d.ts
:
declare module "ava" { function test(name: string, run: (t: any) => void): void; namespace test {} export = test; }
We can now write some tests for the factorial function. In lib/test/utils.ts
, we write a test
case. Any test file will have the following structure:
import * as test from "ava"; import { factorial } from "../model/utils"; test("factorial", t => { t.is(factorial(0), 1); t.is(factorial(1), 1); t.is(factorial(2), 2); t.is(factorial(3), 6); t.is(factorial(4), 24); }); ...