Testing met 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.
Installeer JEST
npm install jest --save-dev
...
"scripts": {
"start:dev": "nodemon ./src/app.js --config nodemon.json",
"test": "jest --coverage"
},
...
npm run test
...
"scripts": {
...
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest --coverage"
},
...
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);
});
Testing met JEST
__tests__ folder
describe(...)
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
npm install supertest --save-dev
import request from "supertest";
request testing
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
'typeorm' does not provide an export named 'getConnection'
'typeorm' does not provide an export named 'createConnection'
import typeorm from "typeorm";
const { createConnection, getConnection } = typeorm;