Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Write the Tests Before the Code

In this video, you watched task 3 of the escape-room builder’s unreachable-puzzle report done tests first: for each puzzle a team can’t reach, the report names the needs that no reachable puzzle provides. It’s an example session written for the video rather than recorded. Your own session will differ, but its steps should be the same.

Key points:

  • Task 1’s test held up because your task gave its expected values. Watch out: without them, tests an agent writes with its code come from its own reading of the task, so if it misread the task, they check the misreading and still pass.
  • You start with the answers, in your own words. In The Smuggler’s Cellar, the strongbox needs the iron key, and nothing unlocks it. The trapdoor needs two things. The map gives the cellar code, so a team finds that one, but the trapdoor code is inside the strongbox, which no team can open. So the trapdoor misses only the trapdoor code. The Lighthouse Keeper’s Study misses nothing. Those three lines are your criteria.
  • Your criteria go into your request, which asks for tests that check them and no code yet. In the example, the agent’s test calls a new function, missingNeeds(), which returns one entry per unreachable puzzle: its id and the needs it’s missing. Its first test checked the Lighthouse Keeper’s Study and the strongbox, but not the trapdoor.
  • Even with your criteria in the request, an agent can drop one, so it’s up to you to read the tests against your criteria before any code exists. Suppose the code counts a need as missing only when nothing in the room unlocks it. The strongbox unlocks the trapdoor code, so that version says the trapdoor misses nothing, and the first test still passes. So you asked for the missing criterion, and the test now expects both cellar puzzles.
  • The agent ran the tests, with no code for task 3 yet. The first run failed, but none of the file’s tests ran. The file stopped at its import, because the function it imports didn’t exist yet. Node counts the file that wouldn’t load as one failed test. That red says nothing about task 3.
  • So you asked for an empty version of the function, which returns an empty list. It leaves task 3 undone and only lets the file load. On the next run, the two earlier tests passed and the new test failed. It expected the strongbox and the trapdoor, and got an empty list. That’s the right reason to fail, because the behavior is missing.
  • You committed the tests, with the empty function. The new test still fails on purpose, and task 3’s box stays unticked. That commit records exactly what your tests expect. From then on, the tests should stay as they are. Nothing stops an agent from editing them, so you’ll check.
  • Then you asked for code that passes these tests, with the test files unchanged. The agent filled in the empty function, and all three tests passed. git status listed only src/check-room.js as changed, so the test file was as you committed it, unless the agent made a commit of its own. You don’t commit this code yet, because a green run doesn’t show how the tests passed. You’ll check that in Check What Made the Run Green.

Tests first runs in this order:

  1. Turn your criteria into tests.
  2. Read the tests against the criteria.
  3. See them fail because the behavior is missing.
  4. Commit them.
  5. Ask for the code that passes them, with the tests unchanged.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Task 3 is next on your task list. For each puzzle a team can’t reach, the report should name the needs that no reachable puzzle provides. Task 1’s test held up because your task gave its expected values.

00:15 Watch out: without them, tests an agent writes with its code come from its own reading of the task, so if it misread the task, they check the misreading and still pass.

00:26 Here, you’ll watch task 3 done the other way round, with tests first, then code.

00:34 Start with the answers, in your own words. In The Smuggler’s Cellar, the strongbox needs the iron key, and nothing unlocks it.

00:44 The trapdoor needs two things. The map gives the cellar code, so a team finds that one. The trapdoor code is inside the strongbox, which no team can open. So the trapdoor misses only the trapdoor code.

00:59 And The Lighthouse Keeper’s Study misses nothing. Those three lines are your criteria. They go into your request, which asks for tests that check them and no code yet.

01:12 Here’s how that might go, in an example session written for this video. Yours will differ, but its steps should be the same. The agent reads your request and the test file, and adds one test.

01:25 It calls a new function, missingNeeds(), which returns one entry per unreachable puzzle: its id and the needs it’s missing. The test expects nothing missing for the Lighthouse Keeper’s Study, and the iron key for the strongbox.

01:40 Before any code exists, read the test against your criteria. Your turn: pause the video and find the criterion this test doesn’t check. The trapdoor. Even with your criteria in the request, an agent can drop one, so this reading is yours.

02:00 Suppose the code counts a need as missing only when nothing in the room unlocks it. The strongbox unlocks the trapdoor code, so that version says the trapdoor misses nothing, and this test still passes.

02:13 So you ask for the missing criterion, and the test now expects both cellar puzzles.

02:21 Next, the agent runs the tests. With no code for task 3, they should fail. This first run fails, but none of the file’s tests ran. The file stops at its import, because the function it imports doesn’t exist yet.

02:37 Node counts the file that wouldn’t load as one failed test. That red says nothing about task 3.

02:45 So you ask for an empty version of that function, which returns an empty list. It leaves task 3 undone and only lets the file load. On the next run, the two earlier tests pass, and the new test fails.

02:59 It expected the strongbox and the trapdoor, and got an empty list. That’s the right reason to fail, because the behavior is missing.

03:10 Now you commit the tests, with the empty function. The new test fails on purpose, and task 3’s box stays unticked. That commit records exactly what your tests expect.

03:23 From here on, the tests should stay as they are. Nothing stops an agent from editing them, so you’ll check.

03:31 Then you ask for code that passes these tests, with the test files unchanged.

03:37 The agent fills in the empty function, and runs the tests again. All three pass. git status lists only the checker as changed. So the test file is as you committed it, unless the agent made a commit of its own.

03:54 You don’t commit this code yet, because a green run doesn’t show how the tests passed. You’ll check that in Check What Made the Run Green.

04:04 So tests first runs in this order. Turn your criteria into tests. Read the tests against the criteria. See them fail because the behavior is missing. Commit them.

04:18 Then ask for the code that passes them, with the tests unchanged.

04:23 Next, in Run a Tests-First Task, you’ll run this cycle on task 3 with your own agent.

Become a Member to join the conversation.