CodeBlanks Logo Icon CodeBlanks

Summary - Advanced testing

Combine integration, supertest and coverage to write an API test suite.

Summary - Advanced testing

In this group you learned the advanced test tools of Node.js: writing tests with test and expect, testing Express in memory with supertest, protecting production with a test database, measuring coverage with jest --coverage, and writing E2E tests with Playwright.

Now you will combine these into a small test skeleton. The skeleton will contain a test call, an expect assertion, and a beforeEach cleaning step. This mini task shows how the test tools combine.

Test design

A test skeleton is this: cleaning with beforeEach, a test definition with test, an assertion with expect. In integration tests, an HTTP request is sent with supertest.

Flow: beforeEach(() => { /* cleaning */ }); test("user creation", () => { const result = createUser({ name: "Ayşe" }); expect(result.name).toBe("Ayşe"); });. This skeleton cleans before each test, defines a test, and asserts the result. In production, supertest is added for integration tests. On top of the test skeleton you can add coverage targets, CI/CD integration and E2E scenarios. This is the foundation of reliable software delivery.

Practice Build the test skeleton

Define beforeEach(() => { console.log("Cleaned"); }). Define test("example", () => { expect(1+1).toBe(2); }). Print "Test system ready" with console.log.

Starter code

index.js
// Define beforeEach and test
// write here

// Print "Test system ready"
// write here

Sample solution

index.js
beforeEach(() => {
  console.log("Cleaned");
});
test("example", () => {
  expect(1+1).toBe(2);
});
console.log("Test system ready");

Expected output

Test system ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What pieces does the test skeleton combine?

index.js
test + expect + beforeEach
  • Only require
  • test, expect and beforeEach Correct answer
  • Only console.log
  • Only coverage

Explanation: test + expect + beforeEach combine.

2 Match each tool to its role

Term Meaning
test Defines a test
expect Does assertions
beforeEach Cleans before each test
supertest Tests Express in memory

All lessons in Advanced