Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest play nice #486

Merged
merged 5 commits into from Dec 19, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -86,7 +86,12 @@ jobs:
key: npm-cache-{{ checksum "package-lock.json" }}
- run: 'echo "//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}" > ${HOME}/.npmrc'
- run: npm version --no-git-tag-version ${CIRCLE_TAG}
- run: npm publish --access public
- run: |
if [ $CIRCLE_TAG ?? '-'];
then npm publish --access public --tag beta;
else npm publish --access public;
fi
workflows:
version: 2
@@ -3,22 +3,32 @@ title: Usage with Jest
position: 6
parentItem: installation
content_markdown: |-
Jest has rapidly become a very popular, full-featured testing library. Usage of fetch-mock with Jest is sufficiently different to previous libraries that it deserve some examples of its own:
Jest has rapidly become a very popular, full-featured testing library. Usage of fetch-mock with Jest is sufficiently different to previous libraries that it deserves some examples of its own:
If using global `fetch`, then no special treatment is required.
If assigning `node-fetch` to a variable in your source code, the following should provide a good manual mock for `node-fetch`, saved as `./__mocks__/node-fetch.js` in your project.
For non-global uses of `node-fetch` use something like:
```js
jest.mock('node-fetch', () => require('fetch-mock').sandbox())
```
if you need to fallback to the network (or have some other use case for giving `fetch-mock` [access to `node-fetch` internals](#usagecustom-classes) you will need to use `jest.requireActual('node-fetch')`, e.g.
```javascript
const nodeFetch = jest.requireActual('node-fetch');
const fetchMock = require('fetch-mock').sandbox();
Object.assign(fetchMock.config, nodeFetch, {
fetch: nodeFetch
});
module.exports = fetchMock;
jest.mock('node-fetch', () => {
const nodeFetch = jest.requireActual('node-fetch');
const fetchMock = require('fetch-mock').sandbox();
Object.assign(fetchMock.config, {
fetch: nodeFetch
});
return fetchMock;
})
```
Then, in your test files, when you `require('node-fetch')`, it will return the sandboxed `fetch-mock` instance from the manual mock, so you can use all the `fetch-mock` methods directly on it.
The content of the above function (exporting `fetchMock`) can also be used in a [manual mock](https://jestjs.io/docs/en/manual-mocks).
Once mocked, you should require `node-fetch`, _not_ `fetch-mock`, in your test files - all the `fetch-mock` methods will be available on it.
When using a webpack based compilation step, something like the following may be necessary instead
@@ -27,16 +37,5 @@ content_markdown: |-
const nodeFetch = require('node-fetch');
nodeFetch.default = fetchMock;
```
If your tests include integration tests that need access to the network, set the `fallbackToNetwork: true` config option within the describe block of those tests:
```javascript
const fetch = require('node-fetch');
describe('integration', () => {
beforeAll(() => fetchMock.config.fallbackToNetwork = true);
afterAll(() => fetchMock.config.fallbackToNetwork = false);
});
```
---

@@ -1,4 +1,13 @@
const fetch = require('node-fetch');
// avoid circular dependency when using jest.mock()
let fetch;
try {
// note that jest is not a global, but is injected somehow into
// the environment. So we can't be safe and check for global.jest
// Hence the try/catch
fetch = jest.requireActual('node-fetch'); //eslint-disable-line no-undef
} catch (e) {
fetch = require('node-fetch');
}
const Request = fetch.Request;
const Response = fetch.Response;
const Headers = fetch.Headers;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.