1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 |
/**
* This module provides Jest compatible expect assertion functionality.
*
* ```ts no-assert
* 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:
* - Common matchers:
* - {@linkcode Expected.toBe | toBe}
* - {@linkcode Expected.toEqual | toEqual}
* - {@linkcode Expected.toStrictEqual | toStrictEqual}
* - {@linkcode Expected.toMatch | toMatch}
* - {@linkcode Expected.toMatchObject | toMatchObject}
* - {@linkcode Expected.toBeDefined | toBeDefined}
* - {@linkcode Expected.toBeUndefined | toBeUndefined}
* - {@linkcode Expected.toBeNull | toBeNull}
* - {@linkcode Expected.toBeNaN | toBeNaN}
* - {@linkcode Expected.toBeTruthy | toBeTruthy}
* - {@linkcode Expected.toBeFalsy | toBeFalsy}
* - {@linkcode Expected.toContain | toContain}
* - {@linkcode Expected.toContainEqual | toContainEqual}
* - {@linkcode Expected.toHaveLength | toHaveLength}
* - {@linkcode Expected.toBeGreaterThan | toBeGreaterThan}
* - {@linkcode Expected.toBeGreaterThanOrEqual | toBeGreaterThanOrEqual}
* - {@linkcode Expected.toBeLessThan | toBeLessThan}
* - {@linkcode Expected.toBeLessThanOrEqual | toBeLessThanOrEqual}
* - {@linkcode Expected.toBeCloseTo | toBeCloseTo}
* - {@linkcode Expected.toBeInstanceOf | toBeInstanceOf}
* - {@linkcode Expected.toThrow | toThrow}
* - {@linkcode Expected.toHaveProperty | toHaveProperty}
* - Mock related matchers:
* - {@linkcode Expected.toHaveBeenCalled | toHaveBeenCalled}
* - {@linkcode Expected.toHaveBeenCalledTimes | toHaveBeenCalledTimes}
* - {@linkcode Expected.toHaveBeenCalledWith | toHaveBeenCalledWith}
* - {@linkcode Expected.toHaveBeenLastCalledWith | toHaveBeenLastCalledWith}
* - {@linkcode Expected.toHaveBeenNthCalledWith | toHaveBeenNthCalledWith}
* - {@linkcode Expected.toHaveReturned | toHaveReturned}
* - {@linkcode Expected.toHaveReturnedTimes | toHaveReturnedTimes}
* - {@linkcode Expected.toHaveReturnedWith | toHaveReturnedWith}
* - {@linkcode Expected.toHaveLastReturnedWith | toHaveLastReturnedWith}
* - {@linkcode Expected.toHaveNthReturnedWith | toHaveNthReturnedWith}
* - Asymmetric matchers:
* - {@linkcode expect.anything}
* - {@linkcode expect.any}
* - {@linkcode expect.arrayContaining}
* - {@linkcode expect.closeTo}
* - {@linkcode expect.stringContaining}
* - {@linkcode expect.stringMatching}
* - Utilities:
* - {@linkcode expect.addEqualityTester}
* - {@linkcode expect.extend}
* - {@linkcode expect.hasAssertions}
*
* Only these functions are still not available:
* - Matchers:
* - `toMatchSnapShot`
* - `toMatchInlineSnapshot`
* - `toThrowErrorMatchingSnapshot`
* - `toThrowErrorMatchingInlineSnapshot`
* - Asymmetric matchers:
* - `expect.objectContaining`
* - `expect.not.objectContaining`
* - Utilities:
* - `expect.assertions`
* - `expect.addSnapshotSerializer`
*
* The tracking issue to add support for unsupported parts of the API is
* {@link https://github.com/denoland/std/issues/3964}.
*
* This module is largely inspired by
* {@link https://github.com/allain/expect | x/expect} module by
* {@link https://github.com/allain | Allain Lalonde}.
*
* @module
*/
import type { AnyConstructor as _typeAlias_AnyConstructor } from "jsr:@std/[email protected]"
/**
* A constructor that accepts any args and returns any value
*/
type AnyConstructor = _typeAlias_AnyConstructor
export type { AnyConstructor }
import type { Async as _typeAlias_Async } from "jsr:@std/[email protected]"
/**
* converts all the methods in an interface to be async functions
*/
type Async<T> = _typeAlias_Async<T>
export type { Async }
import type { Expected as _interface_Expected } from "jsr:@std/[email protected]"
/**
* The Expected interface defines the available assertion methods.
*/
interface Expected<IsAsync = false> extends _interface_Expected<IsAsync> {}
export type { Expected }
import { expect as _namespace_expect } from "jsr:@std/[email protected]"
/**
* Additional properties on the `expect` function.
*/
const expect = _namespace_expect as typeof _namespace_expect
export { expect }
import { fn as _function_fn } from "jsr:@std/[email protected]"
/**
* Creates a mock function that can be used for testing and assertions.
*
* @param stubs Functions to be used as stubs for different calls.
* @return A mock function that keeps track of calls and returns values based on the provided stubs.
*
* @example Usage
* ```ts no-assert
* import { fn, expect } from "@std/expect";
*
* Deno.test("example", () => {
* const mockFn = fn(
* (a: number, b: number) => a + b,
* (a: number, b: number) => a - b
* );
* const result = mockFn(1, 2);
* expect(result).toEqual(3);
* expect(mockFn).toHaveBeenCalledWith(1, 2);
* expect(mockFn).toHaveBeenCalledTimes(1);
*
* const result2 = mockFn(3, 2);
* expect(result2).toEqual(1);
* expect(mockFn).toHaveBeenCalledWith(3, 2);
* expect(mockFn).toHaveBeenCalledTimes(2);
* });
* ```
*/
const fn = _function_fn as typeof _function_fn
export { fn }
|