29.7.0 • Published 2 months ago

jest-mock v29.7.0

Weekly downloads
12,697,004
License
MIT
Repository
github
Last release
2 months ago

jest-mock

API

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 simply by defining metadata for the member.prototype of the function. The type of a function prototype should always be object. For instance, a simple 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

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 timestamps, 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 timestamps list stores a number timestamp every time the mock is called.

.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 .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.
jest-environment-nodejest-runtime@jest/fake-timers@jest/environment@jest/globalsjest-environment-jsdom-fourteen@orillusion/jest-electronarchetype-libraryeasy-select-rnreact-native-bluetooth2killi8n-react-native-fast-imagespecify-importsbabel-specify-imports@icanpm/api-masterreact-native-template-rfbaseairscanairscan-exampleyy-jest-electronreact-native-esc-pos-sahaab@borisovart/atol-kkt-module@squidjs/jest-electrondeneme323112@ntt_app/react-native-custom-notificationreact-native-covid-sdkgql_din_modbitget@olivervorasai/sliderreact-native-printer-brothersrn-pdf-reader-offlinereact-native-shekhar-bridge-testbuild-tool-test-frameworkwilscanner@oiti/documentoscopy-react-nativejest-hax-electronjest-electrochrome@mink-opn/build-tokensquoc-testreact-native-slider-kfelectrochromecdd-ts@saaspe/componentsplginexpand-react-bridgeluminos-ui-coresklif-ui-kitsklif-api@everything-registry/sub-chunk-1956jawwy-sdkjawwy_gamification_release@314oner_npm/universal-components-libraryreact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkgp149-tablesklif-uireact-native-jawwy_sample@simstudio/htmldiffjawwy_library_newjawy_library_v1gamification-jawwy-libraryframework_test_library_sixdee_new_jawwyreact-native-experts-sdktest_lib_module_aarzzzxxxyyy321123@batbayar/superset-plugin-chart-hello-world@hemith/react-native-tnkrouter-logger@hdkhoa1302/app-themes@happy-dom/jest-environment@hproinformatica/functions@humanity.cash/types@garonx/oracle-zkapprendu2belaid@gaofq/utilsrn-adyen-dropinrn-agora-ios-mrn-horizontal-listrn-pay-sdkrn-session-multiplier-demorfp-librn-check-btnrn-use-modal-hook@grosto/jest-create-mock-instancern-counter-demo@gzup/react-image-file-resizerrn-circular-chartrtl-depsepm-npm-tsc@hoangnguyennn/mysql-builderes-react-bridgern_unique_device_id@hansomware/tools@hawkingnetwork/react-native-tab-viewrn-keyboard-avoiding-viewrn-tm-notify@hazyflame/vue-jitsi-meet@hbglobal/react-native-actions-shortcuts@hugoloennqvist/blackjack-libevanutils
30.0.0-alpha.3

2 months ago

30.0.0-alpha.2

5 months ago

30.0.0-alpha.1

6 months ago

29.7.0

7 months ago

29.6.3

8 months ago

29.6.0

10 months ago

29.6.1

10 months ago

29.6.2

9 months ago

29.4.1

1 year ago

29.4.2

1 year ago

29.4.3

1 year ago

29.4.0

1 year ago

29.5.0

1 year ago

29.2.0

2 years ago

29.2.1

2 years ago

29.2.2

1 year ago

29.3.0

1 year ago

29.3.1

1 year ago

29.0.1

2 years ago

29.0.2

2 years ago

29.0.3

2 years ago

29.0.0

2 years ago

29.1.0

2 years ago

29.1.1

2 years ago

29.1.2

2 years ago

28.1.3

2 years ago

28.1.1

2 years ago

29.0.0-alpha.6

2 years ago

29.0.0-alpha.4

2 years ago

29.0.0-alpha.3

2 years ago

29.0.0-alpha.0

2 years ago

28.1.0

2 years ago

28.0.0-alpha.9

2 years ago

28.0.0

2 years ago

28.0.1

2 years ago

28.0.2

2 years ago

28.0.0-alpha.8

2 years ago

28.0.0-alpha.7

2 years ago

28.0.0-alpha.6

2 years ago

28.0.0-alpha.5

2 years ago

27.5.0

2 years ago

27.5.1

2 years ago

27.4.6

2 years ago

27.4.0

2 years ago

27.4.1

2 years ago

27.4.2

2 years ago

28.0.0-alpha.4

2 years ago

28.0.0-alpha.3

2 years ago

28.0.0-alpha.0

2 years ago

28.0.0-alpha.2

2 years ago

28.0.0-alpha.1

2 years ago

27.3.0

3 years ago

27.2.5

3 years ago

27.2.4

3 years ago

27.2.3

3 years ago

27.1.1

3 years ago

27.1.0

3 years ago

27.0.6

3 years ago

27.0.2

3 years ago

27.0.3

3 years ago

27.0.1

3 years ago

27.0.0-next.10

3 years ago

27.0.0-next.8

3 years ago

27.0.0-next.7

3 years ago

27.0.0-next.3

3 years ago

27.0.0-next.1

3 years ago

27.0.0-next.0

3 years ago

26.6.2

3 years ago

26.6.1

3 years ago

26.6.0

4 years ago

26.5.2

4 years ago

26.5.0

4 years ago

26.3.0

4 years ago

26.2.0

4 years ago

26.1.0

4 years ago

26.0.1

4 years ago

26.0.1-alpha.0

4 years ago

26.0.0

4 years ago

26.0.0-alpha.2

4 years ago

26.0.0-alpha.1

4 years ago

26.0.0-alpha.0

4 years ago

25.5.0

4 years ago

25.4.0

4 years ago

25.3.0

4 years ago

25.2.6

4 years ago

25.2.5

4 years ago

25.2.1-alpha.2

4 years ago

25.2.3

4 years ago

25.2.1

4 years ago

25.2.1-alpha.1

4 years ago

25.2.0-alpha.86

4 years ago

25.2.0

4 years ago

25.1.0

4 years ago

25.0.0

5 years ago

24.9.0

5 years ago

24.8.0

5 years ago

24.7.0

5 years ago

24.6.0

5 years ago

24.5.0

5 years ago

24.3.0

5 years ago

24.2.0-alpha.0

5 years ago

24.0.0

5 years ago

24.0.0-alpha.16

5 years ago

24.0.0-alpha.15

5 years ago

24.0.0-alpha.13

5 years ago

24.0.0-alpha.12

5 years ago

24.0.0-alpha.11

5 years ago

24.0.0-alpha.10

5 years ago

24.0.0-alpha.9

5 years ago

24.0.0-alpha.7

5 years ago

24.0.0-alpha.6

5 years ago

24.0.0-alpha.5

5 years ago

24.0.0-alpha.4

5 years ago

24.0.0-alpha.2

5 years ago

24.0.0-alpha.1

5 years ago

24.0.0-alpha.0

6 years ago

23.2.0

6 years ago

23.1.0

6 years ago

23.0.2

6 years ago

23.0.1

6 years ago

23.0.0

6 years ago

23.0.0-charlie.4

6 years ago

23.0.0-charlie.3

6 years ago

23.0.0-charlie.2

6 years ago

23.0.0-charlie.1

6 years ago

23.0.0-charlie.0

6 years ago

23.0.0-beta.3r

6 years ago

23.0.0-alpha.3r

6 years ago

23.0.0-beta.2

6 years ago

23.0.0-beta.1

6 years ago

23.0.0-beta.0

6 years ago

23.0.0-alpha.7

6 years ago

23.0.0-alpha.6r

6 years ago

23.0.0-alpha.5r

6 years ago

23.0.0-alpha.5

6 years ago

23.0.0-alpha.4

6 years ago

23.0.0-alpha.2

6 years ago

22.4.3

6 years ago

23.0.0-alpha.1

6 years ago

23.0.0-alpha.0

6 years ago

22.2.0

6 years ago

22.1.0

6 years ago

22.0.6

6 years ago

22.0.5

6 years ago

22.0.3

6 years ago

22.0.2

6 years ago

22.0.1

6 years ago

22.0.0

6 years ago

21.3.0-beta.15

6 years ago

21.3.0-beta.14

6 years ago

21.3.0-beta.13

6 years ago

21.3.0-beta.12

6 years ago

21.3.0-beta.11

6 years ago

21.3.0-beta.10

6 years ago

21.3.0-beta.9

6 years ago

21.3.0-beta.8

6 years ago

21.3.0-beta.7

6 years ago

21.3.0-beta.6

6 years ago

21.3.0-beta.5

6 years ago

21.3.0-beta.4

6 years ago

21.3.0-beta.3

6 years ago

21.3.0-beta.2

7 years ago

21.2.0

7 years ago

21.1.0

7 years ago

21.0.2

7 years ago

21.0.0

7 years ago

21.0.0-beta.1

7 years ago

21.0.0-alpha.2

7 years ago

21.0.0-alpha.1

7 years ago

20.1.0-echo.1

7 years ago

20.1.0-delta.5

7 years ago

20.1.0-delta.4

7 years ago

20.1.0-delta.3

7 years ago

20.1.0-delta.2

7 years ago

20.1.0-delta.1

7 years ago

20.1.0-chi.1

7 years ago

20.1.0-beta.1

7 years ago

20.1.0-alpha.3

7 years ago

20.1.0-alpha.2

7 years ago

20.1.0-alpha.1

7 years ago

20.0.3

7 years ago

20.0.2

7 years ago

20.0.1

7 years ago

20.0.0

7 years ago

19.0.0

7 years ago

18.0.0

7 years ago

17.0.2

7 years ago

17.0.0

7 years ago

16.0.2

8 years ago

16.0.0

8 years ago

15.0.0

8 years ago

14.0.0

8 years ago

13.2.2

8 years ago

13.2.1

8 years ago

13.1.1

8 years ago

13.0.0

8 years ago

12.1.0

8 years ago

12.0.2

8 years ago

12.0.1

8 years ago

12.0.0

8 years ago

11.0.2

8 years ago

11.0.1

8 years ago

11.0.0

8 years ago

10.0.2

8 years ago

10.0.1

8 years ago

10.0.0

8 years ago

9.0.3

8 years ago

1.0.0

8 years ago

0.0.0

8 years ago