Test what AI wrote
A passing test proves little unless it could have failed.
AI can write tests as quickly as code, and that creates a trap. A test written from the code can simply repeat the code's mistakes, and then a wrong answer shows up as a green tick.
Node.js
// This proves nothing: the real function is replaced by a fake
jest.mock("./price", () => ({ applyDiscount: () => 900 }));
test("10% off 1000 is 900", () => {
expect(applyDiscount(1000, 10)).toBe(900);
});Better habits
- Write down what should happen before you look at the code.
- Test the edges: zero, empty, negative, and too large.
- Test what must be refused, not only what must work.
- Break the code on purpose. If the test stays green, the test is useless.
Node.js
test("a discount over 100% is refused", () => {
expect(() => applyDiscount(1000, 110)).toThrow();
});