Asserting Existence with Mocha, Chai and TypeScript Strict Null Checks

1 · Daniel Doubrovkine (dB.) @dblockdotorg · July 18, 2019, 5:24 p.m.
I recently encountered a unit test that looked like this. describe("Spline", () => { const spline = new Spline(); it("can be reticulated", () => { const reticulatedSpline = spline.reticulate(); if (! reticulatedSpline) { throw new Error('missing a spline'); } expect(reticulatedSpline.reticulatedCount).to.eq(1); }); }); The use of branching and conditionals in tests is an anti-pattern since we want tests to be predictable, each test to focus on a single code execution...