jest-mock
TypeScript icon, indicating that this package has built-in type declarations

29.7.0 • Public • Published

jest-mock

Note: More details on user side API can be found in Jest documentation.

API

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:

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:

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:

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.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
29.7.016,394,267latest
30.0.0-alpha.631,923next

Version History

VersionDownloads (Last 7 Days)Published
30.0.0-alpha.631,923
30.0.0-alpha.52,090
30.0.0-alpha.4615
30.0.0-alpha.31,400
30.0.0-alpha.211,180
30.0.0-alpha.1210
29.7.016,394,267
29.6.3196,634
29.6.2297,822
29.6.1224,769
29.6.013,260
29.5.0813,269
29.4.380,819
29.4.242,160
29.4.140,094
29.4.026,376
29.3.1187,338
29.3.02,809
29.2.226,751
29.2.113,745
29.2.07,201
29.1.230,252
29.1.13,472
29.1.039
29.0.330,397
29.0.211,194
29.0.17,376
29.0.0659
29.0.0-alpha.60
29.0.0-alpha.42
29.0.0-alpha.32
29.0.0-alpha.04
28.1.31,644,881
28.1.161,099
28.1.038,517
28.0.23,272
28.0.1115
28.0.0951
28.0.0-alpha.910
28.0.0-alpha.80
28.0.0-alpha.76,318
28.0.0-alpha.60
28.0.0-alpha.50
28.0.0-alpha.40
28.0.0-alpha.33
28.0.0-alpha.23
28.0.0-alpha.10
28.0.0-alpha.00
27.5.15,581,440
27.5.06,893
27.4.691,760
27.4.257,087
27.4.15,672
27.4.0479
27.3.079,283
27.2.56,423
27.2.469,505
27.2.31,470
27.1.133,905
27.1.012,154
27.0.647,089
27.0.343,229
27.0.26
27.0.11,586
27.0.0-next.105
27.0.0-next.8368
27.0.0-next.73
27.0.0-next.399
27.0.0-next.1235
27.0.0-next.01
26.6.21,946,600
26.6.14,842
26.6.04,686
26.5.27,480
26.5.01,219
26.3.023,875
26.2.05,187
26.1.092,236
26.0.128,685
26.0.1-alpha.00
26.0.049
26.0.0-alpha.20
26.0.0-alpha.10
26.0.0-alpha.00
25.5.0353,452
25.4.01,985
25.3.05,651
25.2.65,211
25.2.31,592
25.2.1140
25.2.1-alpha.21
25.2.1-alpha.13
25.2.062
25.2.0-alpha.860
25.1.021,844
25.0.0543
24.9.01,213,932
24.8.041,096
24.7.05,469
24.6.023
24.5.02,967
24.3.0289
24.2.0-alpha.02
24.0.018,867
24.0.0-alpha.160
24.0.0-alpha.152
24.0.0-alpha.130
24.0.0-alpha.123
24.0.0-alpha.112
24.0.0-alpha.100
24.0.0-alpha.92
24.0.0-alpha.71
24.0.0-alpha.69
24.0.0-alpha.50
24.0.0-alpha.42
24.0.0-alpha.23
24.0.0-alpha.10
24.0.0-alpha.01
23.2.0126,160
23.1.0571
23.0.26
23.0.129
23.0.022
23.0.0-charlie.40
23.0.0-charlie.30
23.0.0-charlie.20
23.0.0-charlie.12
23.0.0-charlie.00
23.0.0-beta.3r2
23.0.0-alpha.3r0
23.0.0-beta.20
23.0.0-beta.10
23.0.0-beta.01
23.0.0-alpha.71
23.0.0-alpha.6r0
23.0.0-alpha.5r0
23.0.0-alpha.51
23.0.0-alpha.47
23.0.0-alpha.20
22.4.353,333
23.0.0-alpha.10
23.0.0-alpha.09
22.2.02,133
22.1.0566
22.0.6120
22.0.520
22.0.3170
22.0.24
22.0.16
22.0.05
21.3.0-beta.1526
21.3.0-beta.142
21.3.0-beta.132
21.3.0-beta.120
21.3.0-beta.110
21.3.0-beta.100
21.3.0-beta.90
21.3.0-beta.85
21.3.0-beta.71
21.3.0-beta.60
21.3.0-beta.50
21.3.0-beta.448
21.3.0-beta.30
21.3.0-beta.214
21.3.0-alpha.eff7a1cf0
21.3.0-alpha.1e3ee68e0
21.2.023,076
21.1.0115
21.0.264
21.0.058
21.0.0-beta.13
21.0.0-alpha.22
21.0.0-alpha.10
20.1.0-echo.118
20.1.0-delta.50
20.1.0-delta.40
20.1.0-delta.32
20.1.0-delta.20
20.1.0-delta.13
20.1.0-chi.10
20.1.0-beta.11
20.1.0-alpha.34
20.1.0-alpha.20
20.1.0-alpha.10
20.0.331,128
20.0.25
20.0.123
20.0.07
19.3.0-alpha.854022540
19.2.0-alpha.993e64af3
19.1.0-alpha.eed820341
19.0.09,962
18.5.0-alpha.7da3df391
18.0.018,241
17.0.21,963
17.0.03
16.1.0-alpha.691b0e220
16.0.2988
16.0.03
15.2.0-alpha.c681f8190
15.0.0216
14.3.2-alpha.83c254170
14.3.1-alpha.410cb91a0
14.3.0-alpha.d13c163e3
14.2.2-alpha.22bd3c330
14.2.1-alpha.e21d71a41
14.2.0-alpha.ca8bfb6e0
14.0.0818
13.4.0-alpha.d26320060
13.3.0-alpha.g8b48d590
13.3.0-alpha.8b48d59e2
13.3.0-alpha.ffc7404b0
13.3.0-alpha.4eb0c9082
13.3.0-alpha.a44f195f1
13.2.291
13.2.10
13.1.15
13.0.00
12.1.5-alpha.b53224220
12.1.4-alpha.a737c6e50
12.1.3-alpha.6230044c3
12.1.2-alpha.6230044c0
12.1.2-alpha.a482b15c1
12.1.1-alpha.2935e14d2
12.1.0212
12.0.20
12.0.10
12.0.00
11.0.20
11.0.10
11.0.01
10.0.22
10.0.10
10.0.00
9.0.35
1.0.02
0.0.01

Package Sidebar

Install

npm i jest-mock

Weekly Downloads

24,374,661

Version

29.7.0

License

MIT

Unpacked Size

47.5 kB

Total Files

5

Last publish

Collaborators

  • simenb
  • cpojer
  • aaronabramov
  • openjs-operations