mirror of
https://github.com/xfarrow/blink
synced 2025-06-27 09:03:02 +02:00
Change endpoint from persons to people
This commit is contained in:
21
backend/apis/nodejs/node_modules/jest-mock/LICENSE
generated
vendored
Normal file
21
backend/apis/nodejs/node_modules/jest-mock/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
106
backend/apis/nodejs/node_modules/jest-mock/README.md
generated
vendored
Normal file
106
backend/apis/nodejs/node_modules/jest-mock/README.md
generated
vendored
Normal file
@ -0,0 +1,106 @@
|
||||
# jest-mock
|
||||
|
||||
**Note:** More details on user side API can be found in [Jest documentation](https://jestjs.io/docs/mock-function-api).
|
||||
|
||||
## API
|
||||
|
||||
```js
|
||||
import {ModuleMocker} from 'jest-mock';
|
||||
```
|
||||
|
||||
### `constructor(global)`
|
||||
|
||||
Creates a new module mocker that generates mocks as if they were created in an environment with the given global object.
|
||||
|
||||
### `generateFromMetadata(metadata)`
|
||||
|
||||
Generates a mock based on the given metadata (Metadata for the mock in the schema returned by the `getMetadata()` method of this module). Mocks treat functions specially, and all mock functions have additional members, described in the documentation for `fn()` in this module.
|
||||
|
||||
One important note: function prototypes are handled specially by this mocking framework. For functions with prototypes, when called as a constructor, the mock will install mocked function members on the instance. This allows different instances of the same constructor to have different values for its mocks member and its return values.
|
||||
|
||||
### `getMetadata(component)`
|
||||
|
||||
Inspects the argument and returns its schema in the following recursive format:
|
||||
|
||||
```
|
||||
{
|
||||
type: ...
|
||||
members: {}
|
||||
}
|
||||
```
|
||||
|
||||
Where type is one of `array`, `object`, `function`, or `ref`, and members is an optional dictionary where the keys are member names and the values are metadata objects. Function prototypes are defined by defining metadata for the `member.prototype` of the function. The type of a function prototype should always be `object`. For instance, a class might be defined like this:
|
||||
|
||||
```js
|
||||
const classDef = {
|
||||
type: 'function',
|
||||
members: {
|
||||
staticMethod: {type: 'function'},
|
||||
prototype: {
|
||||
type: 'object',
|
||||
members: {
|
||||
instanceMethod: {type: 'function'},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
Metadata may also contain references to other objects defined within the same metadata object. The metadata for the referent must be marked with `refID` key and an arbitrary value. The referrer must be marked with a `ref` key that has the same value as object with refID that it refers to. For instance, this metadata blob:
|
||||
|
||||
```js
|
||||
const refID = {
|
||||
type: 'object',
|
||||
refID: 1,
|
||||
members: {
|
||||
self: {ref: 1},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
Defines an object with a slot named `self` that refers back to the object.
|
||||
|
||||
### `fn(implementation?)`
|
||||
|
||||
Generates a stand-alone function with members that help drive unit tests or confirm expectations. Specifically, functions returned by this method have the following members:
|
||||
|
||||
##### `.mock`
|
||||
|
||||
An object with three members, `calls`, `instances` and `invocationCallOrder`, which are all lists. The items in the `calls` list are the arguments with which the function was called. The "instances" list stores the value of 'this' for each call to the function. This is useful for retrieving instances from a constructor. The `invocationCallOrder` lists the order in which the mock was called in relation to all mock calls, starting at 1.
|
||||
|
||||
##### `.mockReturnValueOnce(value)`
|
||||
|
||||
Pushes the given value onto a FIFO queue of return values for the function.
|
||||
|
||||
##### `.mockReturnValue(value)`
|
||||
|
||||
Sets the default return value for the function.
|
||||
|
||||
##### `.mockImplementationOnce(function)`
|
||||
|
||||
Pushes the given mock implementation onto a FIFO queue of mock implementations for the function.
|
||||
|
||||
##### `.mockImplementation(function)`
|
||||
|
||||
Sets the default mock implementation for the function.
|
||||
|
||||
##### `.mockReturnThis()`
|
||||
|
||||
Syntactic sugar for:
|
||||
|
||||
```js
|
||||
mockFn.mockImplementation(function () {
|
||||
return this;
|
||||
});
|
||||
```
|
||||
|
||||
In case both `.mockImplementationOnce()` / `.mockImplementation()` and `.mockReturnValueOnce()` / `.mockReturnValue()` are called. The priority of which to use is based on what is the last call:
|
||||
|
||||
- if the last call is `.mockReturnValueOnce()` or `.mockReturnValue()`, use the specific return value or default return value. If specific return values are used up or no default return value is set, fall back to try `.mockImplementation()`;
|
||||
- if the last call is `.mockImplementationOnce()` or `.mockImplementation()`, run the specific implementation and return the result or run default implementation and return the result.
|
||||
|
||||
##### `.withImplementation(function, callback)`
|
||||
|
||||
Temporarily overrides the default mock implementation within the callback, then restores it's previous implementation.
|
||||
|
||||
If the callback is async or returns a `thenable`, `withImplementation` will return a promise. Awaiting the promise will await the callback and reset the implementation.
|
406
backend/apis/nodejs/node_modules/jest-mock/build/index.d.ts
generated
vendored
Normal file
406
backend/apis/nodejs/node_modules/jest-mock/build/index.d.ts
generated
vendored
Normal file
@ -0,0 +1,406 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
export declare type ClassLike = {
|
||||
new (...args: any): any;
|
||||
};
|
||||
|
||||
export declare type ConstructorLikeKeys<T> = keyof {
|
||||
[K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
|
||||
};
|
||||
|
||||
export declare const fn: <T extends FunctionLike = UnknownFunction>(
|
||||
implementation?: T | undefined,
|
||||
) => Mock<T>;
|
||||
|
||||
export declare type FunctionLike = (...args: any) => any;
|
||||
|
||||
export declare type MethodLikeKeys<T> = keyof {
|
||||
[K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K];
|
||||
};
|
||||
|
||||
/**
|
||||
* All what the internal typings need is to be sure that we have any-function.
|
||||
* `FunctionLike` type ensures that and helps to constrain the type as well.
|
||||
* The default of `UnknownFunction` makes sure that `any`s do not leak to the
|
||||
* user side. For instance, calling `fn()` without implementation will return
|
||||
* a mock of `(...args: Array<unknown>) => unknown` type. If implementation
|
||||
* is provided, its typings are inferred correctly.
|
||||
*/
|
||||
export declare interface Mock<T extends FunctionLike = UnknownFunction>
|
||||
extends Function,
|
||||
MockInstance<T> {
|
||||
new (...args: Parameters<T>): ReturnType<T>;
|
||||
(...args: Parameters<T>): ReturnType<T>;
|
||||
}
|
||||
|
||||
export declare type Mocked<T> = T extends ClassLike
|
||||
? MockedClass<T>
|
||||
: T extends FunctionLike
|
||||
? MockedFunction<T>
|
||||
: T extends object
|
||||
? MockedObject<T>
|
||||
: T;
|
||||
|
||||
export declare const mocked: {
|
||||
<T extends object>(
|
||||
source: T,
|
||||
options?: {
|
||||
shallow: false;
|
||||
},
|
||||
): Mocked<T>;
|
||||
<T_1 extends object>(
|
||||
source: T_1,
|
||||
options: {
|
||||
shallow: true;
|
||||
},
|
||||
): MockedShallow<T_1>;
|
||||
};
|
||||
|
||||
export declare type MockedClass<T extends ClassLike> = MockInstance<
|
||||
(...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>
|
||||
> &
|
||||
MockedObject<T>;
|
||||
|
||||
export declare type MockedFunction<T extends FunctionLike> = MockInstance<T> &
|
||||
MockedObject<T>;
|
||||
|
||||
declare type MockedFunctionShallow<T extends FunctionLike> = MockInstance<T> &
|
||||
T;
|
||||
|
||||
export declare type MockedObject<T extends object> = {
|
||||
[K in keyof T]: T[K] extends ClassLike
|
||||
? MockedClass<T[K]>
|
||||
: T[K] extends FunctionLike
|
||||
? MockedFunction<T[K]>
|
||||
: T[K] extends object
|
||||
? MockedObject<T[K]>
|
||||
: T[K];
|
||||
} & T;
|
||||
|
||||
declare type MockedObjectShallow<T extends object> = {
|
||||
[K in keyof T]: T[K] extends ClassLike
|
||||
? MockedClass<T[K]>
|
||||
: T[K] extends FunctionLike
|
||||
? MockedFunctionShallow<T[K]>
|
||||
: T[K];
|
||||
} & T;
|
||||
|
||||
export declare type MockedShallow<T> = T extends ClassLike
|
||||
? MockedClass<T>
|
||||
: T extends FunctionLike
|
||||
? MockedFunctionShallow<T>
|
||||
: T extends object
|
||||
? MockedObjectShallow<T>
|
||||
: T;
|
||||
|
||||
export declare type MockFunctionMetadata<
|
||||
T = unknown,
|
||||
MetadataType = MockMetadataType,
|
||||
> = MockMetadata<T, MetadataType>;
|
||||
|
||||
export declare type MockFunctionMetadataType = MockMetadataType;
|
||||
|
||||
declare type MockFunctionResult<T extends FunctionLike = UnknownFunction> =
|
||||
| MockFunctionResultIncomplete
|
||||
| MockFunctionResultReturn<T>
|
||||
| MockFunctionResultThrow;
|
||||
|
||||
declare type MockFunctionResultIncomplete = {
|
||||
type: 'incomplete';
|
||||
/**
|
||||
* Result of a single call to a mock function that has not yet completed.
|
||||
* This occurs if you test the result from within the mock function itself,
|
||||
* or from within a function that was called by the mock.
|
||||
*/
|
||||
value: undefined;
|
||||
};
|
||||
|
||||
declare type MockFunctionResultReturn<
|
||||
T extends FunctionLike = UnknownFunction,
|
||||
> = {
|
||||
type: 'return';
|
||||
/**
|
||||
* Result of a single call to a mock function that returned.
|
||||
*/
|
||||
value: ReturnType<T>;
|
||||
};
|
||||
|
||||
declare type MockFunctionResultThrow = {
|
||||
type: 'throw';
|
||||
/**
|
||||
* Result of a single call to a mock function that threw.
|
||||
*/
|
||||
value: unknown;
|
||||
};
|
||||
|
||||
declare type MockFunctionState<T extends FunctionLike = UnknownFunction> = {
|
||||
/**
|
||||
* List of the call arguments of all calls that have been made to the mock.
|
||||
*/
|
||||
calls: Array<Parameters<T>>;
|
||||
/**
|
||||
* List of all the object instances that have been instantiated from the mock.
|
||||
*/
|
||||
instances: Array<ReturnType<T>>;
|
||||
/**
|
||||
* List of all the function contexts that have been applied to calls to the mock.
|
||||
*/
|
||||
contexts: Array<ThisParameterType<T>>;
|
||||
/**
|
||||
* List of the call order indexes of the mock. Jest is indexing the order of
|
||||
* invocations of all mocks in a test file. The index is starting with `1`.
|
||||
*/
|
||||
invocationCallOrder: Array<number>;
|
||||
/**
|
||||
* List of the call arguments of the last call that was made to the mock.
|
||||
* If the function was not called, it will return `undefined`.
|
||||
*/
|
||||
lastCall?: Parameters<T>;
|
||||
/**
|
||||
* List of the results of all calls that have been made to the mock.
|
||||
*/
|
||||
results: Array<MockFunctionResult<T>>;
|
||||
};
|
||||
|
||||
export declare interface MockInstance<
|
||||
T extends FunctionLike = UnknownFunction,
|
||||
> {
|
||||
_isMockFunction: true;
|
||||
_protoImpl: Function;
|
||||
getMockImplementation(): T | undefined;
|
||||
getMockName(): string;
|
||||
mock: MockFunctionState<T>;
|
||||
mockClear(): this;
|
||||
mockReset(): this;
|
||||
mockRestore(): void;
|
||||
mockImplementation(fn: T): this;
|
||||
mockImplementationOnce(fn: T): this;
|
||||
withImplementation(fn: T, callback: () => Promise<unknown>): Promise<void>;
|
||||
withImplementation(fn: T, callback: () => void): void;
|
||||
mockName(name: string): this;
|
||||
mockReturnThis(): this;
|
||||
mockReturnValue(value: ReturnType<T>): this;
|
||||
mockReturnValueOnce(value: ReturnType<T>): this;
|
||||
mockResolvedValue(value: ResolveType<T>): this;
|
||||
mockResolvedValueOnce(value: ResolveType<T>): this;
|
||||
mockRejectedValue(value: RejectType<T>): this;
|
||||
mockRejectedValueOnce(value: RejectType<T>): this;
|
||||
}
|
||||
|
||||
export declare type MockMetadata<T, MetadataType = MockMetadataType> = {
|
||||
ref?: number;
|
||||
members?: Record<string, MockMetadata<T>>;
|
||||
mockImpl?: T;
|
||||
name?: string;
|
||||
refID?: number;
|
||||
type?: MetadataType;
|
||||
value?: T;
|
||||
length?: number;
|
||||
};
|
||||
|
||||
export declare type MockMetadataType =
|
||||
| 'object'
|
||||
| 'array'
|
||||
| 'regexp'
|
||||
| 'function'
|
||||
| 'constant'
|
||||
| 'collection'
|
||||
| 'null'
|
||||
| 'undefined';
|
||||
|
||||
export declare class ModuleMocker {
|
||||
private readonly _environmentGlobal;
|
||||
private _mockState;
|
||||
private _mockConfigRegistry;
|
||||
private _spyState;
|
||||
private _invocationCallCounter;
|
||||
/**
|
||||
* @see README.md
|
||||
* @param global Global object of the test environment, used to create
|
||||
* mocks
|
||||
*/
|
||||
constructor(global: typeof globalThis);
|
||||
private _getSlots;
|
||||
private _ensureMockConfig;
|
||||
private _ensureMockState;
|
||||
private _defaultMockConfig;
|
||||
private _defaultMockState;
|
||||
private _makeComponent;
|
||||
private _createMockFunction;
|
||||
private _generateMock;
|
||||
/**
|
||||
* Check whether the given property of an object has been already replaced.
|
||||
*/
|
||||
private _findReplacedProperty;
|
||||
/**
|
||||
* @see README.md
|
||||
* @param metadata Metadata for the mock in the schema returned by the
|
||||
* getMetadata method of this module.
|
||||
*/
|
||||
generateFromMetadata<T>(metadata: MockMetadata<T>): Mocked<T>;
|
||||
/**
|
||||
* @see README.md
|
||||
* @param component The component for which to retrieve metadata.
|
||||
*/
|
||||
getMetadata<T = unknown>(
|
||||
component: T,
|
||||
_refs?: Map<T, number>,
|
||||
): MockMetadata<T> | null;
|
||||
isMockFunction<T extends FunctionLike = UnknownFunction>(
|
||||
fn: MockInstance<T>,
|
||||
): fn is MockInstance<T>;
|
||||
isMockFunction<P extends Array<unknown>, R>(
|
||||
fn: (...args: P) => R,
|
||||
): fn is Mock<(...args: P) => R>;
|
||||
isMockFunction(fn: unknown): fn is Mock<UnknownFunction>;
|
||||
fn<T extends FunctionLike = UnknownFunction>(implementation?: T): Mock<T>;
|
||||
spyOn<
|
||||
T extends object,
|
||||
K extends PropertyLikeKeys<T>,
|
||||
V extends Required<T>[K],
|
||||
A extends 'get' | 'set',
|
||||
>(
|
||||
object: T,
|
||||
methodKey: K,
|
||||
accessType: A,
|
||||
): A extends 'get'
|
||||
? SpiedGetter<V>
|
||||
: A extends 'set'
|
||||
? SpiedSetter<V>
|
||||
: never;
|
||||
spyOn<
|
||||
T extends object,
|
||||
K extends ConstructorLikeKeys<T> | MethodLikeKeys<T>,
|
||||
V extends Required<T>[K],
|
||||
>(
|
||||
object: T,
|
||||
methodKey: K,
|
||||
): V extends ClassLike | FunctionLike ? Spied<V> : never;
|
||||
private _spyOnProperty;
|
||||
replaceProperty<T extends object, K extends keyof T>(
|
||||
object: T,
|
||||
propertyKey: K,
|
||||
value: T[K],
|
||||
): Replaced<T[K]>;
|
||||
clearAllMocks(): void;
|
||||
resetAllMocks(): void;
|
||||
restoreAllMocks(): void;
|
||||
private _typeOf;
|
||||
mocked<T extends object>(
|
||||
source: T,
|
||||
options?: {
|
||||
shallow: false;
|
||||
},
|
||||
): Mocked<T>;
|
||||
mocked<T extends object>(
|
||||
source: T,
|
||||
options: {
|
||||
shallow: true;
|
||||
},
|
||||
): MockedShallow<T>;
|
||||
}
|
||||
|
||||
export declare type PropertyLikeKeys<T> = Exclude<
|
||||
keyof T,
|
||||
ConstructorLikeKeys<T> | MethodLikeKeys<T>
|
||||
>;
|
||||
|
||||
declare type RejectType<T extends FunctionLike> =
|
||||
ReturnType<T> extends PromiseLike<any> ? unknown : never;
|
||||
|
||||
export declare interface Replaced<T = unknown> {
|
||||
/**
|
||||
* Restore property to its original value known at the time of mocking.
|
||||
*/
|
||||
restore(): void;
|
||||
/**
|
||||
* Change the value of the property.
|
||||
*/
|
||||
replaceValue(value: T): this;
|
||||
}
|
||||
|
||||
export declare const replaceProperty: <T extends object, K extends keyof T>(
|
||||
object: T,
|
||||
propertyKey: K,
|
||||
value: T[K],
|
||||
) => Replaced<T[K]>;
|
||||
|
||||
declare type ResolveType<T extends FunctionLike> =
|
||||
ReturnType<T> extends PromiseLike<infer U> ? U : never;
|
||||
|
||||
export declare type Spied<T extends ClassLike | FunctionLike> =
|
||||
T extends ClassLike
|
||||
? SpiedClass<T>
|
||||
: T extends FunctionLike
|
||||
? SpiedFunction<T>
|
||||
: never;
|
||||
|
||||
export declare type SpiedClass<T extends ClassLike = UnknownClass> =
|
||||
MockInstance<(...args: ConstructorParameters<T>) => InstanceType<T>>;
|
||||
|
||||
export declare type SpiedFunction<T extends FunctionLike = UnknownFunction> =
|
||||
MockInstance<(...args: Parameters<T>) => ReturnType<T>>;
|
||||
|
||||
export declare type SpiedGetter<T> = MockInstance<() => T>;
|
||||
|
||||
export declare type SpiedSetter<T> = MockInstance<(arg: T) => void>;
|
||||
|
||||
export declare interface SpyInstance<T extends FunctionLike = UnknownFunction>
|
||||
extends MockInstance<T> {}
|
||||
|
||||
export declare const spyOn: {
|
||||
<
|
||||
T extends object,
|
||||
K_2 extends Exclude<
|
||||
keyof T,
|
||||
| keyof {
|
||||
[K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
|
||||
}
|
||||
| keyof {
|
||||
[K_1 in keyof T as Required<T>[K_1] extends FunctionLike
|
||||
? K_1
|
||||
: never]: T[K_1];
|
||||
}
|
||||
>,
|
||||
V extends Required<T>[K_2],
|
||||
A extends 'set' | 'get',
|
||||
>(
|
||||
object: T,
|
||||
methodKey: K_2,
|
||||
accessType: A,
|
||||
): A extends 'get'
|
||||
? SpiedGetter<V>
|
||||
: A extends 'set'
|
||||
? SpiedSetter<V>
|
||||
: never;
|
||||
<
|
||||
T_1 extends object,
|
||||
K_5 extends
|
||||
| keyof {
|
||||
[K_3 in keyof T_1 as Required<T_1>[K_3] extends ClassLike
|
||||
? K_3
|
||||
: never]: T_1[K_3];
|
||||
}
|
||||
| keyof {
|
||||
[K_4 in keyof T_1 as Required<T_1>[K_4] extends FunctionLike
|
||||
? K_4
|
||||
: never]: T_1[K_4];
|
||||
},
|
||||
V_1 extends Required<T_1>[K_5],
|
||||
>(
|
||||
object: T_1,
|
||||
methodKey: K_5,
|
||||
): V_1 extends ClassLike | FunctionLike ? Spied<V_1> : never;
|
||||
};
|
||||
|
||||
export declare type UnknownClass = {
|
||||
new (...args: Array<unknown>): unknown;
|
||||
};
|
||||
|
||||
export declare type UnknownFunction = (...args: Array<unknown>) => unknown;
|
||||
|
||||
export {};
|
978
backend/apis/nodejs/node_modules/jest-mock/build/index.js
generated
vendored
Normal file
978
backend/apis/nodejs/node_modules/jest-mock/build/index.js
generated
vendored
Normal file
@ -0,0 +1,978 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.spyOn =
|
||||
exports.replaceProperty =
|
||||
exports.mocked =
|
||||
exports.fn =
|
||||
exports.ModuleMocker =
|
||||
void 0;
|
||||
function _jestUtil() {
|
||||
const data = require('jest-util');
|
||||
_jestUtil = function () {
|
||||
return data;
|
||||
};
|
||||
return data;
|
||||
}
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/* eslint-disable local/ban-types-eventually, local/prefer-rest-params-eventually */
|
||||
|
||||
// TODO remove re-export in Jest 30
|
||||
|
||||
// TODO remove re-export in Jest 30
|
||||
|
||||
// TODO in Jest 30 remove `SpyInstance` in favour of `Spied`
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
/**
|
||||
* All what the internal typings need is to be sure that we have any-function.
|
||||
* `FunctionLike` type ensures that and helps to constrain the type as well.
|
||||
* The default of `UnknownFunction` makes sure that `any`s do not leak to the
|
||||
* user side. For instance, calling `fn()` without implementation will return
|
||||
* a mock of `(...args: Array<unknown>) => unknown` type. If implementation
|
||||
* is provided, its typings are inferred correctly.
|
||||
*/
|
||||
const MOCK_CONSTRUCTOR_NAME = 'mockConstructor';
|
||||
const FUNCTION_NAME_RESERVED_PATTERN = /[\s!-/:-@[-`{-~]/;
|
||||
const FUNCTION_NAME_RESERVED_REPLACE = new RegExp(
|
||||
FUNCTION_NAME_RESERVED_PATTERN.source,
|
||||
'g'
|
||||
);
|
||||
const RESERVED_KEYWORDS = new Set([
|
||||
'arguments',
|
||||
'await',
|
||||
'break',
|
||||
'case',
|
||||
'catch',
|
||||
'class',
|
||||
'const',
|
||||
'continue',
|
||||
'debugger',
|
||||
'default',
|
||||
'delete',
|
||||
'do',
|
||||
'else',
|
||||
'enum',
|
||||
'eval',
|
||||
'export',
|
||||
'extends',
|
||||
'false',
|
||||
'finally',
|
||||
'for',
|
||||
'function',
|
||||
'if',
|
||||
'implements',
|
||||
'import',
|
||||
'in',
|
||||
'instanceof',
|
||||
'interface',
|
||||
'let',
|
||||
'new',
|
||||
'null',
|
||||
'package',
|
||||
'private',
|
||||
'protected',
|
||||
'public',
|
||||
'return',
|
||||
'static',
|
||||
'super',
|
||||
'switch',
|
||||
'this',
|
||||
'throw',
|
||||
'true',
|
||||
'try',
|
||||
'typeof',
|
||||
'var',
|
||||
'void',
|
||||
'while',
|
||||
'with',
|
||||
'yield'
|
||||
]);
|
||||
function matchArity(fn, length) {
|
||||
let mockConstructor;
|
||||
switch (length) {
|
||||
case 1:
|
||||
mockConstructor = function (_a) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
case 2:
|
||||
mockConstructor = function (_a, _b) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
case 3:
|
||||
mockConstructor = function (_a, _b, _c) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
case 4:
|
||||
mockConstructor = function (_a, _b, _c, _d) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
case 5:
|
||||
mockConstructor = function (_a, _b, _c, _d, _e) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
case 6:
|
||||
mockConstructor = function (_a, _b, _c, _d, _e, _f) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
case 7:
|
||||
mockConstructor = function (_a, _b, _c, _d, _e, _f, _g) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
case 8:
|
||||
mockConstructor = function (_a, _b, _c, _d, _e, _f, _g, _h) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
case 9:
|
||||
mockConstructor = function (_a, _b, _c, _d, _e, _f, _g, _h, _i) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
default:
|
||||
mockConstructor = function () {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
break;
|
||||
}
|
||||
return mockConstructor;
|
||||
}
|
||||
function getObjectType(value) {
|
||||
return Object.prototype.toString.apply(value).slice(8, -1);
|
||||
}
|
||||
function getType(ref) {
|
||||
const typeName = getObjectType(ref);
|
||||
if (
|
||||
typeName === 'Function' ||
|
||||
typeName === 'AsyncFunction' ||
|
||||
typeName === 'GeneratorFunction' ||
|
||||
typeName === 'AsyncGeneratorFunction'
|
||||
) {
|
||||
return 'function';
|
||||
} else if (Array.isArray(ref)) {
|
||||
return 'array';
|
||||
} else if (typeName === 'Object' || typeName === 'Module') {
|
||||
return 'object';
|
||||
} else if (
|
||||
typeName === 'Number' ||
|
||||
typeName === 'String' ||
|
||||
typeName === 'Boolean' ||
|
||||
typeName === 'Symbol'
|
||||
) {
|
||||
return 'constant';
|
||||
} else if (
|
||||
typeName === 'Map' ||
|
||||
typeName === 'WeakMap' ||
|
||||
typeName === 'Set'
|
||||
) {
|
||||
return 'collection';
|
||||
} else if (typeName === 'RegExp') {
|
||||
return 'regexp';
|
||||
} else if (ref === undefined) {
|
||||
return 'undefined';
|
||||
} else if (ref === null) {
|
||||
return 'null';
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
function isReadonlyProp(object, prop) {
|
||||
if (
|
||||
prop === 'arguments' ||
|
||||
prop === 'caller' ||
|
||||
prop === 'callee' ||
|
||||
prop === 'name' ||
|
||||
prop === 'length'
|
||||
) {
|
||||
const typeName = getObjectType(object);
|
||||
return (
|
||||
typeName === 'Function' ||
|
||||
typeName === 'AsyncFunction' ||
|
||||
typeName === 'GeneratorFunction' ||
|
||||
typeName === 'AsyncGeneratorFunction'
|
||||
);
|
||||
}
|
||||
if (
|
||||
prop === 'source' ||
|
||||
prop === 'global' ||
|
||||
prop === 'ignoreCase' ||
|
||||
prop === 'multiline'
|
||||
) {
|
||||
return getObjectType(object) === 'RegExp';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
class ModuleMocker {
|
||||
_environmentGlobal;
|
||||
_mockState;
|
||||
_mockConfigRegistry;
|
||||
_spyState;
|
||||
_invocationCallCounter;
|
||||
|
||||
/**
|
||||
* @see README.md
|
||||
* @param global Global object of the test environment, used to create
|
||||
* mocks
|
||||
*/
|
||||
constructor(global) {
|
||||
this._environmentGlobal = global;
|
||||
this._mockState = new WeakMap();
|
||||
this._mockConfigRegistry = new WeakMap();
|
||||
this._spyState = new Set();
|
||||
this._invocationCallCounter = 1;
|
||||
}
|
||||
_getSlots(object) {
|
||||
if (!object) {
|
||||
return [];
|
||||
}
|
||||
const slots = new Set();
|
||||
const EnvObjectProto = this._environmentGlobal.Object.prototype;
|
||||
const EnvFunctionProto = this._environmentGlobal.Function.prototype;
|
||||
const EnvRegExpProto = this._environmentGlobal.RegExp.prototype;
|
||||
|
||||
// Also check the builtins in the current context as they leak through
|
||||
// core node modules.
|
||||
const ObjectProto = Object.prototype;
|
||||
const FunctionProto = Function.prototype;
|
||||
const RegExpProto = RegExp.prototype;
|
||||
|
||||
// Properties of Object.prototype, Function.prototype and RegExp.prototype
|
||||
// are never reported as slots
|
||||
while (
|
||||
object != null &&
|
||||
object !== EnvObjectProto &&
|
||||
object !== EnvFunctionProto &&
|
||||
object !== EnvRegExpProto &&
|
||||
object !== ObjectProto &&
|
||||
object !== FunctionProto &&
|
||||
object !== RegExpProto
|
||||
) {
|
||||
const ownNames = Object.getOwnPropertyNames(object);
|
||||
for (let i = 0; i < ownNames.length; i++) {
|
||||
const prop = ownNames[i];
|
||||
if (!isReadonlyProp(object, prop)) {
|
||||
const propDesc = Object.getOwnPropertyDescriptor(object, prop);
|
||||
if ((propDesc !== undefined && !propDesc.get) || object.__esModule) {
|
||||
slots.add(prop);
|
||||
}
|
||||
}
|
||||
}
|
||||
object = Object.getPrototypeOf(object);
|
||||
}
|
||||
return Array.from(slots);
|
||||
}
|
||||
_ensureMockConfig(f) {
|
||||
let config = this._mockConfigRegistry.get(f);
|
||||
if (!config) {
|
||||
config = this._defaultMockConfig();
|
||||
this._mockConfigRegistry.set(f, config);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
_ensureMockState(f) {
|
||||
let state = this._mockState.get(f);
|
||||
if (!state) {
|
||||
state = this._defaultMockState();
|
||||
this._mockState.set(f, state);
|
||||
}
|
||||
if (state.calls.length > 0) {
|
||||
state.lastCall = state.calls[state.calls.length - 1];
|
||||
}
|
||||
return state;
|
||||
}
|
||||
_defaultMockConfig() {
|
||||
return {
|
||||
mockImpl: undefined,
|
||||
mockName: 'jest.fn()',
|
||||
specificMockImpls: []
|
||||
};
|
||||
}
|
||||
_defaultMockState() {
|
||||
return {
|
||||
calls: [],
|
||||
contexts: [],
|
||||
instances: [],
|
||||
invocationCallOrder: [],
|
||||
results: []
|
||||
};
|
||||
}
|
||||
_makeComponent(metadata, restore) {
|
||||
if (metadata.type === 'object') {
|
||||
return new this._environmentGlobal.Object();
|
||||
} else if (metadata.type === 'array') {
|
||||
return new this._environmentGlobal.Array();
|
||||
} else if (metadata.type === 'regexp') {
|
||||
return new this._environmentGlobal.RegExp('');
|
||||
} else if (
|
||||
metadata.type === 'constant' ||
|
||||
metadata.type === 'collection' ||
|
||||
metadata.type === 'null' ||
|
||||
metadata.type === 'undefined'
|
||||
) {
|
||||
return metadata.value;
|
||||
} else if (metadata.type === 'function') {
|
||||
const prototype =
|
||||
(metadata.members &&
|
||||
metadata.members.prototype &&
|
||||
metadata.members.prototype.members) ||
|
||||
{};
|
||||
const prototypeSlots = this._getSlots(prototype);
|
||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||
const mocker = this;
|
||||
const mockConstructor = matchArity(function (...args) {
|
||||
const mockState = mocker._ensureMockState(f);
|
||||
const mockConfig = mocker._ensureMockConfig(f);
|
||||
mockState.instances.push(this);
|
||||
mockState.contexts.push(this);
|
||||
mockState.calls.push(args);
|
||||
// Create and record an "incomplete" mock result immediately upon
|
||||
// calling rather than waiting for the mock to return. This avoids
|
||||
// issues caused by recursion where results can be recorded in the
|
||||
// wrong order.
|
||||
const mockResult = {
|
||||
type: 'incomplete',
|
||||
value: undefined
|
||||
};
|
||||
mockState.results.push(mockResult);
|
||||
mockState.invocationCallOrder.push(mocker._invocationCallCounter++);
|
||||
|
||||
// Will be set to the return value of the mock if an error is not thrown
|
||||
let finalReturnValue;
|
||||
// Will be set to the error that is thrown by the mock (if it throws)
|
||||
let thrownError;
|
||||
// Will be set to true if the mock throws an error. The presence of a
|
||||
// value in `thrownError` is not a 100% reliable indicator because a
|
||||
// function could throw a value of undefined.
|
||||
let callDidThrowError = false;
|
||||
try {
|
||||
// The bulk of the implementation is wrapped in an immediately
|
||||
// executed arrow function so the return value of the mock function
|
||||
// can be easily captured and recorded, despite the many separate
|
||||
// return points within the logic.
|
||||
finalReturnValue = (() => {
|
||||
if (this instanceof f) {
|
||||
// This is probably being called as a constructor
|
||||
prototypeSlots.forEach(slot => {
|
||||
// Copy prototype methods to the instance to make
|
||||
// it easier to interact with mock instance call and
|
||||
// return values
|
||||
if (prototype[slot].type === 'function') {
|
||||
// @ts-expect-error no index signature
|
||||
const protoImpl = this[slot];
|
||||
// @ts-expect-error no index signature
|
||||
this[slot] = mocker.generateFromMetadata(prototype[slot]);
|
||||
// @ts-expect-error no index signature
|
||||
this[slot]._protoImpl = protoImpl;
|
||||
}
|
||||
});
|
||||
|
||||
// Run the mock constructor implementation
|
||||
const mockImpl = mockConfig.specificMockImpls.length
|
||||
? mockConfig.specificMockImpls.shift()
|
||||
: mockConfig.mockImpl;
|
||||
return mockImpl && mockImpl.apply(this, arguments);
|
||||
}
|
||||
|
||||
// If mockImplementationOnce()/mockImplementation() is last set,
|
||||
// implementation use the mock
|
||||
let specificMockImpl = mockConfig.specificMockImpls.shift();
|
||||
if (specificMockImpl === undefined) {
|
||||
specificMockImpl = mockConfig.mockImpl;
|
||||
}
|
||||
if (specificMockImpl) {
|
||||
return specificMockImpl.apply(this, arguments);
|
||||
}
|
||||
// Otherwise use prototype implementation
|
||||
if (f._protoImpl) {
|
||||
return f._protoImpl.apply(this, arguments);
|
||||
}
|
||||
return undefined;
|
||||
})();
|
||||
} catch (error) {
|
||||
// Store the thrown error so we can record it, then re-throw it.
|
||||
thrownError = error;
|
||||
callDidThrowError = true;
|
||||
throw error;
|
||||
} finally {
|
||||
// Record the result of the function.
|
||||
// NOTE: Intentionally NOT pushing/indexing into the array of mock
|
||||
// results here to avoid corrupting results data if mockClear()
|
||||
// is called during the execution of the mock.
|
||||
// @ts-expect-error reassigning 'incomplete'
|
||||
mockResult.type = callDidThrowError ? 'throw' : 'return';
|
||||
mockResult.value = callDidThrowError ? thrownError : finalReturnValue;
|
||||
}
|
||||
return finalReturnValue;
|
||||
}, metadata.length || 0);
|
||||
const f = this._createMockFunction(metadata, mockConstructor);
|
||||
f._isMockFunction = true;
|
||||
f.getMockImplementation = () => this._ensureMockConfig(f).mockImpl;
|
||||
if (typeof restore === 'function') {
|
||||
this._spyState.add(restore);
|
||||
}
|
||||
this._mockState.set(f, this._defaultMockState());
|
||||
this._mockConfigRegistry.set(f, this._defaultMockConfig());
|
||||
Object.defineProperty(f, 'mock', {
|
||||
configurable: false,
|
||||
enumerable: true,
|
||||
get: () => this._ensureMockState(f),
|
||||
set: val => this._mockState.set(f, val)
|
||||
});
|
||||
f.mockClear = () => {
|
||||
this._mockState.delete(f);
|
||||
return f;
|
||||
};
|
||||
f.mockReset = () => {
|
||||
f.mockClear();
|
||||
this._mockConfigRegistry.delete(f);
|
||||
return f;
|
||||
};
|
||||
f.mockRestore = () => {
|
||||
f.mockReset();
|
||||
return restore ? restore() : undefined;
|
||||
};
|
||||
f.mockReturnValueOnce = value =>
|
||||
// next function call will return this value or default return value
|
||||
f.mockImplementationOnce(() => value);
|
||||
f.mockResolvedValueOnce = value =>
|
||||
f.mockImplementationOnce(() =>
|
||||
this._environmentGlobal.Promise.resolve(value)
|
||||
);
|
||||
f.mockRejectedValueOnce = value =>
|
||||
f.mockImplementationOnce(() =>
|
||||
this._environmentGlobal.Promise.reject(value)
|
||||
);
|
||||
f.mockReturnValue = value =>
|
||||
// next function call will return specified return value or this one
|
||||
f.mockImplementation(() => value);
|
||||
f.mockResolvedValue = value =>
|
||||
f.mockImplementation(() =>
|
||||
this._environmentGlobal.Promise.resolve(value)
|
||||
);
|
||||
f.mockRejectedValue = value =>
|
||||
f.mockImplementation(() =>
|
||||
this._environmentGlobal.Promise.reject(value)
|
||||
);
|
||||
f.mockImplementationOnce = fn => {
|
||||
// next function call will use this mock implementation return value
|
||||
// or default mock implementation return value
|
||||
const mockConfig = this._ensureMockConfig(f);
|
||||
mockConfig.specificMockImpls.push(fn);
|
||||
return f;
|
||||
};
|
||||
f.withImplementation = withImplementation.bind(this);
|
||||
function withImplementation(fn, callback) {
|
||||
// Remember previous mock implementation, then set new one
|
||||
const mockConfig = this._ensureMockConfig(f);
|
||||
const previousImplementation = mockConfig.mockImpl;
|
||||
const previousSpecificImplementations = mockConfig.specificMockImpls;
|
||||
mockConfig.mockImpl = fn;
|
||||
mockConfig.specificMockImpls = [];
|
||||
const returnedValue = callback();
|
||||
if ((0, _jestUtil().isPromise)(returnedValue)) {
|
||||
return returnedValue.then(() => {
|
||||
mockConfig.mockImpl = previousImplementation;
|
||||
mockConfig.specificMockImpls = previousSpecificImplementations;
|
||||
});
|
||||
} else {
|
||||
mockConfig.mockImpl = previousImplementation;
|
||||
mockConfig.specificMockImpls = previousSpecificImplementations;
|
||||
}
|
||||
}
|
||||
f.mockImplementation = fn => {
|
||||
// next function call will use mock implementation return value
|
||||
const mockConfig = this._ensureMockConfig(f);
|
||||
mockConfig.mockImpl = fn;
|
||||
return f;
|
||||
};
|
||||
f.mockReturnThis = () =>
|
||||
f.mockImplementation(function () {
|
||||
return this;
|
||||
});
|
||||
f.mockName = name => {
|
||||
if (name) {
|
||||
const mockConfig = this._ensureMockConfig(f);
|
||||
mockConfig.mockName = name;
|
||||
}
|
||||
return f;
|
||||
};
|
||||
f.getMockName = () => {
|
||||
const mockConfig = this._ensureMockConfig(f);
|
||||
return mockConfig.mockName || 'jest.fn()';
|
||||
};
|
||||
if (metadata.mockImpl) {
|
||||
f.mockImplementation(metadata.mockImpl);
|
||||
}
|
||||
return f;
|
||||
} else {
|
||||
const unknownType = metadata.type || 'undefined type';
|
||||
throw new Error(`Unrecognized type ${unknownType}`);
|
||||
}
|
||||
}
|
||||
_createMockFunction(metadata, mockConstructor) {
|
||||
let name = metadata.name;
|
||||
if (!name) {
|
||||
return mockConstructor;
|
||||
}
|
||||
|
||||
// Preserve `name` property of mocked function.
|
||||
const boundFunctionPrefix = 'bound ';
|
||||
let bindCall = '';
|
||||
// if-do-while for perf reasons. The common case is for the if to fail.
|
||||
if (name.startsWith(boundFunctionPrefix)) {
|
||||
do {
|
||||
name = name.substring(boundFunctionPrefix.length);
|
||||
// Call bind() just to alter the function name.
|
||||
bindCall = '.bind(null)';
|
||||
} while (name && name.startsWith(boundFunctionPrefix));
|
||||
}
|
||||
|
||||
// Special case functions named `mockConstructor` to guard for infinite loops
|
||||
if (name === MOCK_CONSTRUCTOR_NAME) {
|
||||
return mockConstructor;
|
||||
}
|
||||
if (
|
||||
// It's a syntax error to define functions with a reserved keyword as name
|
||||
RESERVED_KEYWORDS.has(name) ||
|
||||
// It's also a syntax error to define functions with a name that starts with a number
|
||||
/^\d/.test(name)
|
||||
) {
|
||||
name = `$${name}`;
|
||||
}
|
||||
|
||||
// It's also a syntax error to define a function with a reserved character
|
||||
// as part of it's name.
|
||||
if (FUNCTION_NAME_RESERVED_PATTERN.test(name)) {
|
||||
name = name.replace(FUNCTION_NAME_RESERVED_REPLACE, '$');
|
||||
}
|
||||
const body =
|
||||
`return function ${name}() {` +
|
||||
` return ${MOCK_CONSTRUCTOR_NAME}.apply(this,arguments);` +
|
||||
`}${bindCall}`;
|
||||
const createConstructor = new this._environmentGlobal.Function(
|
||||
MOCK_CONSTRUCTOR_NAME,
|
||||
body
|
||||
);
|
||||
return createConstructor(mockConstructor);
|
||||
}
|
||||
_generateMock(metadata, callbacks, refs) {
|
||||
// metadata not compatible but it's the same type, maybe problem with
|
||||
// overloading of _makeComponent and not _generateMock?
|
||||
// @ts-expect-error - unsure why TSC complains here?
|
||||
const mock = this._makeComponent(metadata);
|
||||
if (metadata.refID != null) {
|
||||
refs[metadata.refID] = mock;
|
||||
}
|
||||
this._getSlots(metadata.members).forEach(slot => {
|
||||
const slotMetadata = (metadata.members && metadata.members[slot]) || {};
|
||||
if (slotMetadata.ref != null) {
|
||||
callbacks.push(
|
||||
(function (ref) {
|
||||
return () => (mock[slot] = refs[ref]);
|
||||
})(slotMetadata.ref)
|
||||
);
|
||||
} else {
|
||||
mock[slot] = this._generateMock(slotMetadata, callbacks, refs);
|
||||
}
|
||||
});
|
||||
if (
|
||||
metadata.type !== 'undefined' &&
|
||||
metadata.type !== 'null' &&
|
||||
mock.prototype &&
|
||||
typeof mock.prototype === 'object'
|
||||
) {
|
||||
mock.prototype.constructor = mock;
|
||||
}
|
||||
return mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given property of an object has been already replaced.
|
||||
*/
|
||||
_findReplacedProperty(object, propertyKey) {
|
||||
for (const spyState of this._spyState) {
|
||||
if (
|
||||
'object' in spyState &&
|
||||
'property' in spyState &&
|
||||
spyState.object === object &&
|
||||
spyState.property === propertyKey
|
||||
) {
|
||||
return spyState;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see README.md
|
||||
* @param metadata Metadata for the mock in the schema returned by the
|
||||
* getMetadata method of this module.
|
||||
*/
|
||||
generateFromMetadata(metadata) {
|
||||
const callbacks = [];
|
||||
const refs = {};
|
||||
const mock = this._generateMock(metadata, callbacks, refs);
|
||||
callbacks.forEach(setter => setter());
|
||||
return mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see README.md
|
||||
* @param component The component for which to retrieve metadata.
|
||||
*/
|
||||
getMetadata(component, _refs) {
|
||||
const refs = _refs || new Map();
|
||||
const ref = refs.get(component);
|
||||
if (ref != null) {
|
||||
return {
|
||||
ref
|
||||
};
|
||||
}
|
||||
const type = getType(component);
|
||||
if (!type) {
|
||||
return null;
|
||||
}
|
||||
const metadata = {
|
||||
type
|
||||
};
|
||||
if (
|
||||
type === 'constant' ||
|
||||
type === 'collection' ||
|
||||
type === 'undefined' ||
|
||||
type === 'null'
|
||||
) {
|
||||
metadata.value = component;
|
||||
return metadata;
|
||||
} else if (type === 'function') {
|
||||
// @ts-expect-error component is a function so it has a name, but not
|
||||
// necessarily a string: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#function_names_in_classes
|
||||
const componentName = component.name;
|
||||
if (typeof componentName === 'string') {
|
||||
metadata.name = componentName;
|
||||
}
|
||||
if (this.isMockFunction(component)) {
|
||||
metadata.mockImpl = component.getMockImplementation();
|
||||
}
|
||||
}
|
||||
metadata.refID = refs.size;
|
||||
refs.set(component, metadata.refID);
|
||||
let members = null;
|
||||
// Leave arrays alone
|
||||
if (type !== 'array') {
|
||||
// @ts-expect-error component is object
|
||||
this._getSlots(component).forEach(slot => {
|
||||
if (
|
||||
type === 'function' &&
|
||||
this.isMockFunction(component) &&
|
||||
slot.match(/^mock/)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
// @ts-expect-error no index signature
|
||||
const slotMetadata = this.getMetadata(component[slot], refs);
|
||||
if (slotMetadata) {
|
||||
if (!members) {
|
||||
members = {};
|
||||
}
|
||||
members[slot] = slotMetadata;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (members) {
|
||||
metadata.members = members;
|
||||
}
|
||||
return metadata;
|
||||
}
|
||||
isMockFunction(fn) {
|
||||
return fn != null && fn._isMockFunction === true;
|
||||
}
|
||||
fn(implementation) {
|
||||
const length = implementation ? implementation.length : 0;
|
||||
const fn = this._makeComponent({
|
||||
length,
|
||||
type: 'function'
|
||||
});
|
||||
if (implementation) {
|
||||
fn.mockImplementation(implementation);
|
||||
}
|
||||
return fn;
|
||||
}
|
||||
spyOn(object, methodKey, accessType) {
|
||||
if (
|
||||
object == null ||
|
||||
(typeof object !== 'object' && typeof object !== 'function')
|
||||
) {
|
||||
throw new Error(
|
||||
`Cannot use spyOn on a primitive value; ${this._typeOf(object)} given`
|
||||
);
|
||||
}
|
||||
if (methodKey == null) {
|
||||
throw new Error('No property name supplied');
|
||||
}
|
||||
if (accessType) {
|
||||
return this._spyOnProperty(object, methodKey, accessType);
|
||||
}
|
||||
const original = object[methodKey];
|
||||
if (!original) {
|
||||
throw new Error(
|
||||
`Property \`${String(
|
||||
methodKey
|
||||
)}\` does not exist in the provided object`
|
||||
);
|
||||
}
|
||||
if (!this.isMockFunction(original)) {
|
||||
if (typeof original !== 'function') {
|
||||
throw new Error(
|
||||
`Cannot spy on the \`${String(
|
||||
methodKey
|
||||
)}\` property because it is not a function; ${this._typeOf(
|
||||
original
|
||||
)} given instead.${
|
||||
typeof original !== 'object'
|
||||
? ` If you are trying to mock a property, use \`jest.replaceProperty(object, '${String(
|
||||
methodKey
|
||||
)}', value)\` instead.`
|
||||
: ''
|
||||
}`
|
||||
);
|
||||
}
|
||||
const isMethodOwner = Object.prototype.hasOwnProperty.call(
|
||||
object,
|
||||
methodKey
|
||||
);
|
||||
let descriptor = Object.getOwnPropertyDescriptor(object, methodKey);
|
||||
let proto = Object.getPrototypeOf(object);
|
||||
while (!descriptor && proto !== null) {
|
||||
descriptor = Object.getOwnPropertyDescriptor(proto, methodKey);
|
||||
proto = Object.getPrototypeOf(proto);
|
||||
}
|
||||
let mock;
|
||||
if (descriptor && descriptor.get) {
|
||||
const originalGet = descriptor.get;
|
||||
mock = this._makeComponent(
|
||||
{
|
||||
type: 'function'
|
||||
},
|
||||
() => {
|
||||
descriptor.get = originalGet;
|
||||
Object.defineProperty(object, methodKey, descriptor);
|
||||
}
|
||||
);
|
||||
descriptor.get = () => mock;
|
||||
Object.defineProperty(object, methodKey, descriptor);
|
||||
} else {
|
||||
mock = this._makeComponent(
|
||||
{
|
||||
type: 'function'
|
||||
},
|
||||
() => {
|
||||
if (isMethodOwner) {
|
||||
object[methodKey] = original;
|
||||
} else {
|
||||
delete object[methodKey];
|
||||
}
|
||||
}
|
||||
);
|
||||
// @ts-expect-error overriding original method with a Mock
|
||||
object[methodKey] = mock;
|
||||
}
|
||||
mock.mockImplementation(function () {
|
||||
return original.apply(this, arguments);
|
||||
});
|
||||
}
|
||||
return object[methodKey];
|
||||
}
|
||||
_spyOnProperty(object, propertyKey, accessType) {
|
||||
let descriptor = Object.getOwnPropertyDescriptor(object, propertyKey);
|
||||
let proto = Object.getPrototypeOf(object);
|
||||
while (!descriptor && proto !== null) {
|
||||
descriptor = Object.getOwnPropertyDescriptor(proto, propertyKey);
|
||||
proto = Object.getPrototypeOf(proto);
|
||||
}
|
||||
if (!descriptor) {
|
||||
throw new Error(
|
||||
`Property \`${String(
|
||||
propertyKey
|
||||
)}\` does not exist in the provided object`
|
||||
);
|
||||
}
|
||||
if (!descriptor.configurable) {
|
||||
throw new Error(
|
||||
`Property \`${String(propertyKey)}\` is not declared configurable`
|
||||
);
|
||||
}
|
||||
if (!descriptor[accessType]) {
|
||||
throw new Error(
|
||||
`Property \`${String(
|
||||
propertyKey
|
||||
)}\` does not have access type ${accessType}`
|
||||
);
|
||||
}
|
||||
const original = descriptor[accessType];
|
||||
if (!this.isMockFunction(original)) {
|
||||
if (typeof original !== 'function') {
|
||||
throw new Error(
|
||||
`Cannot spy on the ${String(
|
||||
propertyKey
|
||||
)} property because it is not a function; ${this._typeOf(
|
||||
original
|
||||
)} given instead.${
|
||||
typeof original !== 'object'
|
||||
? ` If you are trying to mock a property, use \`jest.replaceProperty(object, '${String(
|
||||
propertyKey
|
||||
)}', value)\` instead.`
|
||||
: ''
|
||||
}`
|
||||
);
|
||||
}
|
||||
descriptor[accessType] = this._makeComponent(
|
||||
{
|
||||
type: 'function'
|
||||
},
|
||||
() => {
|
||||
// @ts-expect-error: mock is assignable
|
||||
descriptor[accessType] = original;
|
||||
Object.defineProperty(object, propertyKey, descriptor);
|
||||
}
|
||||
);
|
||||
descriptor[accessType].mockImplementation(function () {
|
||||
// @ts-expect-error - wrong context
|
||||
return original.apply(this, arguments);
|
||||
});
|
||||
}
|
||||
Object.defineProperty(object, propertyKey, descriptor);
|
||||
return descriptor[accessType];
|
||||
}
|
||||
replaceProperty(object, propertyKey, value) {
|
||||
if (
|
||||
object == null ||
|
||||
(typeof object !== 'object' && typeof object !== 'function')
|
||||
) {
|
||||
throw new Error(
|
||||
`Cannot use replaceProperty on a primitive value; ${this._typeOf(
|
||||
object
|
||||
)} given`
|
||||
);
|
||||
}
|
||||
if (propertyKey == null) {
|
||||
throw new Error('No property name supplied');
|
||||
}
|
||||
let descriptor = Object.getOwnPropertyDescriptor(object, propertyKey);
|
||||
let proto = Object.getPrototypeOf(object);
|
||||
while (!descriptor && proto !== null) {
|
||||
descriptor = Object.getOwnPropertyDescriptor(proto, propertyKey);
|
||||
proto = Object.getPrototypeOf(proto);
|
||||
}
|
||||
if (!descriptor) {
|
||||
throw new Error(
|
||||
`Property \`${String(
|
||||
propertyKey
|
||||
)}\` does not exist in the provided object`
|
||||
);
|
||||
}
|
||||
if (!descriptor.configurable) {
|
||||
throw new Error(
|
||||
`Property \`${String(propertyKey)}\` is not declared configurable`
|
||||
);
|
||||
}
|
||||
if (descriptor.get !== undefined) {
|
||||
throw new Error(
|
||||
`Cannot replace the \`${String(
|
||||
propertyKey
|
||||
)}\` property because it has a getter. Use \`jest.spyOn(object, '${String(
|
||||
propertyKey
|
||||
)}', 'get').mockReturnValue(value)\` instead.`
|
||||
);
|
||||
}
|
||||
if (descriptor.set !== undefined) {
|
||||
throw new Error(
|
||||
`Cannot replace the \`${String(
|
||||
propertyKey
|
||||
)}\` property because it has a setter. Use \`jest.spyOn(object, '${String(
|
||||
propertyKey
|
||||
)}', 'set').mockReturnValue(value)\` instead.`
|
||||
);
|
||||
}
|
||||
if (typeof descriptor.value === 'function') {
|
||||
throw new Error(
|
||||
`Cannot replace the \`${String(
|
||||
propertyKey
|
||||
)}\` property because it is a function. Use \`jest.spyOn(object, '${String(
|
||||
propertyKey
|
||||
)}')\` instead.`
|
||||
);
|
||||
}
|
||||
const existingRestore = this._findReplacedProperty(object, propertyKey);
|
||||
if (existingRestore) {
|
||||
return existingRestore.replaced.replaceValue(value);
|
||||
}
|
||||
const isPropertyOwner = Object.prototype.hasOwnProperty.call(
|
||||
object,
|
||||
propertyKey
|
||||
);
|
||||
const originalValue = descriptor.value;
|
||||
const restore = () => {
|
||||
if (isPropertyOwner) {
|
||||
object[propertyKey] = originalValue;
|
||||
} else {
|
||||
delete object[propertyKey];
|
||||
}
|
||||
};
|
||||
const replaced = {
|
||||
replaceValue: value => {
|
||||
object[propertyKey] = value;
|
||||
return replaced;
|
||||
},
|
||||
restore: () => {
|
||||
restore();
|
||||
this._spyState.delete(restore);
|
||||
}
|
||||
};
|
||||
restore.object = object;
|
||||
restore.property = propertyKey;
|
||||
restore.replaced = replaced;
|
||||
this._spyState.add(restore);
|
||||
return replaced.replaceValue(value);
|
||||
}
|
||||
clearAllMocks() {
|
||||
this._mockState = new WeakMap();
|
||||
}
|
||||
resetAllMocks() {
|
||||
this._mockConfigRegistry = new WeakMap();
|
||||
this._mockState = new WeakMap();
|
||||
}
|
||||
restoreAllMocks() {
|
||||
this._spyState.forEach(restore => restore());
|
||||
this._spyState = new Set();
|
||||
}
|
||||
_typeOf(value) {
|
||||
return value == null ? `${value}` : typeof value;
|
||||
}
|
||||
mocked(source, _options) {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
exports.ModuleMocker = ModuleMocker;
|
||||
const JestMock = new ModuleMocker(globalThis);
|
||||
const fn = JestMock.fn.bind(JestMock);
|
||||
exports.fn = fn;
|
||||
const spyOn = JestMock.spyOn.bind(JestMock);
|
||||
exports.spyOn = spyOn;
|
||||
const mocked = JestMock.mocked.bind(JestMock);
|
||||
exports.mocked = mocked;
|
||||
const replaceProperty = JestMock.replaceProperty.bind(JestMock);
|
||||
exports.replaceProperty = replaceProperty;
|
35
backend/apis/nodejs/node_modules/jest-mock/package.json
generated
vendored
Normal file
35
backend/apis/nodejs/node_modules/jest-mock/package.json
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "jest-mock",
|
||||
"version": "29.7.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/jestjs/jest.git",
|
||||
"directory": "packages/jest-mock"
|
||||
},
|
||||
"license": "MIT",
|
||||
"main": "./build/index.js",
|
||||
"types": "./build/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./build/index.d.ts",
|
||||
"default": "./build/index.js"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@jest/types": "^29.6.3",
|
||||
"@types/node": "*",
|
||||
"jest-util": "^29.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tsd/typescript": "^5.0.4",
|
||||
"tsd-lite": "^0.7.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"gitHead": "4e56991693da7cd4c3730dc3579a1dd1403ee630"
|
||||
}
|
Reference in New Issue
Block a user