API tests
Open live editorDiscover → Predict → Understand
What evidence shows that api tests keeps state and application behavior understandable?
HTTP tests must run against a real server when claiming transport coverage. The exported Node tests use temporary files and actual loopback requests.
- Explain the operation and its responsibility.
- Predict actual values before execution.
- Identify local calculation, actual DOM and external execution boundaries.
Conceptual application flow
- User intent
- Action / handler
- Business logic
- State
- Render / persistence
Five source examples
Working contract
HTTP tests must run against a real server when claiming transport coverage. The exported Node tests use temporary files and actual loopback requests.
async function main(input) {
return input.map(x=>({case:x.name,pass:x.actualStatus===x.expectedStatus}));
}Input
[
{
"name": "missing route",
"actualStatus": 404,
"expectedStatus": 404
}
]Fixed expected result
[
{
"case": "missing route",
"pass": true
}
]Observe input preservation
The wrapper records the original input and compares it after the operation. Some lessons intentionally demonstrate mutation in a local copy.
async function calculate(input){
return input.map(x=>({case:x.name,pass:x.actualStatus===x.expectedStatus}));
}
async function main(input){const before=structuredClone(input);const result=await calculate(input);return {result,inputUnchanged:JSON.stringify(input)===JSON.stringify(before)};}Input
[
{
"name": "missing route",
"actualStatus": 404,
"expectedStatus": 404
}
]Fixed expected result
{
"result": [
{
"case": "missing route",
"pass": true
}
],
"inputUnchanged": true
}Investigate the discarded result
An early return bypasses the intended operation. Repair the first incorrect boundary rather than changing the expected result.
async function calculate(input){
return input.map(x=>({case:x.name,pass:x.actualStatus===x.expectedStatus}));
}
async function main(input){return null; // Deliberate early return discards calculation
return await calculate(input);}Input
[
{
"name": "missing route",
"actualStatus": 404,
"expectedStatus": 404
}
]Fixed expected result
[
{
"case": "missing route",
"pass": true
}
]Repeat the deterministic calculation
Invoke the same declared pure/model operation twice and compare both results. This is not an external idempotency guarantee.
async function calculate(input){
return input.map(x=>({case:x.name,pass:x.actualStatus===x.expectedStatus}));
}
async function main(input){return [await calculate(structuredClone(input)),await calculate(structuredClone(input))];}Input
[
{
"name": "missing route",
"actualStatus": 404,
"expectedStatus": 404
}
]Fixed expected result
[
[
{
"case": "missing route",
"pass": true
}
],
[
{
"case": "missing route",
"pass": true
}
]
]Frozen regression contract
Execute the operation and compare with a fixed fixture result. Changing the calculation must not silently rewrite the expectation.
async function calculate(input){
return input.map(x=>({case:x.name,pass:x.actualStatus===x.expectedStatus}));
}
async function main(input){const actual=await calculate(input);const expected=[{"case":"missing route","pass":true}];if(JSON.stringify(actual)!==JSON.stringify(expected))throw Error("Regression contract mismatch");return {pass:true,actual};}Input
[
{
"name": "missing route",
"actualStatus": 404,
"expectedStatus": 404
}
]Fixed expected result
{
"pass": true,
"actual": [
{
"case": "missing route",
"pass": true
}
]
}Important operations
[
{
"line": 1,
"source": "async function main(input) {",
"explanation": "Declare the named operation or local value and follow its scope in the source."
},
{
"line": 2,
"source": "return input.map(x=>({case:x.name,pass:x.actualStatus===x.expectedStatus}));",
"explanation": "Create a new array from declared inputs; inspect which nested references are retained."
},
{
"line": 3,
"source": "}",
"explanation": "Declare the named operation or local value and follow its scope in the source."
}
]Guided mission
- Discover the problem and inspect source/input.
- Write a prediction. Run the actual source and inspect the returned value.
- Change one requirement and state the new expected result independently.
- Load the repair exercise, reproduce the failure and use hints.
- Restore the authored fixture and Check, then explain and extend.
Three hints
- Locate the input, transition and returned result before editing.
- Compare the first differing field and trace which expression produced it.
- Remove the deliberate early return and restore the named responsibility; compare with Finished Code only after trying.
Complete solution
async function main(input) {
return input.map(x=>({case:x.name,pass:x.actualStatus===x.expectedStatus}));
}Common mistake: Changing the expected result to match a defect instead of repairing the behavior.
Invalid JSON is rejected before execution. Syntax and thrown errors are shown. Model kernels for DOM, HTTP and Node teach contracts; use the actual browser and exported server projects for those execution boundaries.
Reflect and extend
Which returned value or actual DOM observation supports your explanation? Which side effect was outside this check?
Create a second normal, boundary and invalid case. Write exact expected values before execution and explain what the provided fixture does not prove.
AI prompt goals
- Explain the current source, input, actual output and architecture boundary.
- Help refactor one responsibility while preserving independently expected behavior.
- Investigate the current error and propose normal, boundary and invalid tests.