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.018,953,521latest
30.0.0-alpha.723,501next

Version History

VersionDownloads (Last 7 Days)Published
30.0.0-alpha.723,501
30.0.0-alpha.626,832
30.0.0-alpha.51,756
30.0.0-alpha.4445
30.0.0-alpha.32,475
30.0.0-alpha.25,687
30.0.0-alpha.1300
29.7.018,953,521
29.6.3181,553
29.6.2437,225
29.6.1180,894
29.6.019,156
29.5.0789,129
29.4.3105,011
29.4.236,151
29.4.155,855
29.4.07,702
29.3.1355,895
29.3.03,949
29.2.237,955
29.2.113,548
29.2.09,030
29.1.239,012
29.1.13,566
29.1.08
29.0.338,473
29.0.215,168
29.0.17,138
29.0.0804
29.0.0-alpha.64
29.0.0-alpha.420
29.0.0-alpha.32
29.0.0-alpha.030
28.1.31,499,213
28.1.176,026
28.1.047,053
28.0.29,458
28.0.1223
28.0.01,074
28.0.0-alpha.926
28.0.0-alpha.87
28.0.0-alpha.75,016
28.0.0-alpha.66
28.0.0-alpha.56
28.0.0-alpha.411
28.0.0-alpha.32
28.0.0-alpha.25
28.0.0-alpha.17
28.0.0-alpha.010
27.5.15,825,674
27.5.07,669
27.4.6128,111
27.4.278,404
27.4.17,055
27.4.01,454
27.3.0122,993
27.2.515,786
27.2.465,153
27.2.31,998
27.1.147,756
27.1.021,956
27.0.688,756
27.0.3127,106
27.0.2268
27.0.13,033
27.0.0-next.1016
27.0.0-next.8710
27.0.0-next.747
27.0.0-next.3119
27.0.0-next.1246
27.0.0-next.06
26.6.21,922,706
26.6.121,220
26.6.09,737
26.5.218,001
26.5.01,407
26.3.066,787
26.2.016,898
26.1.0110,159
26.0.151,081
26.0.1-alpha.05
26.0.0395
26.0.0-alpha.25
26.0.0-alpha.14
26.0.0-alpha.05
25.5.0384,488
25.4.011,638
25.3.014,640
25.2.610,172
25.2.35,327
25.2.1623
25.2.1-alpha.25
25.2.1-alpha.17
25.2.0280
25.2.0-alpha.862
25.1.046,290
25.0.0734
24.9.01,274,555
24.8.073,995
24.7.015,551
24.6.0123
24.5.010,794
24.3.02,253
24.2.0-alpha.05
24.0.022,462
24.0.0-alpha.1610
24.0.0-alpha.15121
24.0.0-alpha.131
24.0.0-alpha.125
24.0.0-alpha.113
24.0.0-alpha.107
24.0.0-alpha.96
24.0.0-alpha.73
24.0.0-alpha.610
24.0.0-alpha.54
24.0.0-alpha.45
24.0.0-alpha.25
24.0.0-alpha.15
24.0.0-alpha.05
23.2.0226,098
23.1.06,125
23.0.232
23.0.188
23.0.0255
23.0.0-charlie.44
23.0.0-charlie.36
23.0.0-charlie.22
23.0.0-charlie.13
23.0.0-charlie.04
23.0.0-beta.3r8
23.0.0-alpha.3r6
23.0.0-beta.24
23.0.0-beta.13
23.0.0-beta.02
23.0.0-alpha.75
23.0.0-alpha.6r5
23.0.0-alpha.5r5
23.0.0-alpha.53
23.0.0-alpha.438
23.0.0-alpha.25
22.4.3113,045
23.0.0-alpha.14
23.0.0-alpha.012
22.2.018,279
22.1.06,733
22.0.6204
22.0.5698
22.0.32,538
22.0.2269
22.0.14
22.0.05
21.3.0-beta.1579
21.3.0-beta.145
21.3.0-beta.135
21.3.0-beta.125
21.3.0-beta.117
21.3.0-beta.102
21.3.0-beta.96
21.3.0-beta.87
21.3.0-beta.77
21.3.0-beta.65
21.3.0-beta.56
21.3.0-beta.48
21.3.0-beta.34
21.3.0-beta.26
21.3.0-alpha.eff7a1cf4
21.3.0-alpha.1e3ee68e6
21.2.057,867
21.1.02,507
21.0.2745
21.0.0216
21.0.0-beta.16
21.0.0-alpha.23
21.0.0-alpha.12
20.1.0-echo.111
20.1.0-delta.54
20.1.0-delta.46
20.1.0-delta.35
20.1.0-delta.23
20.1.0-delta.1197
20.1.0-chi.15
20.1.0-beta.13
20.1.0-alpha.33
20.1.0-alpha.22
20.1.0-alpha.16
20.0.361,519
20.0.25
20.0.1317
20.0.0533
19.3.0-alpha.854022546
19.2.0-alpha.993e64af5
19.1.0-alpha.eed820343
19.0.027,697
18.5.0-alpha.7da3df399
18.0.017,945
17.0.25,613
17.0.045
16.1.0-alpha.691b0e224
16.0.22,715
16.0.064
15.2.0-alpha.c681f8194
15.0.01,056
14.3.2-alpha.83c254174
14.3.1-alpha.410cb91a2
14.3.0-alpha.d13c163e7
14.2.2-alpha.22bd3c334
14.2.1-alpha.e21d71a45
14.2.0-alpha.ca8bfb6e4
14.0.01,353
13.4.0-alpha.d26320066
13.3.0-alpha.g8b48d595
13.3.0-alpha.8b48d59e8
13.3.0-alpha.ffc7404b4
13.3.0-alpha.4eb0c9085
13.3.0-alpha.a44f195f6
13.2.289
13.2.15
13.1.15
13.0.04
12.1.5-alpha.b53224227
12.1.4-alpha.a737c6e56
12.1.3-alpha.6230044c6
12.1.2-alpha.6230044c3
12.1.2-alpha.a482b15c7
12.1.1-alpha.2935e14d7
12.1.01,748
12.0.23
12.0.15
12.0.06
11.0.24
11.0.11
11.0.03
10.0.26
10.0.16
10.0.07
9.0.32
1.0.05
0.0.03

Package Sidebar

Install

npm i jest-mock

Weekly Downloads

34,183,944

Version

29.7.0

License

MIT

Unpacked Size

47.5 kB

Total Files

5

Last publish

Collaborators

  • simenb
  • cpojer
  • aaronabramov
  • openjs-operations