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

Replacing Fixtures With Hooks

Replacing Fixtures With Hooks

Pytest requests a fixture by parameter name. A node:test callback receives a test context instead, whatever its parameter is called. The conventional name is t; it doesn’t request test data.

Use beforeEach() to build a fresh Morag booking before each test in this file. A surrounding let binding lets the setup callback replace the object and the test callbacks read it. This gives each fitting check independent starting data:

Language: JavaScript Filename: fitting.test.js
import assert from "node:assert/strict";
import { beforeEach, test } from "node:test";
import { fit } from "./fitting.js";

let morag;

beforeEach(() => {
  morag = {
    name: "Morag Fraser", height: 168, weight: 62,
    boot: 6, ability: "intermediate",
  };
});

test("afternoon booking", () => {
  morag.ability = "expert";
  assert.strictEqual(fit(morag).skis, 160);
});

test("fit", () => {
  assert.deepStrictEqual(fit(morag), {
    name: "Morag Fraser", skis: 155,
    boots: 24.5, din: 5,
  });
});

Run the file from your existing ski-fitting folder with this command. The reporter option selects the readable report used in the video:

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 You’ll give each Snowline Hire test a fresh booking. Kirsty’s Python fixture returns Morag’s data, and pytest supplies it to tests that request its name.

00:11 This fixture has the default function scope. Each test gets a fresh dictionary, so making Morag an expert for an afternoon check leaves the next check unchanged.

00:22 In JavaScript, your fitting test currently builds Morag’s object inside its body. You need that same starting data in more than one test. In Python, the test asks for the fixture through its parameter.

00:38 A direct JavaScript port gives the callback that same parameter name and keeps the contents assertion. Will this JavaScript test receive Morag’s booking and pass?

00:48 Your turn: pause the video.

00:56 It fails. The fitting error says the ability is undefined. This excerpt omits the stack and surrounding report. Something other than the booking reached the fitting function.

01:11 Inspect the argument in JavaScript by logging its .name property. It prints fit, the test’s own name. The runner passes a test context to the callback.

01:23 Renaming the parameter doesn’t change that argument. It’s an object for working with the running test, not Morag’s booking. The usual parameter name is t.

01:36 There’s no fixture lookup by name here, so build the booking yourself.

01:42 Add beforeEach() to the named import from the built-in test module. Put a let binding outside the tests because the setup will replace its value.

01:53 Give beforeEach() a callback that assigns Morag’s object.

02:02 The runner calls that function before each test in this file. This is a setup hook. The test callback reads the surrounding variable, a closure. Remove its fixture parameter.

02:18 Your contents assertion can now check the actual fitting.

02:33 The afternoon test changes Morag’s ability to expert and checks for 160-centimeter skis. That mutation belongs to this test.

02:47 Before the next test, the hook builds a new intermediate booking. The whole-fitting check still expects 155-centimeter skis, 24.5 boots, and a binding setting of five.

03:00 Run the file. Both tests pass. The report counts two tests; the timings vary. The fresh object, not a fixture parameter, keeps their starting data independent.

03:16 If only some tests need Morag, use a plain helper instead. This JavaScript function returns a new booking each time you call it. Inside the test, call makeMorag() and keep its result in a local constant.

03:32 That request is explicit; the runner doesn’t inspect the variable’s name. Choose the hook for common setup, or the helper for selected tests. The helper is an alternative here, not another step to add to the hook.

03:50 For this file, before() runs once before its tests, and after() runs once when they finish; afterEach() runs after every test, alongside beforeEach() before every test.

04:02 Put those hooks at file level to surround this file’s tests. Setup and cleanup can also belong to a group when hooks are declared inside describe().

04:13 That timing resembles choosing a Python fixture’s scope. The analogy stops at timing: hooks run callbacks, and don’t resolve or inject fixture values.

04:27 The test context has a use of its own. In this JavaScript test, name the parameter t and register cleanup with t.after(). Pass a callback, so cleanup runs when this test ends.

04:41 This demonstration’s cleanup logs that the workspace was released. The test body prints first. Then cleanup runs. That’s the role of code after a Python fixture’s yield.

04:56 Here, cleanup is attached explicitly to this test; it isn’t discovered through a fixture name.

05:05 Give each test fresh data explicitly: use a hook for shared setup, or call a helper where you need it. A test parameter is the context, and its cleanup hook belongs to the test’s end.

05:18 In the next lesson, Testing Async Code With assert.rejects(), you’ll check loaded bookings from a temporary file and clean it up afterward.

Become a Member to join the conversation.