Skip to main content

@std/testing@0.215.0

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score52%
Published2 years ago (0.215.0)
asserts
Deprecated

(will be removed after 1.0.0) Import from https://deno.land/std/assert/mod.ts instead.

A library of assertion functions. If the assertion is false an AssertionError will be thrown which will result in pretty-printed diff of failing assertion.

Classes

c
AssertionError(message: string)

Error thrown when an assertion fails.

Functions

f
assert(
expr: unknown,
msg?
): asserts expr

Make an assertion, error will be thrown if expr does not have truthy value.

f
assertAlmostEquals(
actual: number,
expected: number,
tolerance?,
msg?: string
): void

Make an assertion that actual and expected are almost equal numbers through a given tolerance. It can be used to take into account IEEE-754 double-precision floating-point representation limitations. If the values are not almost equal then throw.

f
assertArrayIncludes<T>(
actual: ArrayLikeArg<T>,
expected: ArrayLikeArg<T>,
msg?: string
): void

Make an assertion that actual includes the expected values. If not then an error will be thrown.

f
assertEquals<T>(
actual: T,
expected: T,
msg?: string,
options?: { formatter?: (value: unknown) => string; }
): void

Make an assertion that actual and expected are equal, deeply. If not deeply equal, then throw.

f
assertExists<T>(
actual: T,
msg?: string
): asserts actual is NonNullable<T>

Make an assertion that actual is not null or undefined. If not then throw.

f
assertFalse(
expr: unknown,
msg?
): asserts expr is Falsy

Make an assertion, error will be thrown if expr have truthy value.

f
assertGreater<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is greater than expected. If not then throw.

f
assertGreaterOrEqual<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is greater than or equal to expected. If not then throw.

f
assertInstanceOf<T extends AnyConstructor>(
actual: unknown,
expectedType: T,
msg?
): asserts actual is GetConstructorType<T>

Make an assertion that obj is an instance of type. If not then throw.

f
assertIsError<E extends Error = Error>(
error: unknown,
ErrorClass?: new (...args: any[]) => E,
msgMatches?: string | RegExp,
msg?: string
): asserts error is E

Make an assertion that error is an Error. If not then an error will be thrown. An error class and a string that should be included in the error message can also be asserted.

f
assertLess<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is less than expected. If not then throw.

f
assertLessOrEqual<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is less than or equal to expected. If not then throw.

f
assertMatch(
actual: string,
expected: RegExp,
msg?: string
): void

Make an assertion that actual match RegExp expected. If not then throw.

f
assertNotEquals<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual and expected are not equal, deeply. If not then throw.

f
assertNotInstanceOf<A, T>(
actual: A,
unexpectedType: new (...args: any[]) => T,
msg?: string
): asserts actual is Exclude<A, T>

Make an assertion that obj is not an instance of type. If so, then throw.

f
assertNotMatch(
actual: string,
expected: RegExp,
msg?: string
): void

Make an assertion that actual not match RegExp expected. If match then throw.

f
assertNotStrictEquals<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual and expected are not strictly equal. If the values are strictly equal then throw.

f
assertObjectMatch(
actual: Record<PropertyKey, any>,
expected: Record<PropertyKey, unknown>,
msg?: string
): void

Make an assertion that actual object is a subset of expected object, deeply. If not, then throw.

f
assertRejects<E extends Error = Error>(
fn: () => PromiseLike<unknown>,
errorClassOrMsg?: (new (...args: any[]) => E) | string,
msgIncludesOrMsg?: string,
msg?: string
): Promise<E | Error | unknown>
2 overloads

Executes a function which returns a promise, expecting it to reject.

f
assertStrictEquals<T>(
actual: unknown,
expected: T,
msg?: string
): asserts actual is T

Make an assertion that actual and expected are strictly equal. If not then throw.

f
assertStringIncludes(
actual: string,
expected: string,
msg?: string
): void

Make an assertion that actual includes expected. If not then throw.

f
assertThrows<E extends Error = Error>(
fn: () => unknown,
errorClassOrMsg?: (new (...args: any[]) => E) | string,
msgIncludesOrMsg?: string,
msg?: string
): E | Error | unknown
2 overloads

Executes a function, expecting it to throw. If it does not, then it throws.

f
equal(
c: unknown,
d: unknown
): boolean

Deep equality comparison used in assertions

f
fail(msg?: string): never

Forcefully throws a failed assertion.

f
unimplemented(msg?: string): never

Use this to stub out methods that will throw when invoked.

f
unreachable(): never

Use this to assert unreachable code.

Type Aliases

T
AnyConstructor = new (...args: any[]) => any

Any constructor

T
ArrayLikeArg<T> = ArrayLike<T> & object

An array-like object (Array, Uint8Array, NodeList, etc.) that is not a string.

T
Falsy = false | 0 | 0 | "" | null | undefined

Assertion condition for assertFalse.

T
GetConstructorType<T extends AnyConstructor> = T extends new (...args: any) => infer C ? C : never

Gets constructor type

bdd

A BDD interface to Deno.test() API.

Functions

f
afterAll<T>(fn: (this: T) => void | Promise<void>): void

Run some shared teardown after all of the tests in the suite.

f
afterEach<T>(fn: (this: T) => void | Promise<void>): void

Run some shared teardown after each test in the suite.

f
beforeAll<T>(fn: (this: T) => void | Promise<void>): void

Run some shared setup before all of the tests in the suite.

f
beforeEach<T>(fn: (this: T) => void | Promise<void>): void

Run some shared setup before each test in the suite.

Interfaces

I
f

Registers a test suite.

  • ignore<T>(...args: DescribeArgs<T>): TestSuite<T>

    Registers a test suite with ignore set to true.

  • only<T>(...args: DescribeArgs<T>): TestSuite<T>

    Registers a test suite with only set to true.

  • skip<T>(...args: ItArgs<T>): void

    Registers a test suite with ignore set to true. Alias of .ignore().

I

The options for creating a test suite with the describe function.

  • afterAll:
    ((this: T) => void | Promise<void>)
    | ((this: T) => void | Promise<void>)[]

    Run some shared teardown after all of the tests in the suite.

  • afterEach:
    ((this: T) => void | Promise<void>)
    | ((this: T) => void | Promise<void>)[]

    Run some shared teardown after each test in the suite.

  • beforeAll:
    ((this: T) => void | Promise<void>)
    | ((this: T) => void | Promise<void>)[]

    Run some shared setup before all of the tests in the suite.

  • beforeEach:
    ((this: T) => void | Promise<void>)
    | ((this: T) => void | Promise<void>)[]

    Run some shared setup before each test in the suite.

  • fn: () => void
    No documentation available
  • suite: TestSuite<T>

    The describe function returns a TestSuite representing the group of tests. If describe is called within another describe calls fn, the suite will default to that parent describe calls returned TestSuite. If describe is not called within another describe calls fn, the suite will default to the TestSuite representing the global group of tests.

I
f

Registers an individual test case.

  • ignore<T>(...args: ItArgs<T>): void

    Registers an individual test case with ignore set to true.

  • only<T>(...args: ItArgs<T>): void

    Registers an individual test case with only set to true.

  • skip<T>(...args: ItArgs<T>): void

    Registers an individual test case with ignore set to true. Alias of .ignore().

I

The options for creating an individual test case with the it function.

  • fn: (
    this: T,
    t: Deno.TestContext
    ) => void | Promise<void>
    No documentation available
  • suite: TestSuite<T>

    The describe function returns a TestSuite representing the group of tests. If it is called within a describe calls fn, the suite will default to that parent describe calls returned TestSuite. If it is not called within a describe calls fn, the suite will default to the TestSuite representing the global group of tests.

I

A group of tests.

  • symbol: symbol
    No documentation available

Type Aliases

T
DescribeArgs<T> =
[DescribeDefinition<T>]
| [string]
| [string, Omit<DescribeDefinition<T>, "name">]
| [string, () => void]
| [() => void]
| [
string,
Omit<DescribeDefinition<T>, "fn" | "name">,
() => void
]

| [Omit<DescribeDefinition<T>, "fn">, () => void]
| [Omit<DescribeDefinition<T>, "fn" | "name">, () => void]
| [TestSuite<T>, string]
| [
TestSuite<T>,
string,
Omit<DescribeDefinition<T>, "name" | "suite">
]

| [TestSuite<T>, string, () => void]
| [TestSuite<T>, () => void]
| [
TestSuite<T>,
string,
Omit<DescribeDefinition<T>, "fn" | "name" | "suite">,
() => void
]

| [
TestSuite<T>,
Omit<DescribeDefinition<T>, "fn" | "suite">,
() => void
]

| [
TestSuite<T>,
Omit<DescribeDefinition<T>, "fn" | "name" | "suite">,
() => void
]

The arguments for a DescribeFunction.

T
ItArgs<T> =
[ItDefinition<T>]
| [string, Omit<ItDefinition<T>, "name">]
| [
string,
(
this: T,
t: Deno.TestContext
) => void | Promise<void>
]

| [(
this: T,
t: Deno.TestContext
) => void | Promise<void>]

| [
string,
Omit<ItDefinition<T>, "fn" | "name">,
(
this: T,
t: Deno.TestContext
) => void | Promise<void>
]

| [
Omit<ItDefinition<T>, "fn">,
(
this: T,
t: Deno.TestContext
) => void | Promise<void>
]

| [
Omit<ItDefinition<T>, "fn" | "name">,
(
this: T,
t: Deno.TestContext
) => void | Promise<void>
]

| [
TestSuite<T>,
string,
Omit<ItDefinition<T>, "name" | "suite">
]

| [
TestSuite<T>,
string,
(
this: T,
t: Deno.TestContext
) => void | Promise<void>
]

| [
TestSuite<T>,
(
this: T,
t: Deno.TestContext
) => void | Promise<void>
]

| [
TestSuite<T>,
string,
Omit<ItDefinition<T>, "fn" | "name" | "suite">,
(
this: T,
t: Deno.TestContext
) => void | Promise<void>
]

| [
TestSuite<T>,
Omit<ItDefinition<T>, "fn" | "suite">,
(
this: T,
t: Deno.TestContext
) => void | Promise<void>
]

| [
TestSuite<T>,
Omit<ItDefinition<T>, "fn" | "name" | "suite">,
(
this: T,
t: Deno.TestContext
) => void | Promise<void>
]

The arguments for an ItFunction.

mock

A mocking and spying library.

Classes

c
MockError(message: string)

An error related to spying on a function or instance method.

Functions

f
assertSpyCall<Self, Args extends unknown[], Return>(
spy: SpyLike<Self, Args, Return>,
callIndex: number,
expected?: ExpectedSpyCall<Self, Args, Return>
): void

Asserts that a spy is called as expected.

f
assertSpyCallArg<Self, Args extends unknown[], Return, ExpectedArg>(
spy: SpyLike<Self, Args, Return>,
callIndex: number,
argIndex: number,
expected: ExpectedArg
): ExpectedArg

Asserts that a spy is called with a specific arg as expected.

f
assertSpyCallArgs<
ExpectedArgs extends unknown[],
Args extends unknown[],
Return,
Self
>
(
spy: SpyLike<Self, Args, Return>,
callIndex: number,
argsStart?: number | ExpectedArgs,
argsEnd?: number | ExpectedArgs,
expected?: ExpectedArgs
): ExpectedArgs
3 overloads

Asserts that an spy is called with a specific range of args as expected. If a start and end index is not provided, the expected will be compared against all args. If a start is provided without an end index, the expected will be compared against all args from the start index to the end. The end index is not included in the range of args that are compared.

f
assertSpyCallAsync<Self, Args extends unknown[], Return>(
spy: SpyLike<Self, Args, Promise<Return>>,
callIndex: number,
expected?: ExpectedSpyCall<Self, Args, Promise<Return> | Return>
): Promise<void>

Asserts that an async spy is called as expected.

f
assertSpyCalls<Self, Args extends unknown[], Return>(
spy: SpyLike<Self, Args, Return>,
expectedCalls: number
): void

Asserts that a spy is called as much as expected and no more.

f
mockSession<Self, Args extends unknown[], Return>(func?: (
this: Self,
...args: Args
) => Return
): number | ((
this: Self,
...args: Args
) => Return)
2 overloads

Creates a session that tracks all mocks created before it's restored. If a callback is provided, it restores all mocks created within it.

f
mockSessionAsync<Self, Args extends unknown[], Return>(func: (
this: Self,
...args: Args
) => Promise<Return>
): (
this: Self,
...args: Args
) => Promise<Return>

Creates an async session that tracks all mocks created before the promise resolves.

f
resolvesNext<Return, Self = any, Args extends unknown[] = any[]>(iterable:
Iterable<Return | Error | Promise<Return | Error>>
| AsyncIterable<Return | Error | Promise<Return | Error>>
): (
this: Self,
...args: Args
) => Promise<Return>

Creates a function that resolves the awaited iterable values. Any awaited iterable values that are errors will be thrown.

f
restore(id?: number): void

Restores all mocks registered in the current session that have not already been restored. If an id is provided, it will restore all mocks registered in the session associed with that id that have not already been restored.

f
returnsArg<Arg, Self = any>(idx: number): (
this: Self,
...args: Arg[]
) => Arg | undefined

Creates a function that returns one of its arguments.

f
returnsArgs<Args extends unknown[], Self = any>(
start?,
end?: number
): (
this: Self,
...args: Args
) => Args

Creates a function that returns its arguments or a subset of them. If end is specified, it will return arguments up to but not including the end.

f
returnsNext<Return, Self = any, Args extends unknown[] = any[]>(values: Iterable<Return | Error>): (
this: Self,
...args: Args
) => Return

Creates a function that returns the iterable values. Any iterable values that are errors will be thrown.

f
returnsThis<Self = any, Args extends unknown[] = any[]>(): (
this: Self,
...args: Args
) => Self

Creates a function that returns the instance the method was called on.

f
spy<Self, Args extends unknown[], Return>(
funcOrConstOrSelf?:
((
this: Self,
...args: Args
) => Return)

| (new (...args: Args) => Self)
| Self,
property?: keyof Self
): SpyLike<Self, Args, Return>
4 overloads

Wraps a function or instance method with a Spy.

f
stub<Self, Args extends unknown[], Return>(
self: Self,
property: keyof Self,
func?: (
this: Self,
...args: Args
) => Return
): Stub<Self, Args, Return>
2 overloads

Replaces an instance method with a Stub.

Interfaces

I

A constructor wrapper that records all calls made to it.

  • calls: SpyCall<Self, Args, Self>[]

    Information about calls made to the function or instance method.

  • new(...args: Args): Self
    No documentation available
  • original: new (...args: Args) => Self

    The function that is being spied on.

  • restore(): void

    If spying on an instance method, this restores the original instance method.

  • restored: boolean

    Whether or not the original instance method has been restored.

I

Call information recorded by a spy.

  • args: [...Args, ...unknown[]]

    Arguments passed to a function when called.

  • error: { Class?: new (...args: any[]) => Error; msgIncludes?: string; }
    No documentation available
  • returned: Return

    The value that was returned by a function. If you expect a promise to reject, expect error instead.

  • self: Self

    The instance that a method was called on.

I

A function or instance method wrapper that records all calls made to it.

  • calls: SpyCall<Self, Args, Return>[]

    Information about calls made to the function or instance method.

  • original: (
    this: Self,
    ...args: Args
    ) => Return

    The function that is being spied on.

  • restore(): void

    If spying on an instance method, this restores the original instance method.

  • restored: boolean

    Whether or not the original instance method has been restored.

I

Call information recorded by a spy.

  • args: Args

    Arguments passed to a function when called.

  • error: Error

    The error value that was thrown by a function.

  • returned: Return

    The value that was returned by a function.

  • self: Self

    The instance that a method was called on.

I

An instance method replacement that records all calls made to it.

  • fake: (
    this: Self,
    ...args: Args
    ) => Return

    The function that is used instead of the original.

Type Aliases

snapshot

A snapshotting library.

Functions

f
assertSnapshot(
context: Deno.TestContext,
actual: unknown,
msgOrOpts?: string | SnapshotOptions<unknown>
): Promise<void>
2 overloads

Make an assertion that actual matches a snapshot. If the snapshot and actual do not a match, then throw.

f
createAssertSnapshot<T>(
options: SnapshotOptions<T>,
baseAssertSnapshot?: assertSnapshot
): assertSnapshot
No documentation available
f
serialize(actual: unknown): string
2 overloads

Default serializer for assertSnapshot.

Type Aliases

T
SnapshotMode = "assert" | "update"
No documentation available
T
SnapshotOptions<T = unknown> = { dir?: string; mode?: SnapshotMode; msg?: string; name?: string; path?: string; serializer?: (actual: T) => string; }
No documentation available
  • dir: string
    No documentation available
  • mode: SnapshotMode
    No documentation available
  • msg: string
    No documentation available
  • name: string
    No documentation available
  • path: string
    No documentation available
  • serializer: (actual: T) => string
    No documentation available
time

Utilities for mocking time while testing.

Classes

c
FakeTime(
start?: number | string | Date | null,
options?: FakeTimeOptions
)

Overrides the real Date object and timer functions with fake ones that can be controlled through the fake time instance.

  • delay(
    ms: number,
    options?: DelayOptions
    ): Promise<void>

    Resolves after the given number of milliseconds using real time.

  • next(): boolean

    Advances time to when the next scheduled timer is due. If there are no pending timers, time will not be changed. Returns true when there is a scheduled timer and false when there is not.

  • nextAsync(): Promise<boolean>

    Runs all pending microtasks then advances time to when the next scheduled timer is due. If there are no pending timers, time will not be changed.

  • now(): number

    The amount of milliseconds elapsed since January 1, 1970 00:00:00 UTC for the fake time. When set, it will call any functions waiting to be called between the current and new fake time. If the timer callback throws, time will stop advancing forward beyond that timer.

  • restore(): void

    Restores real time.

  • restoreFor<T>(
    callback: (...args: any[]) => Promise<T> | T,
    ...args: any[]
    ): Promise<T>

    Restores real time temporarily until callback returns and resolves.

  • runAll(): void

    Advances time forward to the next due timer until there are no pending timers remaining. If the timers create additional timers, they will be run too. If there is an interval, time will keep advancing forward until the interval is cleared.

  • runAllAsync(): Promise<void>

    Advances time forward to the next due timer until there are no pending timers remaining. If the timers create additional timers, they will be run too. If there is an interval, time will keep advancing forward until the interval is cleared. Runs all pending microtasks before each timer.

  • runMicrotasks(): Promise<void>

    Runs all pending microtasks.

  • start(): number

    The initial amount of milliseconds elapsed since January 1, 1970 00:00:00 UTC for the fake time.

  • tick(ms?): void

    Adds the specified number of milliseconds to the fake time. This will call any functions waiting to be called between the current and new fake time.

  • tickAsync(ms?): Promise<void>

    Runs all pending microtasks then adds the specified number of milliseconds to the fake time. This will call any functions waiting to be called between the current and new fake time.

c
TimeError(message: string)

An error related to faking time.

Interfaces

I
No documentation available
  • The frequency in milliseconds at which fake time is updated. If advanceRate is set, we will update the time every 10 milliseconds by default.

  • advanceRate: number

    The rate relative to real time at which fake time is updated. By default time only moves forward through calling tick or setting now. Set to 1 to have the fake time automatically tick forward at the same rate in milliseconds as real time.

types

Functions

f
assertType<T extends true | false>(_expectTrue: T): void

Asserts at compile time that the provided type argument's type resolves to the expected boolean literal type.

Type Aliases

T
Assert<T extends true | false, Expected extends T> = never

Asserts at compile time that the provided type argument's type resolves to the expected boolean literal type.

T
AssertFalse<T extends false> = never

Asserts at compile time that the provided type argument's type resolves to false.

T
AssertTrue<T extends true> = never

Asserts at compile time that the provided type argument's type resolves to true.

T
Has<T, U> = IsAny<T> extends true ? true : IsAny<U> extends true ? false : Extract<T, U> extends never ? false : true

Checks if type T has the specified type U.

T
IsAny<T> = 0 extends (1 & T) ? true : false

Checks if type T is the any type.

T
IsExact<T, U> = TupleMatches<AnyToBrand<T>, AnyToBrand<U>> extends true ? TupleMatches<DeepPrepareIsExact<T>, DeepPrepareIsExact<U>> extends true ? true : false : false

Checks if type T exactly matches type U.

T
IsNever<T> = [T] extends [never] ? true : false

Checks if type T is the never type.

T
IsNullable<T> = Extract<T, null | undefined> extends never ? false : true

Checks if type T is possibly null or undefined.

T
IsUnknown<T> = unknown extends T ? ([T] extends [null] ? false : true) : false

Checks if type T is the unknown type.

T
NotHas<T, U> = Has<T, U> extends false ? true : false

Checks if type T does not have the specified type U.

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.