Reading Code with AI as Your Explainer
Why This Matters
To judge a test, you must be able to read it. Not fluently, and not alone: an AI assistant is an excellent, patient explainer. But you have to know which questions to ask, and how to check that the explanation is true. That is the skill of this lesson.
Which language?
The question used to be “Java, Python or JavaScript?”. Now it matters less: AI can write in all of them, and the testing concepts are the same. In this course you read TypeScript, because TestRun Board and Playwright use it. The reading skills transfer to any language.
Read a function
Open src/lib/runs.ts in TestRun Board:
/** Formats a duration for the table: 0.4s, 42s, 1m 03s. */
export function formatDuration(ms: number): string {
if (!Number.isFinite(ms) || ms < 0) {
throw new RangeError(`Invalid duration: ${ms}`)
}
if (ms < 10_000) {
return `${Math.round(ms / 100) / 10}s`
}
const totalSeconds = Math.round(ms / 1000)
if (totalSeconds < 60) {
return `${totalSeconds}s`
}
const minutes = Math.floor(totalSeconds / 60)
const seconds = totalSeconds % 60
return `${minutes}m ${String(seconds).padStart(2, '0')}s`
}
You can already understand a lot without knowing TypeScript:
- The comment at the top says what it is for, with examples.
ms: numbermeans the input is a number of milliseconds;: stringmeans it returns text.if (...)blocks are decisions. There are four paths: invalid input, under 10 seconds, under a minute, and a minute or more.throwmeans “stop with an error”.
Ask the AI the right questions
Good questions make the AI a tutor instead of a shortcut:
Explain formatDuration in src/lib/runs.ts line by line for someone who has never programmed.
Then list every different kind of input it handles, with one example input and the output for each.
Then check the explanation against reality. Ask for concrete examples and verify them with the test file tests/lib/runs.test.ts, which contains the real expected outputs (formatDuration(63_000) → "1m 03s"). If the AI’s example disagrees with the test, one of them is wrong, and finding out which is real testing work.
Read a test
it('shows minutes and zero-padded seconds from one minute', () => {
expect(formatDuration(63_000)).toBe('1m 03s')
expect(formatDuration(600_000)).toBe('10m 00s')
})
Every test has the same shape: a name that says which behaviour it checks, and expectations: expect(what happens).toBe(what should happen). Learn to read that shape and you can review most tests in any language.
Exercise
- Ask your AI assistant to explain
formatDurationwith the prompt above. - Compare its list of input kinds with the four paths in the code. Did it miss any?
- Read
tests/lib/runs.test.ts. For each test, write in one sentence what behaviour it protects. - Find a path in
formatDurationthat no test checks exactly at its boundary. (Hint: what happens at exactly 10 000 ms?) Keep the answer for lesson 7, Test Cases Before Code.
Key Takeaways
- You need to read code, not write it fluently; TypeScript reading skills transfer to other languages.
- Comments, types and
ifblocks tell you most of what a function does. - Ask the AI to explain and to give examples, then check the examples against the real tests.
- Every test is a name plus expectations:
expect(actual).toBe(expected).