Testing with Jest


Before going much further, bringing Jest / testing in to the mix is a good idea. Start off early with testing, and you (or I) am much more likely to continue with it throughout the rest of the project. If you add in tests as an after thought, some of the risks include:

  • not fully testing your code
  • not completely trusting the tests
  • not building the habit of writing tests

That's just some. There are tons of downsides to not testing, and very few upsides. Having worked on plenty of large, real world projects without tests, let me tell you: it sucks.

Enough of the testing soapbox.

We have already added jest to our project in the initial project setup.

We also added in our Jest configuration to package.json, so we're good on that front to get started writing tests.

For this, I'm going to create a new top level directory:

mkdir __tests__

This directory will follow the same structure as src.

If we have a src/routes/healthcheck.ts file, then we should have a corresponding __tests__/routes/healthcheck.ts file.

mkdir -p __tests__/routes
touch __tests__/routes/healthcheck.test.ts

Into which we will add the following:

import server from "../../src/server";
import request from "supertest";

// close the server after each test
afterEach((done) => {
  server.close();
  done();
});

describe("routes/healthcheck", () => {
  it("should pong", async () => {
    const response = await request(server).get("/ping");
    expect(response.status).toEqual(200);
    expect(response.type).toEqual("application/json");
    expect(response.body.data).toEqual("pong");
  });
});

You can then run your tests with npm run test, and hopefully see the following:

npm run test

> koa-ts-tutorial@1.0.0 test /home/chris/Development/koa-ts-tutorial
> NODE_ENV=test PORT=7788 jest

ts-jest[versions] (WARN) Version 24.8.0 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=22.0.0 <24.0.0). Please do not report issues in ts-jest if you are using unsupported versions.
 PASS  __tests__/routes/healthcheck.test.ts
  routes/healthcheck
    ✓ should pong (37ms)

  console.log src/server.ts:3130
    Server listening on port: 7788

  console.log node_modules/koa-logger/index.js:52
      <-- GET /ping

  console.log node_modules/koa-logger/index.js:52
      --> GET /ping 200 4ms 34b

-----------------|----------|----------|----------|----------|-------------------|
File             |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------------|----------|----------|----------|----------|-------------------|
All files        |    92.59 |       50 |       75 |    92.31 |                   |
 routes          |    88.89 |      100 |      100 |     87.5 |                   |
  healthcheck.ts |    88.89 |      100 |      100 |     87.5 |                13 |
 src             |    94.44 |       50 |       50 |    94.44 |                   |
  config.ts      |      100 |       50 |      100 |      100 |                 6 |
  server.ts      |    94.12 |      100 |       50 |    94.12 |                26 |
-----------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.056s, estimated 3s
Ran all test suites.

If you're new to testing, why not take this opportunity to get the branch coverage of src/config.ts up to 100%?

Episodes