source: frontend/node_modules/jest-mock/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.8 KB
Line 
1# jest-mock
2
3## API
4
5```js
6import {ModuleMocker} from 'jest-mock';
7```
8
9### `constructor(global)`
10
11Creates a new module mocker that generates mocks as if they were created in an environment with the given global object.
12
13### `generateFromMetadata(metadata)`
14
15Generates 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.
16
17One 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.
18
19### `getMetadata(component)`
20
21Inspects the argument and returns its schema in the following recursive format:
22
23```
24{
25 type: ...
26 members: {}
27}
28```
29
30Where 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:
31
32```js
33const classDef = {
34 type: 'function',
35 members: {
36 staticMethod: {type: 'function'},
37 prototype: {
38 type: 'object',
39 members: {
40 instanceMethod: {type: 'function'},
41 },
42 },
43 },
44};
45```
46
47Metadata 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:
48
49```js
50const refID = {
51 type: 'object',
52 refID: 1,
53 members: {
54 self: {ref: 1},
55 },
56};
57```
58
59defines an object with a slot named `self` that refers back to the object.
60
61### `fn`
62
63Generates a stand-alone function with members that help drive unit tests or confirm expectations. Specifically, functions returned by this method have the following members:
64
65##### `.mock`
66
67An 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.
68
69##### `.mockReturnValueOnce(value)`
70
71Pushes the given value onto a FIFO queue of return values for the function.
72
73##### `.mockReturnValue(value)`
74
75Sets the default return value for the function.
76
77##### `.mockImplementationOnce(function)`
78
79Pushes the given mock implementation onto a FIFO queue of mock implementations for the function.
80
81##### `.mockImplementation(function)`
82
83Sets the default mock implementation for the function.
84
85##### `.mockReturnThis()`
86
87Syntactic sugar for .mockImplementation(function() {return this;})
88
89In case both `mockImplementationOnce()/mockImplementation()` and `mockReturnValueOnce()/mockReturnValue()` are called. The priority of which to use is based on what is the last call:
90
91- 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();
92- if the last call is mockImplementationOnce() or mockImplementation(), run the specific implementation and return the result or run default implementation and return the result.
Note: See TracBrowser for help on using the repository browser.