How to Test AI-Generated JavaScript: A Cart Total Example
Check AI-generated JavaScript against a clear contract using a cart subtotal example, integer cents, invalid inputs, overflow checks, and runnable assertions.

In this guide
Before accepting AI-generated JavaScript, write down what the function must do and what it must reject. A short test suite based on those requirements can reveal mistakes that a convincing explanation misses.
This tutorial builds a teaching cart subtotal, using integer cents and whole-item quantities. It excludes tax, shipping, discounts, refunds, currency conversion, and payment processing. Those omissions are part of its explicit contract.
1. Define the contract before asking for code
Use this specification with your coding assistant:
Write cartTotalCents(items) in JavaScript. Accept an array of items with unitPriceCents as a nonnegative safe integer and quantity as a positive safe integer. An empty array returns zero. Reject malformed items, numeric strings, fractions, and totals outside the safe-integer range. Do not mutate input. Do not add a dependency. Include tests derived from these requirements.
The important decisions are already visible. “19.99” is not an accepted price representation; 1999 is. Quantity zero is invalid in this exercise. If your product allows it, change both the contract and the expected behavior before accepting a patch.
2. Read a small reference implementation
function cartTotalCents(items) {
if (!Array.isArray(items)) throw new TypeError('Expected an array');
let total = 0;
for (const item of items) {
if (!item || typeof item !== 'object') {
throw new TypeError('Invalid item');
}
const { unitPriceCents, quantity } = item;
if (!Number.isSafeInteger(unitPriceCents) || unitPriceCents < 0 ||
!Number.isSafeInteger(quantity) || quantity < 1) {
throw new TypeError('Invalid price or quantity');
}
const lineTotal = unitPriceCents * quantity;
if (!Number.isSafeInteger(lineTotal) ||
!Number.isSafeInteger(total + lineTotal)) {
throw new RangeError('Total exceeds safe integer range');
}
total += lineTotal;
}
return total;
}
Number.isSafeInteger checks whether a value is an integer within JavaScript's safe integer range. It does not convert a numeric string into a number. That suits this deliberately strict input contract. Source: MDN's Number.isSafeInteger reference.
3. Test independently chosen cases
Save the function above as cart-total.cjs, then append the following checks to the same file. Run node cart-total.cjs.
const assert = require('node:assert/strict');
assert.equal(cartTotalCents([]), 0);
assert.equal(cartTotalCents([
{ unitPriceCents: 1999, quantity: 2 },
{ unitPriceCents: 500, quantity: 1 }
]), 4498);
assert.equal(cartTotalCents([{ unitPriceCents: 0, quantity: 1 }]), 0);
assert.throws(() => cartTotalCents(null), TypeError);
assert.throws(() => cartTotalCents([null]), TypeError);
assert.throws(() => cartTotalCents([
{ unitPriceCents: '1999', quantity: 1 }
]), TypeError);
assert.throws(() => cartTotalCents([
{ unitPriceCents: 1999, quantity: 1.5 }
]), TypeError);
assert.throws(() => cartTotalCents([
{ unitPriceCents: Number.MAX_SAFE_INTEGER, quantity: 2 }
]), RangeError);
const input = Object.freeze([
Object.freeze({ unitPriceCents: 250, quantity: 2 })
]);
assert.equal(cartTotalCents(input), 500);
assert.deepEqual(input, [{ unitPriceCents: 250, quantity: 2 }]);
console.log('Cart checks passed');
The 4498-cent case has a manually computed answer: 1999 multiplied by two, plus 500. Keep expected values independent of the implementation. A test that recomputes its expectation with the same faulty formula can approve the same mistake twice.
