Integration tests and service fixtures
Prerequisite: node-coverage-and-test-quality. Objective: Verify local HTTP contract. Local baseline: Node.js 22.16.0 on Linux, ESM. Each example is independent: save as main.mjs in a disposable directory and run node main.mjs. The author harness uses a five-second timeout. Static snippets are not ByteMind interactive execution.
How it works
Bind to 127.0.0.1 and port 0 so the OS chooses a free port; wait for listening before issuing requests. Use timeouts, synthetic data and explicit teardown. In-process framework injection avoids real sockets, while Supertest or a real client may exercise a different boundary. Verify both successful and rejected requests. Database integration requires a real appropriate provider; a mock repository cannot establish SQL semantics.
Test a real local JSON endpoint
import {createServer} from 'node:http';import {once} from 'node:events';import assert from 'node:assert/strict';const server=createServer((req,res)=>{res.setHeader('content-type','application/json');if(req.url==='/items/7')res.end(JSON.stringify({id:7}));else{res.statusCode=404;res.end(JSON.stringify({error:'not_found'}));}});server.listen(0,'127.0.0.1');await once(server,'listening');try{for(const [path,status,body] of [['/items/7',200,{id:7}],['/items/8',404,{error:'not_found'}]]){const r=await fetch(`http://127.0.0.1:${server.address().port}${path}`,{signal:AbortSignal.timeout(1000)});assert.equal(r.status,status);assert.equal(r.headers.get('content-type'),'application/json');assert.deepEqual(await r.json(),body);console.log(status);}}finally{await new Promise((ok,fail)=>server.close(e=>e?fail(e):ok()));}Expected output:
200404This checks real HTTP serialization and routing, not authentication or deployment.
Test repository and service integration in memory
import assert from 'node:assert/strict';const rows=new Map();const repo={save:item=>rows.set(item.id,{...item}),find:id=>rows.get(id)};function add(item){if(repo.find(item.id))return 'conflict';repo.save(item);return 'created';}assert.equal(add({id:1,name:'Pen'}),'created');assert.equal(add({id:1,name:'Book'}),'conflict');assert.equal(repo.find(1).name,'Pen');console.log('one record',rows.size);Expected output:
one record 1Two components interact against a fake repository; actual database durability is not tested.
Common mistakes
Do not use a fixed port that may belong to another application, or point tests at a production service. An HTTP 200 without a body assertion misses contract regressions.
Practice
Add a request to an unknown item and assert both 404 and the error body.
Reference candidate: node-integration-tests-and-service-fixtures-lookup (route unverified). Documentation: https://nodejs.org/download/release/v22.16.0/docs/api/http.html. Selected versioned documentation opened.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Integration tests and service fixtures — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
Why bind to port 0?
What should a JSON endpoint test assert?
Can a Map fixture prove SQL transaction isolation?