Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    NODENode.js

    Runtime and core tooling

    • What is Node.js?
    • CommonJS and ES modules
    • npm and package.json
    • process and command-line apps

    Async I/O and events

    • Event loop: callbacks to async/await
    • Files and paths with fs/promises
    • EventEmitter
    • Streams and backpressure

    HTTP and APIs

    • Your first HTTP server
    • Routing and JSON bodies
    • Build a tiny router and middleware chain
    • REST: resources and status codes
    • Validation and error handling

    Production: security, testing and deployment

    • Authentication basics with crypto
    • Configuration and environment secrets
    • Testing with assert and node:test
    • API security fundamentals
    • Project: tasks REST API and deployment
    Node.js

    Production: security, testing and deployment · Lesson 16 of 18

    Testing with assert and node:test

    Test behavior quickly and separate unit from integration tests without noisy output.

    12 minutes2 games

    A good test follows Arrange, Act, Assert and checks one clear behavior. node:assert/strict throws when an expectation fails, making it useful in scripts and exercises. node:test adds a built-in runner, subtests, hooks and TAP output without third-party packages.

    Arrange input

    Act

    Assert behavior

    Refactor confidently

    Node.js
    const assert = require("node:assert/strict")
    function total(items) {
      return items.reduce((sum, item) => sum + item.price * item.qty, 0)
    }
    assert.equal(total([]), 0)
    assert.equal(total([{ price: 5, qty: 2 }, { price: 3, qty: 1 }]), 13)
    assert.throws(() => { throw new TypeError("bad input") }, TypeError)
    console.log("all tests passed")

    A unit test isolates a fast function; an integration test checks components such as HTTP, database and files together. Do not mock so much that you only test implementation. Cover happy paths, boundaries and failures, keeping tests deterministic without real clocks or external networks.

    Node.js
    // Save as cart.test.js and run: node --test
    const test = require("node:test")
    const assert = require("node:assert/strict")
    
    test("adds two prices", () => {
      assert.equal(5 + 3, 8)
    })

    Important

    Test output is not a stable API; TAP includes variable timings. CI relies on the exit code. Tests are code too: review them, avoid shared state, and clean servers or files in finally or hooks even after failure.

    Quick question

    What does an integration test check?

    Interview question

    What is the test pyramid?

    Exercise

    Write divide(a,b) throwing RangeError when b is zero. Test 8/2 and the error, then print all tests passed.

    Expected output: all tests passed

    Node.js
    const assert = require("node:assert/strict")
    function divide(a, b) {
    
    }
    assert.equal(divide(8, 2), 4)
    assert.throws(() => divide(1, 0), RangeError)
    console.log("all tests passed")

    Learn more from

    • Node.js: Test runnernodejs.org
    • MDN: Testingdeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousConfiguration and environment secretsNextAPI security fundamentals
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.