Testing met JEST

Testing met JEST

Jest

 

Jest is a delightful JavaScript Testing Framework with a focus on simplicity.

Designed to ensure correctness of any JavaScript codebase. It allows you to write tests with an approachable, familiar and feature-rich API that gives you results quickly.

Installatie

Installeer JEST

npm install jest --save-dev

Voorbereiding

	
...
"scripts": {
    "start:dev": "nodemon ./src/app.js --config nodemon.json",
    "test": "jest --coverage"
},
...
  • verander het test-script in package.json
  • aanroepen is nu mogelijk via
   npm run test

⚠️ EcmaScript Modules & Jest

...
"scripts": {
    ...
    "test": "NODE_OPTIONS=--experimental-vm-modules npx jest --coverage"
},
...
Tests schrijven
  • Maak een Utils-folder en steek daarin een file Sum.js 
  • ontwikkel daarin een functie die een som van twee getallen met mekaar optelt en retourneert.
    exporteer die functie
  • Om die functie te testen, hebben we een Unit Test nodig, die specifiek die functie zal testen op correcte werking
  • Een test-file heeft altijd de extensie .test.js
    vb. Sum.test.js

 

 

 

Tests schrijven
import sum from "./Sum.js";

test("1 + 2 to be equal to 3", () => {
  expect(sum(1, 2)).toBe(3);
  expect(sum(5, 6)).toBeGreaterThan(9);
});
  • Schrijf ook functies en tests voor volgende scenario's
    • functie die een array kloont
    • functie die lege waardes uit een array verwijdert

Testing met JEST

HTTP-endpoint testing

__tests__ folder

  • Als we de volledige applicatie willen testen op correcte werking, gaan we te tests in een apart bestand plaatsen.
  • Deze worden gegroepeerd in een  __tests__  directory

 

 

 

describe(...)

  • Je kan tests groeperen via de describe()-methode
  • daarbinnen kan je een gemeenschappelijke setup declareren
import "dotenv/config";
import typeorm from "typeorm";
import entities from "../models/index.js";

describe("Interests APP tests", () => {
  beforeAll(async () => {
    await typeorm.createConnection({
      type: process.env.DATABASE_TYPE,
      database: process.env.DATABASE_NAME,
      entities,
      synchronize: true,
    });
  });

  afterAll(async () => {
    await typeorm.getConnection().close();
  });
});

request testing

 

  • importeer de library vervolgens in je test-file
npm install supertest --save-dev
import request from "supertest";

request testing

  • Een voorbeeld van een test op endpoint /api/interest zou er zo kunnen uitzien
describe("Interests APP tests", () => {

  beforeAll(...);
  
  afterAll(...);

  describe("API tests", (done) => {
    test("GET - /api/interest", async () => {
      const response = await request(app).get("/api/interest");
      expect(response.statusCode).toBe(200);
    });
  });
  
});

⚠️ named exports in Jest 

  • ES-modules werkt voorlopig nog experimenteel
  • De named exports van TypeOrm worden niet goed geïnterpreteerd door Jest.
    • Als je deze fout tegenkomt:

 

 

  • dan moet je overal in je app de imports refactoren naar
'typeorm' does not provide an export named 'getConnection'
'typeorm' does not provide an export named 'createConnection'
import typeorm from "typeorm";
const { createConnection, getConnection } = typeorm;

Testing met JEST

By Frederick Roegiers

Testing met JEST

  • 91