From 4c690305ae92cd45515f62b30c90ab931c1881da Mon Sep 17 00:00:00 2001 From: Charles Szilagyi Date: Tue, 13 Oct 2015 14:44:13 +0100 Subject: [PATCH] mockImplementation class constructor example Adding an example to show how to mock class constructors (see #124) --- docs/API.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/API.md b/docs/API.md index 07b4dc7202bc..1197d9ba3c91 100644 --- a/docs/API.md +++ b/docs/API.md @@ -215,6 +215,28 @@ mockFn.mock.calls[0][0] === 0; // true mockFn.mock.calls[1][0] === 1; // true ``` +`mockImplementation` can also be used to mock class constructors: + +``` +// SomeClass.js +module.exports = class SomeClass { + m(a, b) {} +} + +// OtherModule.test.js +let SomeClass = require("SomeClass") +let mMock = jest.genMockFn() +somaClass.mockImplementation(() => { + return { + m: mMock + } +}) + +let some = new SomeClass() +some.m("a", "b") +console.log("Calls to m: ", mMock.mock.calls) +``` + ### `mockFn.mockImpl(fn)` Shorthand alias for [`mockFn.mockImplementation(fn)`](#mockfn-mockimplementation-fn).