Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/testdouble/testdouble-jest
A testdouble.js extension to add support for Jest module mocking
https://github.com/testdouble/testdouble-jest
Last synced: 7 days ago
JSON representation
A testdouble.js extension to add support for Jest module mocking
- Host: GitHub
- URL: https://github.com/testdouble/testdouble-jest
- Owner: testdouble
- License: mit
- Created: 2018-02-10T22:06:38.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2021-06-02T08:57:04.000Z (over 3 years ago)
- Last Synced: 2024-10-25T02:43:55.360Z (14 days ago)
- Language: JavaScript
- Size: 55.7 KB
- Stars: 37
- Watchers: 5
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-jest - testdouble-jest
README
# testdouble-jest
Support for [testdouble.js](https://github.com/testdouble/testdouble.js) for
users of [Jest](https://github.com/facebook/jest)!Note that testdouble-jest requires [email protected] and [email protected] or higher to work.
## Installation
```
$ npm i -D testdouble-jest
```Then, from a test helper (we recommend setting a
[setupFilesAfterEnv](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array)
module), invoke the module and pass in both `td` and `jest`, like so:```js
global.td = require('testdouble')
require('testdouble-jest')(td, jest)
```For an example of a helper that sets up testdouble.js, testdouble-jest, and
ensures `td.reset()` is called after each test, look at
[example/helper.js](/example/helper.js) in this repo.## Usage
When you invoke `testdouble-jest`, it does two things: (1) adds support for
using `td.replace()` for module replacement in Jest tests, and (2) adds a new
top-level `td.mock()` function that mirrors the `jest.mock()` API.We recommend using `td.replace()`, since it's terser (by returning the fake
instead of the `jest` object) and your use of testdouble.js will remain portable
even if you were to move to a different test runner.**`td.replace(moduleName[, manualStub])`**
Once you've initialized testdouble-jest in your test run, `td.replace()` will be
able to replace modules just as it does in any other test runner (as of
[email protected]). Functionally, it's delegates to `td.mock()`, but behaves just
as it [always
has](https://github.com/testdouble/testdouble.js#module-replacement-with-nodejs)
for module replacement.Here's a trivial example:
```js
let loadInvoices, subject
describe('td.replace', () => {
beforeEach(() => {
loadInvoices = td.replace('./load-invoices')
subject = require('./calculate-payment')
})
it('calculates payments', () => {
td.when(loadInvoices(2018, 7)).thenReturn([24,28])const result = subject('2018-07')
expect(result).toEqual(52)
})
})
```For a runnable example, check
[example/td-replace.test.js](/example/td-replace.test.js).**`td.mock(moduleName[, moduleFactory, options])`**
`td.mock()` is designed to have the same API as
[`jest.mock()`](https://facebook.github.io/jest/docs/en/es6-class-mocks.html).
If you just pass a module name to `td.mock()`, it will imitate the real
dependency and use Jest's own module replacement facility to ensure that any
`require()` calls by your test subject receive the testdouble fake, as opposed
to the real dependency. There's an example in this repo at
[example/td-mock.test.js](/example/td-mock.test.js).If you've used `jest.mock()` before, `td.mock()` will seem pretty familiar.
`td.mock()` returns the `jest` object (since that's what
`jest.mock()` does), so your test will also need to `require()` the thing you
just faked if you want to set up any stubbings or invocation assertions.Note that if you provide a `moduleFactory` and/or `options` argument, `td.mock`
will simply delegate to `jest.mock`, since it won't have anything
testdouble.js-specific to do.