Tests of the happy path ask for things that exist. Bugs like to hide on the other path, the error, because nobody tries it twice by hand. The shop has no product 99, so it must answer 404 Not Found with its JSON error. A 200 would tell the app that called the shop that everything is fine.
assert.strictEqual can't compare two objects: like ===, it calls them equal only when they are one and the same object. assert.deepStrictEqual compares what is inside them: it passes only when both have the same properties with the same values. Here is a check of a whole product, on another route:
const response = await client.get("/products/4");
assert.deepStrictEqual(response.body, { id: 4, name: "Pen", price: 2, category: "office" });
The request your new test sends, and what the shop answers:
GET /products/99 → 404 {"error": "Product not found"}
The check runs your tests on the shop and on two broken copies: one answers 200 for a missing product, the other answers 404 with Express's HTML page instead of JSON. The test "one product" passes on both. The output must be:
Your tests on the shop:
one product ... ok
missing product ... ok
2 tests: OK
Your tests on a copy that answers 200 for a missing product:
one product ... ok
missing product ... FAIL
AssertionError: 200 != 404
2 tests: 1 failed, so they caught this bug
Your tests on a copy that answers an HTML page for a missing product:
one product ... ok
missing product ... FAIL
AssertionError: {} != { error: 'Product not found' }
2 tests: 1 failed, so they caught this bug
Your task: below "one product", write a test named "missing product" with an async function: GET /products/99, check that the status is 404, and check with assert.deepStrictEqual that response.body equals { error: "Product not found" }.
Assignment
Follow the Coding Tutorial to write a test for a product that doesn't exist.
Hint
You need both checks: the status catches one broken copy, the body catches the other. When the body isn't JSON, response.body is an empty object, {}.
Free plan: 30 tutor messages. PRO is unlimited.
