0
0
DenoHow-ToBeginner ยท 4 min read

How to Use Test Steps in Deno for Clearer Testing

In Deno, use t.step() inside a test function to create test steps, which are smaller units of a test that run sequentially and show detailed results. Each step can be async and helps organize complex tests by breaking them into clear, named parts.
๐Ÿ“

Syntax

The t.step() method is called inside a test callback function. It takes two arguments: a string name describing the step, and a callback function that contains the step's test code. The callback can be async if needed.

This creates a nested test step that runs in order and reports its own pass/fail status.

typescript
Deno.test("main test", async (t) => {
  await t.step("step name", async () => {
    // step test code here
  });
});
๐Ÿ’ป

Example

This example shows a test with two steps: one checks if a number is positive, the other checks if it is even. Each step runs separately and reports its result.

typescript
Deno.test("Number checks", async (t) => {
  const num = 4;

  await t.step("Check if number is positive", () => {
    if (num <= 0) throw new Error("Number is not positive");
  });

  await t.step("Check if number is even", () => {
    if (num % 2 !== 0) throw new Error("Number is not even");
  });
});
Output
running 1 test from file Number checks โœ“ Check if number is positive โœ“ Check if number is even Test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out running 0 tests from file
โš ๏ธ

Common Pitfalls

  • Not awaiting t.step() calls: Steps are async, so forgetting await can cause tests to run out of order or finish early.
  • Using t.step() outside a test callback: Steps must be inside a Deno.test callback; otherwise, they won't run.
  • Not naming steps clearly: Step names should describe what they test for easier debugging.
typescript
Deno.test("Wrong usage", (t) => {
  // Missing await - step may not run properly
  t.step("step without await", () => {
    // test code
  });
});

// Correct usage
Deno.test("Correct usage", async (t) => {
  await t.step("step with await", () => {
    // test code
  });
});
๐Ÿ“Š

Quick Reference

Use this quick guide to remember how to use test steps in Deno:

  • t.step(name, callback): creates a test step inside a test.
  • Always await t.step() to ensure proper order.
  • Steps can be async or sync functions.
  • Use descriptive names for clarity.
  • Steps help break complex tests into smaller parts with detailed output.
โœ…

Key Takeaways

Use t.step() inside Deno.test to create smaller test parts called steps.
Always await t.step() calls to run steps in order.
Name steps clearly to make test output easier to understand.
Test steps can be async or sync functions.
Test steps improve test organization and debugging by showing detailed results for each part.