From 69e59b72a91f0598cee62d3dfbfc41f3380735eb Mon Sep 17 00:00:00 2001 From: James Reggio Date: Sat, 23 Sep 2017 19:19:59 -0400 Subject: [PATCH] Use done.fail(e) instead of done(e) for forced failures --- .../apollo-client/src/__tests__/client.ts | 20 +++---- .../apollo-client/src/__tests__/fetchMore.ts | 40 +++++++++----- .../src/__tests__/mutationResults.ts | 20 +++---- .../apollo-client/src/__tests__/optimistic.ts | 16 +++--- .../src/core/__tests__/ObservableQuery.ts | 10 ++-- .../src/core/__tests__/QueryManager/index.ts | 54 ++++++++++--------- .../src/scheduler/__tests__/scheduler.ts | 4 +- .../src/util/subscribeAndCount.ts | 4 +- packages/apollo-client/src/util/wrap.ts | 2 +- 9 files changed, 95 insertions(+), 75 deletions(-) diff --git a/packages/apollo-client/src/__tests__/client.ts b/packages/apollo-client/src/__tests__/client.ts index ce4b6186d6a..691305257cd 100644 --- a/packages/apollo-client/src/__tests__/client.ts +++ b/packages/apollo-client/src/__tests__/client.ts @@ -601,7 +601,7 @@ describe('client', () => { if (e === expectedError) { done(); } else { - done(e); + done.fail(e); } }; process.removeListener('uncaughtException', oldHandler); @@ -656,7 +656,7 @@ describe('client', () => { if (e === expectedError) { done(); } else { - done(e); + done.fail(e); } }; process.removeListener('uncaughtException', oldHandler); @@ -685,7 +685,7 @@ describe('client', () => { const handle = client.watchQuery({ query }); handle.subscribe({ next() { - done(new Error('did not expect next to be called')); + done.fail(new Error('did not expect next to be called')); }, error() { throw expectedError; @@ -1242,11 +1242,11 @@ describe('client', () => { done(); }) .catch(err => { - done(err); + done.fail(err); }); }, error(err) { - done(err); + done.fail(err); }, }); }); @@ -1649,7 +1649,9 @@ describe('client', () => { } if (handleCount === 2) { handleCalled = true; - done(new Error('Handle should never be called on standby query')); + done.fail( + new Error('Handle should never be called on standby query'), + ); } }); }); @@ -1845,7 +1847,7 @@ describe('client', () => { client .mutate({ mutation }) .then(_ => { - done(new Error('Returned a result when it should not have.')); + done.fail(new Error('Returned a result when it should not have.')); }) .catch((error: ApolloError) => { expect(error.networkError).toBeDefined(); @@ -1882,7 +1884,7 @@ describe('client', () => { client .mutate({ mutation }) .then(_ => { - done(new Error('Returned a result when it should not have.')); + done.fail(new Error('Returned a result when it should not have.')); }) .catch((error: ApolloError) => { expect(error.graphQLErrors).toBeDefined(); @@ -2009,7 +2011,7 @@ describe('client', () => { ).toBe(1); mutatePromise .then(_ => { - done(new Error('Returned a result when it should not have.')); + done.fail(new Error('Returned a result when it should not have.')); }) .catch((_: ApolloError) => { expect( diff --git a/packages/apollo-client/src/__tests__/fetchMore.ts b/packages/apollo-client/src/__tests__/fetchMore.ts index 401dc8604a2..c97ac9e3895 100644 --- a/packages/apollo-client/src/__tests__/fetchMore.ts +++ b/packages/apollo-client/src/__tests__/fetchMore.ts @@ -361,11 +361,11 @@ describe('fetchMore on an observable query', () => { done(); break; default: - done(new Error('`next` called too many times')); + done.fail(new Error('`next` called too many times')); } }, - error: error => done(error), - complete: () => done(new Error('Should not have completed')), + error: error => done.fail(error), + complete: () => done.fail(new Error('Should not have completed')), }); }); @@ -414,7 +414,9 @@ describe('fetchMore on an observable query', () => { expect((data as any).entry.comments.length).toBe(10); break; default: - done(new Error('`next` called when it wasn’t supposed to be.')); + done.fail( + new Error('`next` called when it wasn’t supposed to be.'), + ); } }, error: error => { @@ -425,14 +427,18 @@ describe('fetchMore on an observable query', () => { done(); break; default: - done(new Error('`error` called when it wasn’t supposed to be.')); + done.fail( + new Error('`error` called when it wasn’t supposed to be.'), + ); } } catch (error) { - done(error); + done.fail(error); } }, complete: () => - done(new Error('`complete` called when it wasn’t supposed to be.')), + done.fail( + new Error('`complete` called when it wasn’t supposed to be.'), + ), }); }); }); @@ -612,11 +618,11 @@ describe('fetchMore on an observable query with connection', () => { done(); break; default: - done(new Error('`next` called too many times')); + done.fail(new Error('`next` called too many times')); } }, - error: error => done(error), - complete: () => done(new Error('Should not have completed')), + error: error => done.fail(error), + complete: () => done.fail(new Error('Should not have completed')), }); }); @@ -665,7 +671,9 @@ describe('fetchMore on an observable query with connection', () => { expect((data as any).entry.comments.length).toBe(10); break; default: - done(new Error('`next` called when it wasn’t supposed to be.')); + done.fail( + new Error('`next` called when it wasn’t supposed to be.'), + ); } }, error: error => { @@ -676,14 +684,18 @@ describe('fetchMore on an observable query with connection', () => { done(); break; default: - done(new Error('`error` called when it wasn’t supposed to be.')); + done.fail( + new Error('`error` called when it wasn’t supposed to be.'), + ); } } catch (error) { - done(error); + done.fail(error); } }, complete: () => - done(new Error('`complete` called when it wasn’t supposed to be.')), + done.fail( + new Error('`complete` called when it wasn’t supposed to be.'), + ), }); }); }); diff --git a/packages/apollo-client/src/__tests__/mutationResults.ts b/packages/apollo-client/src/__tests__/mutationResults.ts index 19352ff7c70..9f7be9f452e 100644 --- a/packages/apollo-client/src/__tests__/mutationResults.ts +++ b/packages/apollo-client/src/__tests__/mutationResults.ts @@ -555,7 +555,7 @@ describe('mutation results', () => { }, }) .then( - () => done(new Error('Mutation should have failed')), + () => done.fail(new Error('Mutation should have failed')), () => client.mutate({ mutation, @@ -570,10 +570,10 @@ describe('mutation results', () => { }), ) .then( - () => done(new Error('Mutation should have failed')), + () => done.fail(new Error('Mutation should have failed')), () => obsHandle.refetch(), ) - .then(() => done(), done); + .then(() => done(), done.fail); }, }); }); @@ -690,7 +690,7 @@ describe('mutation results', () => { const firstSubs = watchedQuery.subscribe({ next: () => null, - error: done, + error: done.fail, }); // Cancel the query right away! @@ -800,7 +800,7 @@ describe('mutation results', () => { }); done(); }) - .catch(done); + .catch(done.fail); }); it('allows mutations with default values', done => { @@ -878,7 +878,7 @@ describe('mutation results', () => { }); done(); }) - .catch(done); + .catch(done.fail); }); it('will pass null to the network interface when provided', done => { @@ -957,7 +957,7 @@ describe('mutation results', () => { }); done(); }) - .catch(done); + .catch(done.fail); }); describe('store transaction updater', () => { @@ -1192,7 +1192,7 @@ describe('mutation results', () => { }, }) .then( - () => done(new Error('Mutation should have failed')), + () => done.fail(new Error('Mutation should have failed')), () => client.mutate({ mutation, @@ -1228,10 +1228,10 @@ describe('mutation results', () => { }), ) .then( - () => done(new Error('Mutation should have failed')), + () => done.fail(new Error('Mutation should have failed')), () => obsHandle.refetch(), ) - .then(() => done(), done); + .then(() => done(), done.fail); }, }); }); diff --git a/packages/apollo-client/src/__tests__/optimistic.ts b/packages/apollo-client/src/__tests__/optimistic.ts index f10453c2a30..b03101f1932 100644 --- a/packages/apollo-client/src/__tests__/optimistic.ts +++ b/packages/apollo-client/src/__tests__/optimistic.ts @@ -1175,10 +1175,10 @@ describe('optimistic mutation results', () => { done(); break; default: - done(new Error('Next should not have been called again.')); + done.fail(new Error('Next should not have been called again.')); } }, - error: error => done(error), + error: error => done.fail(error), }); function twoMutations() { @@ -1188,7 +1188,7 @@ describe('optimistic mutation results', () => { optimisticResponse: customOptimisticResponse1, updateQueries, }) - .catch(error => done(error)); + .catch(error => done.fail(error)); client .mutate({ @@ -1196,7 +1196,7 @@ describe('optimistic mutation results', () => { optimisticResponse: customOptimisticResponse2, updateQueries, }) - .catch(error => done(error)); + .catch(error => done.fail(error)); } }); }); @@ -1681,10 +1681,10 @@ describe('optimistic mutation results', () => { done(); break; default: - done(new Error('Next should not have been called again.')); + done.fail(new Error('Next should not have been called again.')); } }, - error: error => done(error), + error: error => done.fail(error), }); function twoMutations() { @@ -1694,7 +1694,7 @@ describe('optimistic mutation results', () => { optimisticResponse: customOptimisticResponse1, update, }) - .catch(error => done(error)); + .catch(error => done.fail(error)); client .mutate({ @@ -1702,7 +1702,7 @@ describe('optimistic mutation results', () => { optimisticResponse: customOptimisticResponse2, update, }) - .catch(error => done(error)); + .catch(error => done.fail(error)); } }); }); diff --git a/packages/apollo-client/src/core/__tests__/ObservableQuery.ts b/packages/apollo-client/src/core/__tests__/ObservableQuery.ts index 35c3fc244b7..f045772a769 100644 --- a/packages/apollo-client/src/core/__tests__/ObservableQuery.ts +++ b/packages/apollo-client/src/core/__tests__/ObservableQuery.ts @@ -123,7 +123,7 @@ describe('ObservableQuery', () => { jest.runTimersToTime(100); done(); } else if (handleCount === 2) { - done(new Error('Should not get more than one result')); + done.fail(new Error('Should not get more than one result')); } }); @@ -604,7 +604,7 @@ describe('ObservableQuery', () => { expect(result2.data).toEqual(dataTwo); try { (result2.data as any).stuff = 'awful'; - done( + done.fail( new Error( 'results from setVariables should be frozen in development mode', ), @@ -993,7 +993,7 @@ describe('ObservableQuery', () => { stale: false, }); } catch (e) { - done(e); + done.fail(e); } if (count === 1) { @@ -1003,7 +1003,7 @@ describe('ObservableQuery', () => { setTimeout(done, 5); } if (count > 3) { - done(new Error('Observable.next called too many times')); + done.fail(new Error('Observable.next called too many times')); } }); }); @@ -1292,7 +1292,7 @@ describe('ObservableQuery', () => { } else if (handleCount === 2) { // oops! we are polling for data, this should not happen. startedPolling = true; - done(new Error('should not start polling, already stopped')); + done.fail(new Error('should not start polling, already stopped')); } }); diff --git a/packages/apollo-client/src/core/__tests__/QueryManager/index.ts b/packages/apollo-client/src/core/__tests__/QueryManager/index.ts index 69f65eca8c7..0717c7e784d 100644 --- a/packages/apollo-client/src/core/__tests__/QueryManager/index.ts +++ b/packages/apollo-client/src/core/__tests__/QueryManager/index.ts @@ -193,7 +193,7 @@ describe('QueryManager', () => { }, observer: { next() { - done( + done.fail( new Error('Returned a result when it was supposed to error out'), ); }, @@ -275,7 +275,9 @@ describe('QueryManager', () => { }, observer: { next() { - done(new Error('Returned data when it was supposed to error out.')); + done.fail( + new Error('Returned data when it was supposed to error out.'), + ); }, error(apolloError) { @@ -337,7 +339,7 @@ describe('QueryManager', () => { }, observer: { next() { - done(new Error('Should not fire next for an error')); + done.fail(new Error('Should not fire next for an error')); }, error(error) { expect((error as any).graphQLErrors).toEqual([null]); @@ -363,7 +365,7 @@ describe('QueryManager', () => { error: new Error('Network error'), observer: { next: () => { - done(new Error('Should not deliver result')); + done.fail(new Error('Should not deliver result')); }, error: error => { const apolloError = error as ApolloError; @@ -396,7 +398,7 @@ describe('QueryManager', () => { error: new Error('Network error'), observer: { next: () => { - done(new Error('Should not deliver result')); + done.fail(new Error('Should not deliver result')); }, }, }); @@ -425,10 +427,10 @@ describe('QueryManager', () => { delay: 1000, observer: { next: () => { - done(new Error('Should not deliver result')); + done.fail(new Error('Should not deliver result')); }, error: () => { - done(new Error('Should not deliver result')); + done.fail(new Error('Should not deliver result')); }, }, }); @@ -562,7 +564,7 @@ describe('QueryManager', () => { subOne.unsubscribe(); handle.refetch(); } catch (e) { - done(e); + done.fail(e); } }, 0); } else if (subTwoCount === 3) { @@ -571,7 +573,7 @@ describe('QueryManager', () => { expect(subOneCount).toBe(2); done(); } catch (e) { - done(e); + done.fail(e); } }, 0); } @@ -701,10 +703,10 @@ describe('QueryManager', () => { throw new Error('Next run too many times.'); } } catch (error) { - done(error); + done.fail(error); } }, - error: error => done(error), + error: error => done.fail(error), }); }); @@ -747,10 +749,10 @@ describe('QueryManager', () => { expect(result.data).toEqual(observable.currentResult().data); done(); } catch (error) { - done(error); + done.fail(error); } }, - error: error => done(error), + error: error => done.fail(error), }); }); @@ -1208,7 +1210,7 @@ describe('QueryManager', () => { done: () => {}, observer: { next() { - // done(new Error('Returned a result when it should not have.')); + // done.fail(new Error('Returned a result when it should not have.')); }, error(error) { expect(error).toBeInstanceOf(Error); @@ -1646,7 +1648,7 @@ describe('QueryManager', () => { }) .query({ query }) .then(() => { - done(new Error('Returned result on an errored fetchQuery')); + done.fail(new Error('Returned result on an errored fetchQuery')); }) .catch(error => { const apolloError = error as ApolloError; @@ -1656,7 +1658,7 @@ describe('QueryManager', () => { expect(apolloError.graphQLErrors).toEqual([]); done(); }) - .catch(done); + .catch(done.fail); }); it('should error when we attempt to give an id beginning with $', done => { @@ -1690,7 +1692,7 @@ describe('QueryManager', () => { }) .query({ query }) .then(() => { - done(new Error('Returned a result when it should not have.')); + done.fail(new Error('Returned a result when it should not have.')); }) .catch(() => { done(); @@ -1758,7 +1760,9 @@ describe('QueryManager', () => { queryManager .query({ query, fetchPolicy: 'network-only' }) .then(() => { - done(new Error('Returned a result when it was not supposed to.')); + done.fail( + new Error('Returned a result when it was not supposed to.'), + ); }) .catch(() => { // make that the error thrown doesn't empty the state @@ -1771,7 +1775,7 @@ describe('QueryManager', () => { }); }) .catch(() => { - done(new Error('Threw an error on the first query.')); + done.fail(new Error('Threw an error on the first query.')); }); }); @@ -2230,7 +2234,7 @@ describe('QueryManager', () => { handle .refetch() .then(() => { - done(new Error('Error on refetch should reject promise')); + done.fail(new Error('Error on refetch should reject promise')); }) .catch(error => { expect(error.graphQLErrors).toEqual([ @@ -2364,7 +2368,7 @@ describe('QueryManager', () => { case 2: default: doneCalled = true; - done(new Error('Only expected one result, not multiple')); + done.fail(new Error('Only expected one result, not multiple')); } }, }); @@ -3038,7 +3042,7 @@ describe('QueryManager', () => { queryManager .fetchQuery('made up id', { query }) .then(() => { - done(new Error('Returned a result.')); + done.fail(new Error('Returned a result.')); }) .catch(error => { expect(error.message).toMatch('Store reset'); @@ -3169,7 +3173,7 @@ describe('QueryManager', () => { queryManager .query({ query }) .then(() => { - done(new Error('query() gave results on a store reset')); + done.fail(new Error('query() gave results on a store reset')); }) .catch(() => { done(); @@ -3279,10 +3283,10 @@ describe('QueryManager', () => { done(); break; default: - done(new Error('`next` was called to many times.')); + done.fail(new Error('`next` was called to many times.')); } }, - error: error => done(error), + error: error => done.fail(error), }); }); }); diff --git a/packages/apollo-client/src/scheduler/__tests__/scheduler.ts b/packages/apollo-client/src/scheduler/__tests__/scheduler.ts index 6ecb950bf8d..1442cbeefef 100644 --- a/packages/apollo-client/src/scheduler/__tests__/scheduler.ts +++ b/packages/apollo-client/src/scheduler/__tests__/scheduler.ts @@ -205,7 +205,9 @@ describe('QueryScheduler', () => { let observableQuery = scheduler.registerPollingQuery(queryOptions); const subscription = observableQuery.subscribe({ next() { - done(new Error('Observer provided a result despite a network error.')); + done.fail( + new Error('Observer provided a result despite a network error.'), + ); }, error(errorVal) { diff --git a/packages/apollo-client/src/util/subscribeAndCount.ts b/packages/apollo-client/src/util/subscribeAndCount.ts index 35a39caf812..23685547eef 100644 --- a/packages/apollo-client/src/util/subscribeAndCount.ts +++ b/packages/apollo-client/src/util/subscribeAndCount.ts @@ -19,11 +19,11 @@ export default function subscribeAndCount( // to be defined. setImmediate(() => { subscription.unsubscribe(); - done(e); + done.fail(e); }); } }, - error: done, + error: done.fail, }); return subscription; } diff --git a/packages/apollo-client/src/util/wrap.ts b/packages/apollo-client/src/util/wrap.ts index 2379be6c312..bfe752308d7 100644 --- a/packages/apollo-client/src/util/wrap.ts +++ b/packages/apollo-client/src/util/wrap.ts @@ -6,7 +6,7 @@ export default (done: (...args) => void, cb: (...args: any[]) => any) => ( try { return cb(...args); } catch (e) { - done(e); + done.fail(e); } };