Contract and end-to-end testing
Prerequisite: node-integration-tests-and-service-fixtures. Objective: Compare consumer expectation and response. 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
For a catalog API, a consumer might require an integer id, a string title and a nonnegative price in cents. Permit extra fields unless the contract forbids them. Version and test breaking changes explicitly. A local end-to-end test can create an item, fetch it and verify state; a browser or deployed environment is a different evidence class. Avoid asserting incidental JSON key ordering.
Check consumer-required fields
const accepts=x=>Number.isSafeInteger(x.id)&&typeof x.title==='string'&&Number.isSafeInteger(x.priceCents)&&x.priceCents>=0;for(const x of [{id:1,title:'Book',priceCents:300,extra:true},{id:1,title:'Book',priceCents:'300'},{id:1,title:'Book',priceCents:-1}])console.log(accepts(x));Expected output:
truefalsefalseAn additive field is compatible with this consumer; a type change or negative price is not.
Exercise create then read over real HTTP
import {createServer} from 'node:http';import {once} from 'node:events';import assert from 'node:assert/strict';const data=new Map();const server=createServer((req,res)=>{res.setHeader('content-type','application/json');if(req.method==='POST'&&req.url==='/items'){data.set(1,{id:1,title:'Notebook'});res.statusCode=201;res.setHeader('location','/items/1');res.end(JSON.stringify(data.get(1)));}else if(req.method==='GET'&&req.url==='/items/1'&&data.has(1)){res.end(JSON.stringify(data.get(1)));}else{res.statusCode=404;res.end('{}');}});server.listen(0,'127.0.0.1');await once(server,'listening');const base=`http://127.0.0.1:${server.address().port}`;try{const created=await fetch(base+'/items',{method:'POST',signal:AbortSignal.timeout(1000)});assert.equal(created.status,201);const item=await created.json();const fetched=await fetch(base+created.headers.get('location'),{signal:AbortSignal.timeout(1000)});assert.equal(fetched.status,200);assert.deepEqual(await fetched.json(),item);console.log('create-read',item.title);}finally{await new Promise(ok=>server.close(ok));}Expected output:
create-read NotebookThe intentionally fixed-data endpoint tests a narrow create-read flow over HTTP, not arbitrary inputs or persistent storage.
Common mistakes
Do not call a schema check a full end-to-end test, or require equality of all fields when consumers tolerate additions. A shape check does not prove authorization or semantic correctness.
Practice
Add a contract case that rejects negative price and another that accepts an optional description.
Reference candidate: node-contract-and-end-to-end-testing-lookup (route unverified). Documentation: https://nodejs.org/download/release/v22.16.0/docs/api/test.html. Selected versioned documentation opened.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Contract and end-to-end testing — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
Which change breaks the illustrated consumer contract?
What does an end-to-end test scope need?
What is still unverified in a fixed-data HTTP flow?