Skip to content

Commit

Permalink
mockImplementation class constructor example
Browse files Browse the repository at this point in the history
Adding an example to show how to mock class constructors (see jestjs#124)
  • Loading branch information
thekarel committed Oct 13, 2015
1 parent 744f69d commit 4c69030
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down

0 comments on commit 4c69030

Please sign in to comment.