Skip to main content
Home
Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score58%
Downloads1/wk
Published4 months ago (0.7.1)

A universal testing module that works seamlessly across Deno, Node.js, and Bun runtimes. Write tests once, run them anywhere - from mobile to desktop, and 3D/spatial environments!

interface ExtendedExpected
extends Expected<IsAsync>

The ExtendedExpected interface defines the available assertion methods.

Type Parameters

IsAsync = false

Properties

toThrow: <E extends Error = Error>(
error?:
string
| RegExp
| E
| (new (...args: any[]) => E)
,
message?: string | RegExp,
) => void

Asserts a function to throw an error.

Unlike the epoynmous method from @std/expect, this matcher accepts a second argument that can be either a string or a regular expression when the first argument is a Error class or instance. In this case, the behavior is the same as assertIsError where you can both check the error type and message.

import { expect } from "@in/test"
const throws = () => { throw new Error("Expected error") }
expect(throws).toThrow(Error)
expect(throws).toThrow("Expected error")
expect(throws).toThrow(Error, /Expected/)
toSatisfy: (evaluate: callback) => unknown

Asserts a value matches the given predicate.

import { expect } from "@in/test"
expect("foo").toSatisfy((value:string) => value.length > 1)
toBeType: (
type: string,
options?: { nullable?: boolean; },
) => unknown

Asserts a value is of a given type (using typeof operator). Note that null is not considered of type "object" unless nullable option is set to true.

import { expect } from "@in/test"
expect("foo").toBeType("string")
expect({}).toBeType("object")
expect(null).toBeType("object", { nullable: true })
expect(null).not.toBeType("object")
toHaveDescribedProperty: (
key: PropertyKey,
expected: PropertyDescriptor,
) => unknown

Asserts a property matches a given descriptor (using Object.getOwnPropertyDescriptor).

import { expect } from "@in/test"
const foo = Object.defineProperties({}, { bar: { value: true, writable: false, enumerable: true } })
expect(foo).toHaveDescribedProperty("bar", { writable: false, enumerable: true })
toHaveImmutableProperty: (
key: PropertyKey,
testValue?: unknown,
) => unknown

Asserts a writable property is immutable (i.e. setting its value does not throw but does not change its value either).

Note that it will actually proceed to assign testValue and restore it to original value after test.

import { expect } from "@in/test"
const foo = new (class {
  #bar = true
  get bar() {
    return this.#bar
  }
  set bar(_:unknown) {
    return
  }
})()
expect(foo).toHaveImmutableProperty("bar")
toBeIterable: () => unknown

Assert an object is iterable (checking Symbol.iterator presence).

import { expect } from "@in/test"
expect([]).toBeIterable()
expect(new Map()).toBeIterable()
expect(new Set()).toBeIterable()
toBeSealed: () => unknown

Assert an object is sealed (using Object.isSealed).

import { expect } from "@in/test"
expect(Object.seal({})).toBeSealed()
toBeFrozen: () => unknown

Assert an object is frozen (using Object.isFrozen).

import { expect } from "@in/test"
expect(Object.freeze({})).toBeFrozen()
toBeExtensible: () => unknown

Assert an object is extensible (using Object.isExtensible).

import { expect } from "@in/test"
expect(Object.preventExtensions({})).not.toBeExtensible()
toBeShallowCopyOf: (expected?: Iterable<unknown> | record) => unknown

Asserts an object is a shallow copy (i.e. its content is identical but reference is not).

import { expect } from "@in/test"
const foo = { bar: true }
expect({...foo}).toBeShallowCopyOf(foo)
toBeEmpty: () => unknown

Asserts an iterable is empty.

import { expect } from "@in/test"
expect([]).toBeEmpty()
expect(new Map()).toBeEmpty()
expect(new Set()).toBeEmpty()
toBeSorted: (compare?: Arg<Array<unknown>["sort"]>) => unknown

Asserts an iterable to be sorted.

import { expect } from "@in/test"
expect([1, 2, 3]).toBeSorted()
toBeReverseSorted: (compare?: Arg<Array<unknown>["sort"]>) => unknown

Asserts an iterable to be reverse sorted.

import { expect } from "@in/test"
expect([3, 2, 1]).toBeReverseSorted()
toBeOneOf: (values: Iterable<unknown>) => unknown

Asserts a value is one of expected value.

import { expect } from "@in/test"
expect("foo").toBeOneOf(["foo", "bar"])
toBeWithin: (
range: [number, number],
exclusive?: boolean,
) => unknown

Asserts a number is with a given range.

import { expect } from "@in/test"
expect(Math.PI).toBeWithin([3, 4])
toBeFinite: () => unknown

Asserts a number to be finite.

import { expect } from "@in/test"
expect(1).toBeFinite()
expect(Infinity).not.toBeFinite()
toBeParseableJSON: (reviver?: Arg<JSON["parse"], 1>) => unknown

Asserts a string to be parseable JSON.

import { expect } from "@in/test"
expect('{"foo":"bar"}').toBeParseableJSON()
toBeBase64: () => unknown

Asserts a string is a valid base64 string.

import { expect } from "@in/test"
expect(btoa("foo")).toBeBase64()
toRespondWithStatus: (status:
Arrayable<number>
| "informational"
| "1XX"
| "successful"
| "2XX"
| "redirect"
| "3XX"
| "client_error"
| "4XX"
| "server_error"
| "5XX"
) => unknown

Asserts a response to have a given status code.

Note that Response.body.cancel() is automatically called to prevent leaks. If you need to perform more assertions on a response, it is advised use separate matchers instead.

import { expect, Status } from "@in/test"
expect(new Response(null, { status: Status.OK })).toRespondWithStatus(Status.OK)
expect(new Response(null, { status: Status.OK })).toRespondWithStatus([Status.OK, Status.Created])
expect(new Response(null, { status: Status.OK })).toRespondWithStatus("2XX")
toBeHashed: (algorithm: string) => unknown

Asserts a string is hashed by specified algorithm.

Algorithm may be either lowercase or uppercase, and contains dashes or not (e.g. sha256, SHA256 and SHA-256 will all be treated as the same). Will throw if an unknown algorithm is specified.

Please note that it only checks whether it could be a valid output of specified hash algorithm.

import { expect, Status } from "@in/test"
expect("$2a$12$lpGSoVPZ8erLDF93Sqyic.U73v0/w0owPb3dIP9goO7iC5Wp/I8OG").toBeHashed("bcrypt")
toBeDate: () => unknown

Asserts a value is a valid date (using Date).

import { expect, Status } from "@in/test"
expect("2024-07-13T20:30:57.958Z").toBeDate()
expect(new Date()).toBeDate()
toBePast: (date?: ) => unknown

Asserts a date is before another date.

import { expect, Status } from "@in/test"
expect(new Date(Date.now() - 5000)).toBePast()
toBeFuture: (date?: ) => unknown

Asserts a date is after another date.

import { expect, Status } from "@in/test"
expect(new Date(Date.now() + 5000)).toBeFuture()
not: IsAsync extends true ? Async<ExtendedExpected<true>> : ExtendedExpected<false>

The negation object that allows chaining negated assertions.

The object that allows chaining assertions with async functions that are expected to resolve to a value.

The object that allows chaining assertions with async functions that are expected to throw an error.

Methods

toBeEmail(): () => unknown

Asserts a string is a valid email address (using https://pdw.ex-parrot.com/Mail-RFC822-Address.html).

import { expect } from "@in/test"
expect("foo@example.com").toBeEmail()
expect("foo+bar@example.com").toBeEmail()
toBeUrl(): () => unknown

Asserts a string is a valid url (using URL).

import { expect } from "@in/test"
expect("https://xr.new").toBeUrl()

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@in/test

Import symbol

import { type ExtendedExpected } from "@in/test/expect";
or

Import directly with a jsr specifier

import { type ExtendedExpected } from "jsr:@in/test/expect";

Add Package

pnpm i jsr:@in/test
or (using pnpm 10.8 or older)
pnpm dlx jsr add @in/test

Import symbol

import { type ExtendedExpected } from "@in/test/expect";

Add Package

yarn add jsr:@in/test
or (using Yarn 4.8 or older)
yarn dlx jsr add @in/test

Import symbol

import { type ExtendedExpected } from "@in/test/expect";

Add Package

vlt install jsr:@in/test

Import symbol

import { type ExtendedExpected } from "@in/test/expect";

Add Package

npx jsr add @in/test

Import symbol

import { type ExtendedExpected } from "@in/test/expect";

Add Package

bunx jsr add @in/test

Import symbol

import { type ExtendedExpected } from "@in/test/expect";