Testing

Contract and end-to-end testing

A contract test checks the agreement between a consumer and provider: inputs, status, required fields and their meaning. End-to-end testing exercises an entire chosen workflow through its real boundaries. 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. 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. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/test.html.

Formal Syntax & Structure

nodejs syntaxnodejs
assert.ok(predicate); assert.deepEqual(actual, expected)

Working Example

Example Snippetnodejs
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));
Node.js Reference