CHAPTER 05 / WEBFRONT
Test and check
Jasmine, type checking and useful failures.
A first testable contract
In the terminal, run wf install jasmine-core@7.0.2. Create tests/life.spec.js in life-project with this grid-format test. It checks JavaScript packing, rather than the C++ computation itself.
describe('the Life grid', () => {
it('packs 1024 cells into 128 bytes', () => {
const cells = new Uint8Array(32 * 32 / 8);
for (const i of [330, 331, 332])
cells[i >> 3] |= 1 << (i & 7);
expect(cells.length).toBe(128);
expect(cells[41]).toBe(0b00011100);
});
});Declare and run
Add this property at the root of webfront.project.json, with the required JSON commas. wf init does not create this recipe. Run wf test: Jasmine runs in the window, and a failing spec produces a non-zero status. Temporarily replace 128 with 127 to check that your test fails, then restore it.
"test": {"specs": ["tests/life.spec.js"]}Check the types
Create tsconfig.json at the root of life-project with this configuration. Then install the types and compiler using the following commands. wf check uses the project’s locked TypeScript version.
{
"compilerOptions": {
"strict": true,
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
"jsx": "react-jsx",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"noEmit": true
},
"include": ["ui"]
}Install the type checker
The starter imports React and React DOM: their type declarations are needed for strict checking.
wf install typescript@5.9.3 @types/react@19.3.0 @types/react-dom@19.3.0
wf checkQuestion