bun-types 1.2.11-canary.20250418T140557 → 1.2.11-canary.20250420T140512

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test.d.ts CHANGED
@@ -29,11 +29,14 @@ declare module "bun:test" {
29
29
  *
30
30
  * This is useful for mocking modules.
31
31
  *
32
+ * If the module is already loaded, exports are overwritten with the return
33
+ * value of `factory`. If the export didn't exist before, it will not be
34
+ * added to existing import statements. This is due to how ESM works.
35
+ *
32
36
  * @param id module ID to mock
33
37
  * @param factory a function returning an object that will be used as the exports of the mocked module
34
38
  *
35
39
  * @example
36
- * ## Example
37
40
  * ```ts
38
41
  * import { mock } from "bun:test";
39
42
  *
@@ -47,12 +50,6 @@ declare module "bun:test" {
47
50
  *
48
51
  * console.log(await readFile("hello.txt", "utf8")); // hello world
49
52
  * ```
50
- *
51
- * ## More notes
52
- *
53
- * If the module is already loaded, exports are overwritten with the return
54
- * value of `factory`. If the export didn't exist before, it will not be
55
- * added to existing import statements. This is due to how ESM works.
56
53
  */
57
54
  module(id: string, factory: () => any): void | Promise<void>;
58
55
  /**
@@ -155,6 +152,8 @@ declare module "bun:test" {
155
152
  readonly name: string;
156
153
  }
157
154
 
155
+ type DescribeLabel = number | string | Function | FunctionLike;
156
+
158
157
  /**
159
158
  * Describes a group of related tests.
160
159
  *
@@ -176,28 +175,28 @@ declare module "bun:test" {
176
175
  export interface Describe {
177
176
  (fn: () => void): void;
178
177
 
179
- (label: number | string | Function | FunctionLike, fn: () => void): void;
178
+ (label: DescribeLabel, fn: () => void): void;
180
179
  /**
181
180
  * Skips all other tests, except this group of tests.
182
181
  *
183
182
  * @param label the label for the tests
184
183
  * @param fn the function that defines the tests
185
184
  */
186
- only(label: string, fn: () => void): void;
185
+ only(label: DescribeLabel, fn: () => void): void;
187
186
  /**
188
187
  * Skips this group of tests.
189
188
  *
190
189
  * @param label the label for the tests
191
190
  * @param fn the function that defines the tests
192
191
  */
193
- skip(label: string, fn: () => void): void;
192
+ skip(label: DescribeLabel, fn: () => void): void;
194
193
  /**
195
194
  * Marks this group of tests as to be written or to be fixed.
196
195
  *
197
196
  * @param label the label for the tests
198
197
  * @param fn the function that defines the tests
199
198
  */
200
- todo(label: string, fn?: () => void): void;
199
+ todo(label: DescribeLabel, fn?: () => void): void;
201
200
  /**
202
201
  * Runs this group of tests, only if `condition` is true.
203
202
  *
@@ -205,19 +204,19 @@ declare module "bun:test" {
205
204
  *
206
205
  * @param condition if these tests should run
207
206
  */
208
- if(condition: boolean): (label: string, fn: () => void) => void;
207
+ if(condition: boolean): (label: DescribeLabel, fn: () => void) => void;
209
208
  /**
210
209
  * Skips this group of tests, if `condition` is true.
211
210
  *
212
211
  * @param condition if these tests should be skipped
213
212
  */
214
- skipIf(condition: boolean): (label: string, fn: () => void) => void;
213
+ skipIf(condition: boolean): (label: DescribeLabel, fn: () => void) => void;
215
214
  /**
216
215
  * Marks this group of tests as to be written or to be fixed, if `condition` is true.
217
216
  *
218
217
  * @param condition if these tests should be skipped
219
218
  */
220
- todoIf(condition: boolean): (label: string, fn: () => void) => void;
219
+ todoIf(condition: boolean): (label: DescribeLabel, fn: () => void) => void;
221
220
  /**
222
221
  * Returns a function that runs for each item in `table`.
223
222
  *
@@ -225,13 +224,17 @@ declare module "bun:test" {
225
224
  */
226
225
  each<T extends Readonly<[any, ...any[]]>>(
227
226
  table: readonly T[],
228
- ): (label: string, fn: (...args: [...T]) => void | Promise<unknown>, options?: number | TestOptions) => void;
227
+ ): (label: DescribeLabel, fn: (...args: [...T]) => void | Promise<unknown>, options?: number | TestOptions) => void;
229
228
  each<T extends any[]>(
230
229
  table: readonly T[],
231
- ): (label: string, fn: (...args: Readonly<T>) => void | Promise<unknown>, options?: number | TestOptions) => void;
230
+ ): (
231
+ label: DescribeLabel,
232
+ fn: (...args: Readonly<T>) => void | Promise<unknown>,
233
+ options?: number | TestOptions,
234
+ ) => void;
232
235
  each<T>(
233
236
  table: T[],
234
- ): (label: string, fn: (...args: T[]) => void | Promise<unknown>, options?: number | TestOptions) => void;
237
+ ): (label: DescribeLabel, fn: (...args: T[]) => void | Promise<unknown>, options?: number | TestOptions) => void;
235
238
  }
236
239
  /**
237
240
  * Describes a group of related tests.
@@ -593,8 +596,6 @@ declare module "bun:test" {
593
596
  * @returns never
594
597
  *
595
598
  * @example
596
- * ## Example
597
- *
598
599
  * ```ts
599
600
  * import { expect, test } from "bun:test";
600
601
  *
@@ -1786,367 +1787,329 @@ declare module "bun:test" {
1786
1787
  }
1787
1788
 
1788
1789
  type MatcherContext = MatcherUtils & MatcherState;
1789
- }
1790
-
1791
- declare namespace JestMock {
1792
- /**
1793
- * Copyright (c) Meta Platforms, Inc. and affiliates.
1794
- *
1795
- * This source code is licensed under the MIT license found in the
1796
- * LICENSE file in the root directory of this source tree.
1797
- */
1798
- export interface ClassLike {
1799
- new (...args: any): any;
1800
- }
1801
-
1802
- export type ConstructorLikeKeys<T> = keyof {
1803
- [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
1804
- };
1805
1790
 
1806
- // export const fn: <T extends FunctionLike = UnknownFunction>(
1807
- // implementation?: T | undefined,
1808
- // ) => Mock<T>;
1809
-
1810
- export type FunctionLike = (...args: any) => any;
1811
-
1812
- export type MethodLikeKeys<T> = keyof {
1813
- [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K];
1814
- };
1815
-
1816
- export interface Mock<T extends (...args: any[]) => any> extends MockInstance<T> {
1817
- (...args: Parameters<T>): ReturnType<T>;
1818
- }
1819
-
1820
- /**
1821
- * All what the internal typings need is to be sure that we have any-function.
1822
- * `FunctionLike` type ensures that and helps to constrain the type as well.
1823
- * The default of `UnknownFunction` makes sure that `any`s do not leak to the
1824
- * user side. For instance, calling `fn()` without implementation will return
1825
- * a mock of `(...args: Array<unknown>) => unknown` type. If implementation
1826
- * is provided, its typings are inferred correctly.
1827
- */
1828
- // export interface Mock<T extends FunctionLike = UnknownFunction>
1829
- // extends Function,
1830
- // MockInstance<T> {
1831
- // new (...args: Parameters<T>): ReturnType<T>;
1832
- // (...args: Parameters<T>): ReturnType<T>;
1833
- // }
1834
-
1835
- // export type Mocked<T> = T extends ClassLike
1836
- // ? MockedClass<T>
1837
- // : T extends FunctionLike
1838
- // ? MockedFunction<T>
1839
- // : T extends object
1840
- // ? MockedObject<T>
1841
- // : T;
1842
-
1843
- // export const mocked: {
1844
- // <T extends object>(
1845
- // source: T,
1846
- // options?: {
1847
- // shallow: false;
1848
- // },
1849
- // ): Mocked<T>;
1850
- // <T_1 extends object>(
1851
- // source: T_1,
1852
- // options: {
1853
- // shallow: true;
1854
- // },
1855
- // ): MockedShallow<T_1>;
1856
- // };
1857
-
1858
- // export type MockedClass<T extends ClassLike> = MockInstance<
1859
- // (...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>
1860
- // > &
1861
- // MockedObject<T>;
1862
-
1863
- // export type MockedFunction<T extends FunctionLike> = MockInstance<T> &
1864
- // MockedObject<T>;
1865
-
1866
- // type MockedFunctionShallow<T extends FunctionLike> = MockInstance<T> & T;
1867
-
1868
- // export type MockedObject<T extends object> = {
1869
- // [K in keyof T]: T[K] extends ClassLike
1870
- // ? MockedClass<T[K]>
1871
- // : T[K] extends FunctionLike
1872
- // ? MockedFunction<T[K]>
1873
- // : T[K] extends object
1874
- // ? MockedObject<T[K]>
1875
- // : T[K];
1876
- // } & T;
1877
-
1878
- // type MockedObjectShallow<T extends object> = {
1879
- // [K in keyof T]: T[K] extends ClassLike
1880
- // ? MockedClass<T[K]>
1881
- // : T[K] extends FunctionLike
1882
- // ? MockedFunctionShallow<T[K]>
1883
- // : T[K];
1884
- // } & T;
1885
-
1886
- // export type MockedShallow<T> = T extends ClassLike
1887
- // ? MockedClass<T>
1888
- // : T extends FunctionLike
1889
- // ? MockedFunctionShallow<T>
1890
- // : T extends object
1891
- // ? MockedObjectShallow<T>
1892
- // : T;
1893
-
1894
- // export type MockFunctionMetadata<
1895
- // T = unknown,
1896
- // MetadataType = MockMetadataType,
1897
- // > = MockMetadata<T, MetadataType>;
1898
-
1899
- // export type MockFunctionMetadataType = MockMetadataType;
1900
-
1901
- type MockFunctionResult<T extends FunctionLike = UnknownFunction> =
1902
- | MockFunctionResultIncomplete
1903
- | MockFunctionResultReturn<T>
1904
- | MockFunctionResultThrow;
1905
-
1906
- interface MockFunctionResultIncomplete {
1907
- type: "incomplete";
1908
- /**
1909
- * Result of a single call to a mock function that has not yet completed.
1910
- * This occurs if you test the result from within the mock function itself,
1911
- * or from within a function that was called by the mock.
1912
- */
1913
- value: undefined;
1914
- }
1915
-
1916
- interface MockFunctionResultReturn<T extends FunctionLike = UnknownFunction> {
1917
- type: "return";
1918
- /**
1919
- * Result of a single call to a mock function that returned.
1920
- */
1921
- value: ReturnType<T>;
1922
- }
1923
-
1924
- interface MockFunctionResultThrow {
1925
- type: "throw";
1926
- /**
1927
- * Result of a single call to a mock function that threw.
1928
- */
1929
- value: unknown;
1930
- }
1931
-
1932
- interface MockFunctionState<T extends FunctionLike = FunctionLike> {
1933
- /**
1934
- * List of the call arguments of all calls that have been made to the mock.
1935
- */
1936
- calls: Array<Parameters<T>>;
1937
- /**
1938
- * List of all the object instances that have been instantiated from the mock.
1939
- */
1940
- instances: Array<ReturnType<T>>;
1941
- /**
1942
- * List of all the function contexts that have been applied to calls to the mock.
1943
- */
1944
- contexts: Array<ThisParameterType<T>>;
1945
- /**
1946
- * List of the call order indexes of the mock. Jest is indexing the order of
1947
- * invocations of all mocks in a test file. The index is starting with `1`.
1948
- */
1949
- invocationCallOrder: number[];
1950
- /**
1951
- * List of the call arguments of the last call that was made to the mock.
1952
- * If the function was not called, it will return `undefined`.
1953
- */
1954
- lastCall?: Parameters<T>;
1955
- /**
1956
- * List of the results of all calls that have been made to the mock.
1957
- */
1958
- results: Array<MockFunctionResult<T>>;
1959
- }
1960
-
1961
- export interface MockInstance<T extends FunctionLike = UnknownFunction> {
1962
- _isMockFunction: true;
1963
- _protoImpl: Function;
1964
- getMockImplementation(): T | undefined;
1965
- getMockName(): string;
1966
- mock: MockFunctionState<T>;
1967
- mockClear(): this;
1968
- mockReset(): this;
1969
- mockRestore(): void;
1970
- mockImplementation(fn: T): this;
1971
- mockImplementationOnce(fn: T): this;
1972
- withImplementation(fn: T, callback: () => Promise<unknown>): Promise<void>;
1973
- withImplementation(fn: T, callback: () => void): void;
1974
- mockName(name: string): this;
1975
- mockReturnThis(): this;
1976
- mockReturnValue(value: ReturnType<T>): this;
1977
- mockReturnValueOnce(value: ReturnType<T>): this;
1978
- mockResolvedValue(value: ResolveType<T>): this;
1979
- mockResolvedValueOnce(value: ResolveType<T>): this;
1980
- mockRejectedValue(value: RejectType<T>): this;
1981
- mockRejectedValueOnce(value: RejectType<T>): this;
1982
- }
1983
-
1984
- // export type MockMetadata<T, MetadataType = MockMetadataType> = {
1985
- // ref?: number;
1986
- // members?: Record<string, MockMetadata<T>>;
1987
- // mockImpl?: T;
1988
- // name?: string;
1989
- // refID?: number;
1990
- // type?: MetadataType;
1991
- // value?: T;
1992
- // length?: number;
1993
- // };
1994
-
1995
- // export type MockMetadataType =
1996
- // | "object"
1997
- // | "array"
1998
- // | "regexp"
1999
- // | "function"
2000
- // | "constant"
2001
- // | "collection"
2002
- // | "null"
2003
- // | "undefined";
2004
-
2005
- // export class ModuleMocker {
2006
- // private readonly _environmentGlobal;
2007
- // private _mockState;
2008
- // private _mockConfigRegistry;
2009
- // private _spyState;
2010
- // private _invocationCallCounter;
2011
- // /**
2012
- // * @see README.md
2013
- // * @param global Global object of the test environment, used to create
2014
- // * mocks
2015
- // */
2016
- // constructor(global: typeof globalThis);
2017
- // private _getSlots;
2018
- // private _ensureMockConfig;
2019
- // private _ensureMockState;
2020
- // private _defaultMockConfig;
2021
- // private _defaultMockState;
2022
- // private _makeComponent;
2023
- // private _createMockFunction;
2024
- // private _generateMock;
2025
- // /**
2026
- // * Check whether the given property of an object has been already replaced.
2027
- // */
2028
- // private _findReplacedProperty;
2029
- // /**
2030
- // * @see README.md
2031
- // * @param metadata Metadata for the mock in the schema returned by the
2032
- // * getMetadata method of this module.
2033
- // */
2034
- // generateFromMetadata<T>(metadata: MockMetadata<T>): Mocked<T>;
2035
- // /**
2036
- // * @see README.md
2037
- // * @param component The component for which to retrieve metadata.
2038
- // */
2039
- // getMetadata<T = unknown>(
2040
- // component: T,
2041
- // _refs?: Map<T, number>,
2042
- // ): MockMetadata<T> | null;
2043
- // isMockFunction<T extends FunctionLike = UnknownFunction>(
2044
- // fn: MockInstance<T>,
2045
- // ): fn is MockInstance<T>;
2046
- // isMockFunction<P extends Array<unknown>, R>(
2047
- // fn: (...args: P) => R,
2048
- // ): fn is Mock<(...args: P) => R>;
2049
- // isMockFunction(fn: unknown): fn is Mock<UnknownFunction>;
2050
- // fn<T extends FunctionLike = UnknownFunction>(implementation?: T): Mock<T>;
2051
- // private _attachMockImplementation;
2052
- // spyOn<
2053
- // T extends object,
2054
- // K extends PropertyLikeKeys<T>,
2055
- // A extends "get" | "set",
2056
- // >(
2057
- // object: T,
2058
- // methodKey: K,
2059
- // accessType: A,
2060
- // ): A extends "get"
2061
- // ? SpiedGetter<T[K]>
2062
- // : A extends "set"
2063
- // ? SpiedSetter<T[K]>
2064
- // : never;
2065
- // spyOn<
2066
- // T extends object,
2067
- // K extends ConstructorLikeKeys<T> | MethodLikeKeys<T>,
2068
- // V extends Required<T>[K],
2069
- // >(
2070
- // object: T,
2071
- // methodKey: K,
2072
- // ): V extends ClassLike | FunctionLike ? Spied<V> : never;
2073
- // private _spyOnProperty;
2074
- // replaceProperty<
2075
- // T extends object,
2076
- // K extends PropertyLikeKeys<T>,
2077
- // V extends T[K],
2078
- // >(object: T, propertyKey: K, value: V): Replaced<T[K]>;
2079
- // clearAllMocks(): void;
2080
- // resetAllMocks(): void;
2081
- // restoreAllMocks(): void;
2082
- // private _typeOf;
2083
- // mocked<T extends object>(
2084
- // source: T,
2085
- // options?: {
2086
- // shallow: false;
2087
- // },
2088
- // ): Mocked<T>;
2089
- // mocked<T extends object>(
2090
- // source: T,
2091
- // options: {
2092
- // shallow: true;
2093
- // },
2094
- // ): MockedShallow<T>;
2095
- // }
2096
-
2097
- export type PropertyLikeKeys<T> = Exclude<keyof T, ConstructorLikeKeys<T> | MethodLikeKeys<T>>;
2098
-
2099
- export type RejectType<T extends FunctionLike> = ReturnType<T> extends PromiseLike<any> ? unknown : never;
2100
-
2101
- export interface Replaced<T = unknown> {
2102
- /**
2103
- * Restore property to its original value known at the time of mocking.
2104
- */
2105
- restore(): void;
2106
- /**
2107
- * Change the value of the property.
2108
- */
2109
- replaceValue(value: T): this;
2110
- }
2111
-
2112
- export function replaceProperty<
2113
- T extends object,
2114
- K_2 extends Exclude<
2115
- keyof T,
2116
- | keyof {
2117
- [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
2118
- }
2119
- | keyof {
2120
- [K_1 in keyof T as Required<T>[K_1] extends FunctionLike ? K_1 : never]: T[K_1];
2121
- }
2122
- >,
2123
- V extends T[K_2],
2124
- >(object: T, propertyKey: K_2, value: V): Replaced<T[K_2]>;
2125
-
2126
- export type ResolveType<T extends FunctionLike> = ReturnType<T> extends PromiseLike<infer U> ? U : never;
2127
-
2128
- export type Spied<T extends ClassLike | FunctionLike> = T extends ClassLike
2129
- ? SpiedClass<T>
2130
- : T extends FunctionLike
2131
- ? SpiedFunction<T>
2132
- : never;
2133
-
2134
- export type SpiedClass<T extends ClassLike = UnknownClass> = MockInstance<
2135
- (...args: ConstructorParameters<T>) => InstanceType<T>
2136
- >;
2137
-
2138
- export type SpiedFunction<T extends FunctionLike = UnknownFunction> = MockInstance<
2139
- (...args: Parameters<T>) => ReturnType<T>
2140
- >;
1791
+ namespace JestMock {
1792
+ /**
1793
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
1794
+ *
1795
+ * This source code is licensed under the MIT license found in the
1796
+ * LICENSE file in the root directory of this source tree.
1797
+ */
1798
+ export interface ClassLike {
1799
+ new (...args: any): any;
1800
+ }
1801
+
1802
+ export type ConstructorLikeKeys<T> = keyof {
1803
+ [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
1804
+ };
1805
+
1806
+ // export const fn: <T extends FunctionLike = UnknownFunction>(
1807
+ // implementation?: T | undefined,
1808
+ // ) => Mock<T>;
1809
+
1810
+ export type FunctionLike = (...args: any) => any;
1811
+
1812
+ export type MethodLikeKeys<T> = keyof {
1813
+ [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K];
1814
+ };
1815
+
1816
+ export interface Mock<T extends (...args: any[]) => any> extends MockInstance<T> {
1817
+ (...args: Parameters<T>): ReturnType<T>;
1818
+ }
1819
+
1820
+ /**
1821
+ * All what the internal typings need is to be sure that we have any-function.
1822
+ * `FunctionLike` type ensures that and helps to constrain the type as well.
1823
+ * The default of `UnknownFunction` makes sure that `any`s do not leak to the
1824
+ * user side. For instance, calling `fn()` without implementation will return
1825
+ * a mock of `(...args: Array<unknown>) => unknown` type. If implementation
1826
+ * is provided, its typings are inferred correctly.
1827
+ */
1828
+ // export interface Mock<T extends FunctionLike = UnknownFunction>
1829
+ // extends Function,
1830
+ // MockInstance<T> {
1831
+ // new (...args: Parameters<T>): ReturnType<T>;
1832
+ // (...args: Parameters<T>): ReturnType<T>;
1833
+ // }
1834
+
1835
+ // export type Mocked<T> = T extends ClassLike
1836
+ // ? MockedClass<T>
1837
+ // : T extends FunctionLike
1838
+ // ? MockedFunction<T>
1839
+ // : T extends object
1840
+ // ? MockedObject<T>
1841
+ // : T;
1842
+
1843
+ // export const mocked: {
1844
+ // <T extends object>(
1845
+ // source: T,
1846
+ // options?: {
1847
+ // shallow: false;
1848
+ // },
1849
+ // ): Mocked<T>;
1850
+ // <T_1 extends object>(
1851
+ // source: T_1,
1852
+ // options: {
1853
+ // shallow: true;
1854
+ // },
1855
+ // ): MockedShallow<T_1>;
1856
+ // };
1857
+
1858
+ // export type MockedClass<T extends ClassLike> = MockInstance<
1859
+ // (...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>
1860
+ // > &
1861
+ // MockedObject<T>;
1862
+
1863
+ // export type MockedFunction<T extends FunctionLike> = MockInstance<T> &
1864
+ // MockedObject<T>;
1865
+
1866
+ // type MockedFunctionShallow<T extends FunctionLike> = MockInstance<T> & T;
1867
+
1868
+ // export type MockedObject<T extends object> = {
1869
+ // [K in keyof T]: T[K] extends ClassLike
1870
+ // ? MockedClass<T[K]>
1871
+ // : T[K] extends FunctionLike
1872
+ // ? MockedFunction<T[K]>
1873
+ // : T[K] extends object
1874
+ // ? MockedObject<T[K]>
1875
+ // : T[K];
1876
+ // } & T;
1877
+
1878
+ // type MockedObjectShallow<T extends object> = {
1879
+ // [K in keyof T]: T[K] extends ClassLike
1880
+ // ? MockedClass<T[K]>
1881
+ // : T[K] extends FunctionLike
1882
+ // ? MockedFunctionShallow<T[K]>
1883
+ // : T[K];
1884
+ // } & T;
1885
+
1886
+ // export type MockedShallow<T> = T extends ClassLike
1887
+ // ? MockedClass<T>
1888
+ // : T extends FunctionLike
1889
+ // ? MockedFunctionShallow<T>
1890
+ // : T extends object
1891
+ // ? MockedObjectShallow<T>
1892
+ // : T;
1893
+
1894
+ // export type MockFunctionMetadata<
1895
+ // T = unknown,
1896
+ // MetadataType = MockMetadataType,
1897
+ // > = MockMetadata<T, MetadataType>;
1898
+
1899
+ // export type MockFunctionMetadataType = MockMetadataType;
1900
+
1901
+ type MockFunctionResult<T extends FunctionLike = UnknownFunction> =
1902
+ | MockFunctionResultIncomplete
1903
+ | MockFunctionResultReturn<T>
1904
+ | MockFunctionResultThrow;
1905
+
1906
+ interface MockFunctionResultIncomplete {
1907
+ type: "incomplete";
1908
+ /**
1909
+ * Result of a single call to a mock function that has not yet completed.
1910
+ * This occurs if you test the result from within the mock function itself,
1911
+ * or from within a function that was called by the mock.
1912
+ */
1913
+ value: undefined;
1914
+ }
2141
1915
 
2142
- export type SpiedGetter<T> = MockInstance<() => T>;
1916
+ interface MockFunctionResultReturn<T extends FunctionLike = UnknownFunction> {
1917
+ type: "return";
1918
+ /**
1919
+ * Result of a single call to a mock function that returned.
1920
+ */
1921
+ value: ReturnType<T>;
1922
+ }
2143
1923
 
2144
- export type SpiedSetter<T> = MockInstance<(arg: T) => void>;
1924
+ interface MockFunctionResultThrow {
1925
+ type: "throw";
1926
+ /**
1927
+ * Result of a single call to a mock function that threw.
1928
+ */
1929
+ value: unknown;
1930
+ }
2145
1931
 
2146
- export interface SpyInstance<T extends FunctionLike = UnknownFunction> extends MockInstance<T> {}
1932
+ interface MockFunctionState<T extends FunctionLike = FunctionLike> {
1933
+ /**
1934
+ * List of the call arguments of all calls that have been made to the mock.
1935
+ */
1936
+ calls: Array<Parameters<T>>;
1937
+ /**
1938
+ * List of all the object instances that have been instantiated from the mock.
1939
+ */
1940
+ instances: Array<ReturnType<T>>;
1941
+ /**
1942
+ * List of all the function contexts that have been applied to calls to the mock.
1943
+ */
1944
+ contexts: Array<ThisParameterType<T>>;
1945
+ /**
1946
+ * List of the call order indexes of the mock. Jest is indexing the order of
1947
+ * invocations of all mocks in a test file. The index is starting with `1`.
1948
+ */
1949
+ invocationCallOrder: number[];
1950
+ /**
1951
+ * List of the call arguments of the last call that was made to the mock.
1952
+ * If the function was not called, it will return `undefined`.
1953
+ */
1954
+ lastCall?: Parameters<T>;
1955
+ /**
1956
+ * List of the results of all calls that have been made to the mock.
1957
+ */
1958
+ results: Array<MockFunctionResult<T>>;
1959
+ }
1960
+
1961
+ export interface MockInstance<T extends FunctionLike = UnknownFunction> {
1962
+ _isMockFunction: true;
1963
+ _protoImpl: Function;
1964
+ getMockImplementation(): T | undefined;
1965
+ getMockName(): string;
1966
+ mock: MockFunctionState<T>;
1967
+ mockClear(): this;
1968
+ mockReset(): this;
1969
+ mockRestore(): void;
1970
+ mockImplementation(fn: T): this;
1971
+ mockImplementationOnce(fn: T): this;
1972
+ withImplementation(fn: T, callback: () => Promise<unknown>): Promise<void>;
1973
+ withImplementation(fn: T, callback: () => void): void;
1974
+ mockName(name: string): this;
1975
+ mockReturnThis(): this;
1976
+ mockReturnValue(value: ReturnType<T>): this;
1977
+ mockReturnValueOnce(value: ReturnType<T>): this;
1978
+ mockResolvedValue(value: ResolveType<T>): this;
1979
+ mockResolvedValueOnce(value: ResolveType<T>): this;
1980
+ mockRejectedValue(value: RejectType<T>): this;
1981
+ mockRejectedValueOnce(value: RejectType<T>): this;
1982
+ }
1983
+
1984
+ // export type MockMetadata<T, MetadataType = MockMetadataType> = {
1985
+ // ref?: number;
1986
+ // members?: Record<string, MockMetadata<T>>;
1987
+ // mockImpl?: T;
1988
+ // name?: string;
1989
+ // refID?: number;
1990
+ // type?: MetadataType;
1991
+ // value?: T;
1992
+ // length?: number;
1993
+ // };
1994
+
1995
+ // export type MockMetadataType =
1996
+ // | "object"
1997
+ // | "array"
1998
+ // | "regexp"
1999
+ // | "function"
2000
+ // | "constant"
2001
+ // | "collection"
2002
+ // | "null"
2003
+ // | "undefined";
2004
+
2005
+ // export class ModuleMocker {
2006
+ // private readonly _environmentGlobal;
2007
+ // private _mockState;
2008
+ // private _mockConfigRegistry;
2009
+ // private _spyState;
2010
+ // private _invocationCallCounter;
2011
+ // /**
2012
+ // * @see README.md
2013
+ // * @param global Global object of the test environment, used to create
2014
+ // * mocks
2015
+ // */
2016
+ // constructor(global: typeof globalThis);
2017
+ // private _getSlots;
2018
+ // private _ensureMockConfig;
2019
+ // private _ensureMockState;
2020
+ // private _defaultMockConfig;
2021
+ // private _defaultMockState;
2022
+ // private _makeComponent;
2023
+ // private _createMockFunction;
2024
+ // private _generateMock;
2025
+ // /**
2026
+ // * Check whether the given property of an object has been already replaced.
2027
+ // */
2028
+ // private _findReplacedProperty;
2029
+ // /**
2030
+ // * @see README.md
2031
+ // * @param metadata Metadata for the mock in the schema returned by the
2032
+ // * getMetadata method of this module.
2033
+ // */
2034
+ // generateFromMetadata<T>(metadata: MockMetadata<T>): Mocked<T>;
2035
+ // /**
2036
+ // * @see README.md
2037
+ // * @param component The component for which to retrieve metadata.
2038
+ // */
2039
+ // getMetadata<T = unknown>(
2040
+ // component: T,
2041
+ // _refs?: Map<T, number>,
2042
+ // ): MockMetadata<T> | null;
2043
+ // isMockFunction<T extends FunctionLike = UnknownFunction>(
2044
+ // fn: MockInstance<T>,
2045
+ // ): fn is MockInstance<T>;
2046
+ // isMockFunction<P extends Array<unknown>, R>(
2047
+ // fn: (...args: P) => R,
2048
+ // ): fn is Mock<(...args: P) => R>;
2049
+ // isMockFunction(fn: unknown): fn is Mock<UnknownFunction>;
2050
+ // fn<T extends FunctionLike = UnknownFunction>(implementation?: T): Mock<T>;
2051
+ // private _attachMockImplementation;
2052
+ // spyOn<
2053
+ // T extends object,
2054
+ // K extends PropertyLikeKeys<T>,
2055
+ // A extends "get" | "set",
2056
+ // >(
2057
+ // object: T,
2058
+ // methodKey: K,
2059
+ // accessType: A,
2060
+ // ): A extends "get"
2061
+ // ? SpiedGetter<T[K]>
2062
+ // : A extends "set"
2063
+ // ? SpiedSetter<T[K]>
2064
+ // : never;
2065
+ // spyOn<
2066
+ // T extends object,
2067
+ // K extends ConstructorLikeKeys<T> | MethodLikeKeys<T>,
2068
+ // V extends Required<T>[K],
2069
+ // >(
2070
+ // object: T,
2071
+ // methodKey: K,
2072
+ // ): V extends ClassLike | FunctionLike ? Spied<V> : never;
2073
+ // private _spyOnProperty;
2074
+ // replaceProperty<
2075
+ // T extends object,
2076
+ // K extends PropertyLikeKeys<T>,
2077
+ // V extends T[K],
2078
+ // >(object: T, propertyKey: K, value: V): Replaced<T[K]>;
2079
+ // clearAllMocks(): void;
2080
+ // resetAllMocks(): void;
2081
+ // restoreAllMocks(): void;
2082
+ // private _typeOf;
2083
+ // mocked<T extends object>(
2084
+ // source: T,
2085
+ // options?: {
2086
+ // shallow: false;
2087
+ // },
2088
+ // ): Mocked<T>;
2089
+ // mocked<T extends object>(
2090
+ // source: T,
2091
+ // options: {
2092
+ // shallow: true;
2093
+ // },
2094
+ // ): MockedShallow<T>;
2095
+ // }
2096
+
2097
+ export type PropertyLikeKeys<T> = Exclude<keyof T, ConstructorLikeKeys<T> | MethodLikeKeys<T>>;
2098
+
2099
+ export type RejectType<T extends FunctionLike> = ReturnType<T> extends PromiseLike<any> ? unknown : never;
2100
+
2101
+ export interface Replaced<T = unknown> {
2102
+ /**
2103
+ * Restore property to its original value known at the time of mocking.
2104
+ */
2105
+ restore(): void;
2106
+ /**
2107
+ * Change the value of the property.
2108
+ */
2109
+ replaceValue(value: T): this;
2110
+ }
2147
2111
 
2148
- export const spyOn: {
2149
- <
2112
+ export function replaceProperty<
2150
2113
  T extends object,
2151
2114
  K_2 extends Exclude<
2152
2115
  keyof T,
@@ -2157,32 +2120,70 @@ declare namespace JestMock {
2157
2120
  [K_1 in keyof T as Required<T>[K_1] extends FunctionLike ? K_1 : never]: T[K_1];
2158
2121
  }
2159
2122
  >,
2160
- V extends Required<T>[K_2],
2161
- A extends "set" | "get",
2162
- >(
2163
- object: T,
2164
- methodKey: K_2,
2165
- accessType: A,
2166
- ): A extends "get" ? SpiedGetter<V> : A extends "set" ? SpiedSetter<V> : never;
2167
- <
2168
- T_1 extends object,
2169
- K_5 extends
2170
- | keyof {
2171
- [K_3 in keyof T_1 as Required<T_1>[K_3] extends ClassLike ? K_3 : never]: T_1[K_3];
2172
- }
2173
- | keyof {
2174
- [K_4 in keyof T_1 as Required<T_1>[K_4] extends FunctionLike ? K_4 : never]: T_1[K_4];
2175
- },
2176
- V_1 extends Required<T_1>[K_5],
2177
- >(
2178
- object: T_1,
2179
- methodKey: K_5,
2180
- ): V_1 extends ClassLike | FunctionLike ? Spied<V_1> : never;
2181
- };
2182
-
2183
- export interface UnknownClass {
2184
- new (...args: unknown[]): unknown;
2123
+ V extends T[K_2],
2124
+ >(object: T, propertyKey: K_2, value: V): Replaced<T[K_2]>;
2125
+
2126
+ export type ResolveType<T extends FunctionLike> = ReturnType<T> extends PromiseLike<infer U> ? U : never;
2127
+
2128
+ export type Spied<T extends ClassLike | FunctionLike> = T extends ClassLike
2129
+ ? SpiedClass<T>
2130
+ : T extends FunctionLike
2131
+ ? SpiedFunction<T>
2132
+ : never;
2133
+
2134
+ export type SpiedClass<T extends ClassLike = UnknownClass> = MockInstance<
2135
+ (...args: ConstructorParameters<T>) => InstanceType<T>
2136
+ >;
2137
+
2138
+ export type SpiedFunction<T extends FunctionLike = UnknownFunction> = MockInstance<
2139
+ (...args: Parameters<T>) => ReturnType<T>
2140
+ >;
2141
+
2142
+ export type SpiedGetter<T> = MockInstance<() => T>;
2143
+
2144
+ export type SpiedSetter<T> = MockInstance<(arg: T) => void>;
2145
+
2146
+ export interface SpyInstance<T extends FunctionLike = UnknownFunction> extends MockInstance<T> {}
2147
+
2148
+ export const spyOn: {
2149
+ <
2150
+ T extends object,
2151
+ K_2 extends Exclude<
2152
+ keyof T,
2153
+ | keyof {
2154
+ [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
2155
+ }
2156
+ | keyof {
2157
+ [K_1 in keyof T as Required<T>[K_1] extends FunctionLike ? K_1 : never]: T[K_1];
2158
+ }
2159
+ >,
2160
+ V extends Required<T>[K_2],
2161
+ A extends "set" | "get",
2162
+ >(
2163
+ object: T,
2164
+ methodKey: K_2,
2165
+ accessType: A,
2166
+ ): A extends "get" ? SpiedGetter<V> : A extends "set" ? SpiedSetter<V> : never;
2167
+ <
2168
+ T_1 extends object,
2169
+ K_5 extends
2170
+ | keyof {
2171
+ [K_3 in keyof T_1 as Required<T_1>[K_3] extends ClassLike ? K_3 : never]: T_1[K_3];
2172
+ }
2173
+ | keyof {
2174
+ [K_4 in keyof T_1 as Required<T_1>[K_4] extends FunctionLike ? K_4 : never]: T_1[K_4];
2175
+ },
2176
+ V_1 extends Required<T_1>[K_5],
2177
+ >(
2178
+ object: T_1,
2179
+ methodKey: K_5,
2180
+ ): V_1 extends ClassLike | FunctionLike ? Spied<V_1> : never;
2181
+ };
2182
+
2183
+ export interface UnknownClass {
2184
+ new (...args: unknown[]): unknown;
2185
+ }
2186
+
2187
+ export type UnknownFunction = (...args: unknown[]) => unknown;
2185
2188
  }
2186
-
2187
- export type UnknownFunction = (...args: unknown[]) => unknown;
2188
2189
  }