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.0.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.0.1
Choose a head ref
  • 10 commits
  • 7 files changed
  • 4 contributors

Commits on Nov 19, 2019

  1. Verified

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

Commits on Nov 25, 2019

  1. Copy the full SHA
    3b78031 View commit details

Commits on Nov 28, 2019

  1. Copy the full SHA
    aa1fd8f View commit details
  2. Copy the full SHA
    02de2f8 View commit details

Commits on Dec 4, 2019

  1. Merge branch 'bugs/abort' of https://github.com/Sayan751/fetch-mock i…

    …nto signal-request
    
    * 'bugs/abort' of https://github.com/Sayan751/fetch-mock:
      refactor(all): post review changes
      fix(abort): no abort if signal was in request
    wheresrhys committed Dec 4, 2019
    Copy the full SHA
    2c23a6c View commit details
  2. Copy the full SHA
    fcbdc2a View commit details
  3. Copy the full SHA
    b384567 View commit details
  4. Merge pull request #475 from koba04/patch-1

    remove a duplicated documentation for repeat option
    wheresrhys authored Dec 4, 2019
    Copy the full SHA
    502cf11 View commit details
  5. solve the signal problem

    wheresrhys committed Dec 4, 2019
    Copy the full SHA
    ee581ff View commit details
  6. Merge pull request #479 from wheresrhys/signal-request

    Signal request
    wheresrhys authored Dec 4, 2019
    Copy the full SHA
    67c3abb View commit details
Showing with 189 additions and 116 deletions.
  1. +0 −5 docs/_api-mocking/mock_options.md
  2. +1 −1 docs/v6-v7-upgrade-guide.md
  3. +116 −84 package-lock.json
  4. +9 −5 src/lib/fetch-handler.js
  5. +11 −11 src/lib/request-utils.js
  6. +25 −0 test/specs/abortable.test.js
  7. +27 −10 test/specs/inspecting.test.js
5 changes: 0 additions & 5 deletions docs/_api-mocking/mock_options.md
Original file line number Diff line number Diff line change
@@ -75,11 +75,6 @@ multiParameters:
- Integer
content: |-
Limits the number of times the route can be used. If the route has already been called `repeat` times, the call to `fetch()` will fall through to be handled by any other routes defined (which may eventually result in an error if nothing matches it)
- name: repeat
types:
- Integer
content: |-
Limits the number of times the route can be used. If the route has already been called `repeat` times, the call to `fetch()` will fall through to be handled by any other routes defined (which may eventually result in an error if nothing matches it)
- parametersBlockTitle: Response options
parameters:
- name: delay
2 changes: 1 addition & 1 deletion docs/v6-v7-upgrade-guide.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Upgrading from V6 to V7
# Upgrading from V6 to V7 or v8

# Changes

200 changes: 116 additions & 84 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions src/lib/fetch-handler.js
Original file line number Diff line number Diff line change
@@ -47,11 +47,15 @@ const resolve = async (
};

FetchMock.fetchHandler = function(url, options, request) {
({ url, options, request } = requestUtils.normalizeRequest(
const normalizedRequest = requestUtils.normalizeRequest(
url,
options,
this.config.Request
));
);

({ url, options, request } = normalizedRequest);

const { signal } = normalizedRequest;

const route = this.executeRouter(url, options, request);

@@ -62,15 +66,15 @@ FetchMock.fetchHandler = function(url, options, request) {
// wrapped in this promise to make sure we respect custom Promise
// constructors defined by the user
return new this.config.Promise((res, rej) => {
if (options && options.signal) {
if (signal) {
const abort = () => {
rej(new AbortError());
done();
};
if (options.signal.aborted) {
if (signal.aborted) {
abort();
}
options.signal.addEventListener('abort', abort);
signal.addEventListener('abort', abort);
}

this.generateResponse(route, url, options, request)
22 changes: 11 additions & 11 deletions src/lib/request-utils.js
Original file line number Diff line number Diff line change
@@ -39,31 +39,31 @@ module.exports = {
},
normalizeRequest: (url, options, Request) => {
if (Request.prototype.isPrototypeOf(url)) {
const obj = {
const derivedOptions = {
method: url.method
};
const normalizedRequestObject = {
url: normalizeUrl(url.url),
options: Object.assign(
{
method: url.method
},
options
),
request: url
options: Object.assign(derivedOptions, options),
request: url,
signal: (options && options.signal) || url.signal
};

const headers = headersToArray(url.headers);

if (headers.length) {
obj.options.headers = zipObject(headers);
normalizedRequestObject.options.headers = zipObject(headers);
}
return obj;
return normalizedRequestObject;
} else if (
typeof url === 'string' ||
// horrible URL object duck-typing
(typeof url === 'object' && 'href' in url)
) {
return {
url: normalizeUrl(url),
options: options
options: options,
signal: options && options.signal
};
} else if (typeof url === 'object') {
throw new TypeError(
25 changes: 25 additions & 0 deletions test/specs/abortable.test.js
Original file line number Diff line number Diff line change
@@ -30,6 +30,31 @@ module.exports = (fetchMock, AbortController) => {
}
});

it('error on signal abort for request object', async () => {
fm.mock('http://it.at.there/', () => {
return new Promise(resolve => {
setTimeout(() => {
resolve({});
}, 500);
});
});

const controller = new AbortController();
setTimeout(() => controller.abort(), 300);

try {
await fm.fetchHandler(
new fm.config.Request('http://it.at.there/', {
signal: controller.signal
})
);
} catch (error) {
console.error(error);
expect(error.name).to.equal('AbortError');
expect(error.message).to.equal('The operation was aborted.');
}
});

it('error when signal already aborted', async () => {
const controller = new AbortController();
controller.abort();
37 changes: 27 additions & 10 deletions test/specs/inspecting.test.js
Original file line number Diff line number Diff line change
@@ -485,12 +485,13 @@ module.exports = fetchMock => {
method: 'POST'
});
fm.fetchHandler(req);
expect(fm.lastCall()).to.eql([
'http://it.at.here/',
{ method: 'POST' }
]);
const [url, callOptions] = fm.lastCall();

expect(url).to.equal('http://it.at.here/');
expect(callOptions).to.include({ method: 'POST' });
expect(fm.lastUrl()).to.equal('http://it.at.here/');
expect(fm.lastOptions()).to.eql({ method: 'POST' });
const options = fm.lastOptions();
expect(options).to.eql({ method: 'POST' });
expect(fm.lastCall().request).to.equal(req);
});

@@ -499,17 +500,33 @@ module.exports = fetchMock => {
method: 'POST'
});
fm.fetchHandler(req, { arbitraryOption: true });
expect(fm.lastCall()).to.eql([
'http://it.at.here/',
{ method: 'POST', arbitraryOption: true }
]);
const [url, callOptions] = fm.lastCall();
expect(url).to.equal('http://it.at.here/');
expect(callOptions).to.include({
method: 'POST',
arbitraryOption: true
});
expect(fm.lastUrl()).to.equal('http://it.at.here/');
expect(fm.lastOptions()).to.eql({
const options = fm.lastOptions();

expect(options).to.eql({
method: 'POST',
arbitraryOption: true
});
expect(fm.lastCall().request).to.equal(req);
});

it('Not make default signal available in options when called with Request instance using signal', () => {
const req = new fm.config.Request('http://it.at.here/', {
method: 'POST'
});
fm.fetchHandler(req);
const [, callOptions] = fm.lastCall();

expect(callOptions.signal).to.be.undefined;
const options = fm.lastOptions();
expect(options.signal).to.be.undefined;
});
});

describe('flushing pending calls', () => {