Test layout

Mirror src layout

Give every source file a twin. When src holds math.ts, tests hold math.test.ts with the same folder nesting: src/math.ts pairs with tests/math.test.ts, and src/shop/cart.ts pairs with tests/shop/cart.test.ts. A reader finds the test for any module without searching, and a new module reminds its author to add the twin.

The mirror also keeps moves cheap. Rename a source file and its test follows the same path, so no index of test locations needs updating.

Name tests .test.ts

End every test file with .test.ts and the separation stays visible forever. The suffix tells readers and tools apart at a glance: one pattern selects the whole suite, and compiled math.test.js never collides with product math.js. Name the twin after its module, so cart.test.ts always tests cart.ts:

ls tests

math.test.ts

One name, one module, zero detective work.

Compile then run with node

No runner is needed here. Compile the test files with tsc, then run the emitted JavaScript with node. A thrown assert fails the run with a non-zero exit, so silence plus green output means the suite passed:

tsc -p tsconfig.json && node dist/tests/math.test.js

pass: doubles two

tsc prints nothing on success and node runs the compiled test. The check helper from the previous lesson prints its pass line, which proves the whole chain from TS source to green output.

Isolate with fresh imports

Tests share nothing when each file imports the module under test and builds fresh values. Never import one test file from another, and never reuse a live object across files. Shared setup may live in a plain helper module, but test state starts empty in every file:

import assert from "node:assert/strict"; import { add } from "../src/math.js"; const result: number = add(2, 3); assert.equal(result, 5); console.log("add(2, 3) = 5");

add(2, 3) = 5

The program prints the sum it checked, so the run proves the import found the module and the assert agreed with it. The js suffix in the import looks odd inside a ts file, but it names the file Node loads after compile time. Keep the suffix and plain tsc plus node runs stay boring and green.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where does the test for src/shop/cart.ts live when tests mirror the source layout?

    Answer

    tests/shop/cart.test.ts

    The test keeps the folder nesting of its module and adds the .test.ts suffix, so source and test sit at the same relative path.

  2. Why end every test file with .test.ts?

    Answer

    One pattern selects the suite, and the compiled name never collides with the product file

    The suffix separates tests from product code by name, so math.test.js and math.js can sit in one output folder.

  3. How does this course run a test without a test runner?

    Answer

    Compile the test files with tsc, then run the emitted JavaScript with node

    The compiler erases the types into JavaScript, and node runs that output, so a thrown assert fails the process with a non-zero exit.

  4. Why write the import as "../src/math.js" inside a .ts test file?

    Answer

    The compiled test loads a JavaScript file, so the path names the file Node finds

    After compilation the test is JavaScript, and Node resolves the import to the emitted math.js, so the suffix names the file that really loads.

  5. What does this program print? import { basename } from "node:path"; const twin = "tests/shop/cart.test.ts"; console.log(basename(twin));

    Answer

    cart.test.ts

    basename drops the folders and keeps the last part of the path, which is the test file name.

  6. What does this program print? const file = "math.test.ts"; console.log(file.replace(".ts", ".js"));

    Answer

    math.test.js

    The first .ts is replaced by .js, so the compiled twin keeps the test name and changes only its extension.

  7. What does this program print? const name = "math.ts"; console.log(name.endsWith(".test.ts"));

    Answer

    false

    A product file ends with .ts but not with .test.ts, so the check reports false and the file is not part of the suite.

  8. What does this program print? import assert from "node:assert/strict"; function double(n: number): number { return n * 2; } function check(name: string, fn: () => void): void { fn(); console.log("pass: " + name); } check("doubles two", () => { assert.equal(double(2), 4); }); check("doubles zero", () => { assert.equal(double(0), 0); });

    Answer

    pass: doubles two / pass: doubles zero

    Both asserts pass, and each check prints its own name in the order of the calls.

  9. What does the compiler say about this program? const expected: string = 5; console.log(expected);

    Answer

    error TS2322: Type 'number' is not assignable to type 'string'.

    The annotation promises a string while the initializer is a number, so the assignment is rejected.

  10. Why should one test file never import another test file?

    Answer

    Importing a test runs its checks and shares its state with the importer

    A test file is an entry point, and importing it runs its asserts inside another suite and lets objects from one file reach the other.

Article author: Arthur Isaev