Skip to main content

@std/expect@0.223.0

Jest compatible `expect` assertion functions

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
70%
Published
a week ago
deno add @std/expect
import * as mod from "@std/expect";
npx jsr add @std/expect
import * as mod from "@std/expect";
yarn dlx jsr add @std/expect
import * as mod from "@std/expect";
pnpm dlx jsr add @std/expect
import * as mod from "@std/expect";
bunx jsr add @std/expect
import * as mod from "@std/expect";

This module provides Jest compatible expect assertion functionality.

Currently this module supports the following functions:

  • Common matchers:
    • toBe
    • toEqual
    • toStrictEqual
    • toMatch
    • toMatchObject
    • toBeDefined
    • toBeUndefined
    • toBeNull
    • toBeNaN
    • toBeTruthy
    • toBeFalsy
    • toContain
    • toContainEqual
    • toHaveLength
    • toBeGreaterThan
    • toBeGreaterThanOrEqual
    • toBeLessThan
    • toBeLessThanOrEqual
    • toBeCloseTo
    • toBeInstanceOf
    • toThrow
    • toHaveProperty
    • toHaveLength
  • Mock related matchers:
    • toHaveBeenCalled
    • toHaveBeenCalledTimes
    • toHaveBeenCalledWith
    • toHaveBeenLastCalledWith
    • toHaveBeenNthCalledWith
    • toHaveReturned
    • toHaveReturnedTimes
    • toHaveReturnedWith
    • toHaveLastReturnedWith
    • toHaveNthReturnedWith
  • Asymmetric matchers:
    • expect.anything
    • expect.any
    • expect.arrayContaining
    • expect.not.arrayContaining
    • expect.closeTo
    • expect.stringContaining
    • expect.not.stringContaining
    • expect.stringMatching
    • expect.not.stringMatching
  • Utilities:
    • expect.addEqualityTester
    • expect.extend

Only these functions are still not available:

  • Matchers:
    • toMatchSnapShot
    • toMatchInlineSnapShot
    • toThrowErrorMatchingSnapShot
    • toThrowErrorMatchingInlineSnapShot
  • Asymmetric matchers:
    • expect.objectContaining
    • expect.not.objectContaining
  • Utilities:
    • expect.assertions
    • expect.hasAssertions
    • expect.addSnapshotSerializer

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

Examples

Example 1

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);
Built and signed on
GitHub Actions
View transparency log