Lesson 6 / الدرس 6
Assertions with teeth / توكيدات لها أنياب
An assertion is where your judgement about what is correct becomes executable. Write it too loosely and the test passes forever while the product decays; too tightly and it fails every week for reasons nobody cares about.
التوكيد هو حيث يصير حكمك على ما هو صحيح قابلًا للتنفيذ. اكتبه فضفاضًا ينجح الاختبار إلى الأبد بينما يتحلل المنتج؛ واكتبه ضيّقًا يرسب كل أسبوع لأسباب لا يبالي بها أحد.
Everything before the assertion is setup. The assertion is the test. It is the single line where "this is what correct means" stops being an opinion in your head and becomes something a machine will enforce at three in the morning — which is why a weak one is so expensive: it enforces almost nothing, convincingly.
Too loose, too tight, and right
| Assertion | Problem |
|---|---|
| status is not 500 | Passes on 404, 401, 302 — almost anything |
| the body contains "order" | Passes on "order not found" |
| the whole body equals this exact JSON | Fails when an unrelated field is added |
| status is 201 and body.id is a number and body.total is 550 | Right: specific about what matters, silent about the rest |
The first two are the common failure and they look like tests. The third is the over-correction, and it is why some suites need repairing after every release.
// Weak. Green forever, and it is checking that a server exists.
expect(res.status).not.toBe(500);
expect(body).toBeTruthy();
// Brittle. Red the day someone adds a field nobody asked you about.
expect(body).toEqual({ id: 44, total: 550, currency: 'EGP', createdBy: 'sara' });
// Right. Names what the rule promised, ignores what it did not.
expect(res.status).toBe(201);
expect(typeof body.id).toBe('number');
expect(body.total).toBe(550); // 500 + 10%, the rule under test
expect(body.password).toBeUndefined(); // and nothing that must never appear
550 means nothing to a reader six months from now, and // 500 + 10% turns a magic number back into the rule it came from. 550 لا تعني شيئًا لقارئ بعد ستة أشهر، و// 500 + 10% تُعيد الرقم السحري إلى القاعدة التي جاء منها.Prove the assertion can fail
A test you have only ever seen pass is a test you have not finished. Break it on purpose once: change the expected total to a wrong number and watch it go red, then change it back. That thirty seconds is the difference between a test and a decoration, and it is the single habit that most separates people who trust their suite from people who quietly do not.
expect(res).toBeTruthy() على كائن صحيح دائمًا. وexpect(page.locator(".error")) بلا مطابِق لا يوكّد شيئًا البتة — فهو قيمة لا فحص. وكلاهما يبدو اختبارًا في الفرق، وكلاهما أخضر إلى الأبد.Check yourself / اختبر نفسك
1. What is wrong with asserting only that the status is not 500?
It checks that a server exists, not that it did the right thing. A redirect to the sign-in page, a not-found and an unauthorised all sail through, so the test is green while the feature is entirely broken.
2. Why is asserting the entire response body equal to one exact object usually a mistake?
It asserts far more than the requirement said. Every extra field it pins down is a future red build caused by a change that was perfectly correct — and repeated false alarms are how a team learns to ignore its suite.
3. Why break a passing test on purpose?
Assertions that cannot fail are green forever and read like real checks in a diff. Deliberately making it red is the only cheap proof that the assertion has teeth.
Score / النتيجة: 0 / 3