Skip to main content
On this page

@std/expect

Overview Jump to heading

This module provides Jest compatible expect assertion functionality.

import { expect } from "@std/expect"; const x = 6 * 7; expect(x).toEqual(42); expect(x).not.toEqual(0); await expect(Promise.resolve(x)).resolves.toEqual(42); 

Currently this module supports the following functions:

Only these functions are still not available:

  • Matchers:
    • toMatchSnapshot
    • toMatchInlineSnapshot
    • toThrowErrorMatchingSnapshot
    • toThrowErrorMatchingInlineSnapshot

The tracking issue to add support for unsupported parts of the API is https://github.com/denoland/std/issues/3964.

This module is largely inspired by x/expect module by Allain Lalonde.

Add to your project Jump to heading

deno add jsr:@std/expect 

See all symbols in @std/expect on

What is expect? Jump to heading

A Jest‑compatible fluent assertion API. Instead of standalone assert functions, you write expectations like expect(value).toBe(1) and can chain modifiers like .not, .resolves, and .rejects.

When to use @std/expect Jump to heading

If you prefer a Jest-like API for fluent, readable assertions with rich matchers, especially when migrating from Jest.

Examples Jump to heading

import { expect } from "@std/expect"; expect({ a: 1, b: 2 }).toMatchObject({ a: 1 }); await expect(Promise.resolve(42)).resolves.toBe(42); expect([1, 2, 3]).toContain(2); // Throwing and async rejections expect(() => JSON.parse("{")) .toThrow(SyntaxError); await expect(Promise.reject(new Error("boom"))) .rejects.toThrow("boom"); // Close-to for floating point, regex matching expect(0.1 + 0.2).toBeCloseTo(0.3, 15); expect("hello world").toMatch(/world/); // Using asymmetric matchers expect({ id: 1, name: "Ada" }) .toEqual(expect.objectContaining({ name: expect.stringMatching(/^A/) })); 

Use with Deno.test Jump to heading

import { expect } from "@std/expect"; Deno.test("basic expect", () => { const value = 2 + 2; expect(value).toBe(4); }); 

For more information on testing in Deno, see the testing docs.

Tips Jump to heading

  • Use toEqual for deep data comparisons and toBe for identity.
  • Prefer .resolves / .rejects helpers for promises.
  • Prefer .resolves / .rejects helpers for promises.
  • Use toStrictEqual for stricter deep checks (no extra props, etc.).
  • Extend with custom matchers via expect.extend when needed.

Did you find what you needed?