0
0
DenoHow-ToBeginner ยท 4 min read

How to Test Async Code in Deno: Simple Guide with Examples

To test async code in Deno, write your test function as an async function and use await inside it. Use Deno's built-in test runner with Deno.test and assertions from std/testing/asserts.ts to check async results.
๐Ÿ“

Syntax

Use Deno.test to define a test. Mark the test function as async to handle asynchronous code. Inside, use await to wait for promises. Use assertions like assertEquals to verify results.

  • Deno.test: Defines a test case.
  • async function: Allows use of await inside.
  • await: Waits for async operations to complete.
  • assertEquals: Checks if expected and actual values match.
typescript
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts";

Deno.test("async test example", async () => {
  const result = await Promise.resolve(42);
  assertEquals(result, 42);
});
๐Ÿ’ป

Example

This example shows how to test an async function that returns a promise. The test waits for the promise to resolve and then checks the value.

typescript
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts";

async function fetchData() {
  return new Promise((resolve) => {
    setTimeout(() => resolve("data loaded"), 100);
  });
}

Deno.test("fetchData returns correct data", async () => {
  const data = await fetchData();
  assertEquals(data, "data loaded");
});
Output
running 1 test from file ok 1 fetchData returns correct data test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
โš ๏ธ

Common Pitfalls

Common mistakes include not marking the test function as async, which causes the test to finish before the async code runs. Another mistake is forgetting to await the promise, leading to false positives or errors.

Always use async and await together in your test functions to ensure proper timing.

typescript
import { assertEquals } from "https://deno.land/std@0.203.0/testing/asserts.ts";

// Wrong: missing async and await
Deno.test("wrong async test", () => {
  const promise = Promise.resolve(5);
  promise.then(value => {
    assertEquals(value, 5); // This runs after test ends
  });
});

// Right: async test with await
Deno.test("correct async test", async () => {
  const value = await Promise.resolve(5);
  assertEquals(value, 5);
});
๐Ÿ“Š

Quick Reference

ConceptDescriptionExample
Deno.testDefines a test caseDeno.test("name", async () => { ... })
async functionAllows use of await inside testasync () => { await ... }
awaitWaits for promise to resolveconst result = await fetchData()
assertEqualsChecks expected vs actualassertEquals(result, expected)
โœ…

Key Takeaways

Always mark your test functions as async to handle asynchronous code.
Use await inside tests to wait for promises to resolve before asserting.
Use Deno's built-in test runner and assertion library for clean tests.
Avoid forgetting async/await to prevent tests finishing too early.
Run tests with `deno test` command to see async test results.