Skip to content

Active user tests

Testing · Difficulty 1/5 · TypeScript · 24 linesOrigin: Fictional

Task

What the AI was asked to do

Write unit tests for activeUsers(users). The function returns only the active users, keeping their order, and leaves the input list unchanged.

Handoff note

What the AI wrote when it handed over the code:

“Three tests: every user returned is active, the empty list, and the input staying unchanged. The function's whole contract is covered.”

The code the AI wrote

TypeScript24 lines

Mark the lines you think are wrong; give each mark a severity and a one-sentence reason.

Click or tap a line. With a keyboard, move to the line and press Enter.

0 of 5 marks
How to pick a severity
  • CriticalData loss, a security hole or lost money
  • MajorThe user gets a wrong result in a case the task asks for
  • MinorA rare edge case outside the task, or readability

In the Testing topic, severity is set by the effect of the broken implementation the test doesn't catch.

Your reason isn't graded; you'll compare it with a sample answer.

import assert from "node:assert/strict";
import type * as Users from "./subject.ts";
3
const makeUsers = (): Users.User[] => [
  { name: "Ada", active: true },
  { name: "Linus", active: false },
  { name: "Grace", active: true },
];
9
export function testReturnsOnlyActiveUsers({ activeUsers }: typeof Users) {
  for (const user of activeUsers(makeUsers())) {
    assert.equal(user.active, true);
  }
}
15
export function testEmptyList({ activeUsers }: typeof Users) {
  assert.deepEqual(activeUsers([]), []);
}
19
export function testLeavesInputUnchanged({ activeUsers }: typeof Users) {
  const input = makeUsers();
  activeUsers(input);
  assert.deepEqual(input, makeUsers());
}

The test that proves it

The right option is the test that fails on this code, which means it exposes the bug.

Test options
Mark at least one line to submit.

Submitting sends seven pieces of information to a counter; your reason text stays on your device. Data