Skip to content

Commit

Permalink
Fix throws (#310)
Browse files Browse the repository at this point in the history
* revert to throws behaviour from v5 of library

* docs
  • Loading branch information
wheresrhys authored May 14, 2018
1 parent c54413d commit 728f985
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Replaces `fetch` with a stub which records its calls, grouped by route, and opti
* `body`: Set the response body (`string` or `object`)
* `status`: Set the response status (default `200`)
* `headers`: Set the response headers. (`object`)
* `throws`: If this property is present then a the value of `throws` is thrown
* `throws`: If this property is present then fetch returns a `Promise` rejected with the value of `throws`
* `sendAsJson`: This property determines whether or not the request body should be converted to `JSON` before being sent (defaults to `true`).
* `includeContentLength`: Set this property to true to automatically add the `content-length` header (defaults to `true`).
* `redirectUrl`: *experimental* the url the response should be from (to imitate followed redirects - will set `redirected: true` on the response)
Expand Down
15 changes: 7 additions & 8 deletions src/lib/fetch-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ FetchMock.fetchHandler = function (url, opts) {

let response = this.executeRouter(url, opts);

// If the response says to throw an error, throw it
// It only makes sense to do this before doing any async stuff below
// as the async stuff swallows catastrophic errors in a promise
// Type checking is to deal with sinon spies having a throws property :-0
if (response.throws && typeof response !== 'function') {
throw response.throws;
}

// this is used to power the .flush() method
let done
this._holdingPromises.push(new this.config.Promise(res => done = res));
Expand Down Expand Up @@ -55,6 +47,7 @@ FetchMock.executeRouter = function (url, opts) {
}

FetchMock.generateResponse = async function (response, url, opts) {

// We want to allow things like
// - function returning a Promise for a response
// - delaying (using a timeout Promise) a function's execution to generate
Expand All @@ -71,6 +64,12 @@ FetchMock.generateResponse = async function (response, url, opts) {
}
}

// If the response says to throw an error, throw it
// Type checking is to deal with sinon spies having a throws property :-0
if (response.throws && typeof response !== 'function') {
throw response.throws;
}

// If the response is a pre-made Response, respond with it
if (this.config.Response.prototype.isPrototypeOf(response)) {
return response;
Expand Down
45 changes: 27 additions & 18 deletions test/specs/responses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ e.g. {"body": {"status: "registered"}}`)
expect(res.headers.get('content-type')).not.to.exist;
});

it('throw `throws` property exists', async () => {
fm.mock('http://it.at.there/', {throws: 'exists'});
expect(() => fm.fetchHandler('http://it.at.there/')).to.throw();
});

it('not convert if `status` property exists', async () => {
fm.mock('http://it.at.there/', {status: 300});
const res = await fm.fetchHandler('http://it.at.there/')
Expand Down Expand Up @@ -204,6 +199,7 @@ e.g. {"body": {"status: "registered"}}`)
});

describe('response negotiation', () => {

it('function', async () => {
fm.mock('http://it.at.there/', url => url);
const res = await fm.fetchHandler('http://it.at.there/');
Expand Down Expand Up @@ -231,19 +227,6 @@ e.g. {"body": {"status: "registered"}}`)
expect(await res.text()).to.equal('http://it.at.there/');
});

it('response that throws', async () => {
fm.mock('http://it.at.there/', {
throws: 'Oh no'
});

try {
fm.fetchHandler('http://it.at.there/')
expect(true).to.be.false
} catch (err) {
expect(err).to.equal('Oh no');
}
});

it('Response', async () => {
fm.mock('http://it.at.there/', new fm.config.Response('http://it.at.there/', {status: 200}));
const res = await fm.fetchHandler('http://it.at.there/');
Expand All @@ -262,6 +245,32 @@ e.g. {"body": {"status: "registered"}}`)
expect(res.status).to.equal(200);
});

describe('rejecting', () => {
it('reject if object with `throws` property', async () => {
fm.mock('http://it.at.there/', {throws: 'as expected'});

return fm.fetchHandler('http://it.at.there/')
.then(() => {
throw 'not as expected';
})
.catch(err => {
expect(err).to.equal('as expected');
})
});

it('reject if function that returns object with `throws` property', async () => {
fm.mock('http://it.at.there/', () => ({throws: 'as expected'}));

return fm.fetchHandler('http://it.at.there/')
.then(() => {
throw 'not as expected';
})
.catch(err => {
expect(err).to.equal('as expected');
})
});

})
});

});
Expand Down

0 comments on commit 728f985

Please sign in to comment.