Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: wheresrhys/fetch-mock
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v8.1.0
Choose a base ref
...
head repository: wheresrhys/fetch-mock
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v8.2.1
Choose a head ref
  • 9 commits
  • 5 files changed
  • 3 contributors

Commits on Dec 17, 2019

  1. fix(abort): throwing DOMEXception when available

    As per spec when a fetch is aborted a DOMException is thrown with
    name 'AbortError'. However, the current implemnetation threw a custom
    error instance.  That should work for node.js due to unavailibility of
    DOMException. This commit ensures that when DOMException is available
    it is thrown instead.
    
    Related: #416
    Sayan751 committed Dec 17, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8adfe7e View commit details

Commits on Dec 19, 2019

  1. Copy the full SHA
    28ad321 View commit details
  2. setup proper beta releases

    wheresrhys committed Dec 19, 2019
    Copy the full SHA
    c23047f View commit details
  3. fix lnting

    wheresrhys committed Dec 19, 2019
    Copy the full SHA
    61a5056 View commit details
  4. Copy the full SHA
    c715c16 View commit details
  5. document new structure

    wheresrhys committed Dec 19, 2019
    Copy the full SHA
    d52e7cf View commit details
  6. Merge pull request #485 from Sayan751/topic/abort-error

    fix(abort): throwing DOMEXception when available
    wheresrhys authored Dec 19, 2019
    Copy the full SHA
    5c4f18c View commit details
  7. Merge pull request #486 from wheresrhys/jest-play-nice

    Jest play nice
    wheresrhys authored Dec 19, 2019
    Copy the full SHA
    0ccc604 View commit details

Commits on Dec 21, 2019

  1. lint

    wheresrhys committed Dec 21, 2019
    Copy the full SHA
    9a89458 View commit details
Showing with 54 additions and 24 deletions.
  1. +6 −1 .circleci/config.yml
  2. +20 −21 docs/_usage/usage-with-jest.md
  3. +6 −1 src/lib/fetch-handler.js
  4. +10 −1 src/server.js
  5. +12 −0 test/specs/abortable.test.js
7 changes: 6 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -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
41 changes: 20 additions & 21 deletions docs/_usage/usage-with-jest.md
Original file line number Diff line number Diff line change
@@ -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);
});
```
---

7 changes: 6 additions & 1 deletion src/lib/fetch-handler.js
Original file line number Diff line number Diff line change
@@ -68,7 +68,12 @@ FetchMock.fetchHandler = function(url, options, request) {
return new this.config.Promise((res, rej) => {
if (signal) {
const abort = () => {
rej(new AbortError());
// note that DOMException is not available in node.js; even node-fetch uses a custom error class: https://github.com/bitinn/node-fetch/blob/master/src/abort-error.js
rej(
typeof DOMException !== 'undefined'
? new DOMException('The operation was aborted.', 'AbortError')
: new AbortError()
);
done();
};
if (signal.aborted) {
11 changes: 10 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions test/specs/abortable.test.js
Original file line number Diff line number Diff line change
@@ -25,6 +25,9 @@ module.exports = (fetchMock, AbortController) => {
signal: controller.signal
});
} catch (error) {
if (typeof DOMException !== 'undefined') {
expect(error instanceof DOMException).to.equal(true);
}
expect(error.name).to.equal('AbortError');
expect(error.message).to.equal('The operation was aborted.');
}
@@ -49,6 +52,9 @@ module.exports = (fetchMock, AbortController) => {
})
);
} catch (error) {
if (typeof DOMException !== 'undefined') {
expect(error instanceof DOMException).to.equal(true);
}
console.error(error);
expect(error.name).to.equal('AbortError');
expect(error.message).to.equal('The operation was aborted.');
@@ -66,6 +72,9 @@ module.exports = (fetchMock, AbortController) => {
signal: controller.signal
});
} catch (error) {
if (typeof DOMException !== 'undefined') {
expect(error instanceof DOMException).to.equal(true);
}
expect(error.name).to.equal('AbortError');
expect(error.message).to.equal('The operation was aborted.');
}
@@ -87,6 +96,9 @@ module.exports = (fetchMock, AbortController) => {
signal: controller.signal
});
} catch (error) {
if (typeof DOMException !== 'undefined') {
expect(error instanceof DOMException).to.equal(true);
}
expect(fm.done()).to.be.true;
}
});