Skip to content

Commit

Permalink
tests: Request life-cycle events, e.g. didEncounterErrors.
Browse files Browse the repository at this point in the history
This accounts for some preliminary tests for things which don't already have
tests, but was primarily motivated by the new `didEncounterErrors`
life-cycle hooks that I introduced in
#2719.
  • Loading branch information
abernix committed May 27, 2019
1 parent 590c1ba commit f056840
Showing 1 changed file with 103 additions and 1 deletion.
104 changes: 103 additions & 1 deletion packages/apollo-server-core/src/__tests__/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,109 @@ describe('runQuery', () => {
});
});

describe('parsing and validation cache', () => {
describe('request pipeline life-cycle hooks', () => {
describe('requestDidStart', () => {
const requestDidStart = jest.fn();
it('called for each request', async () => {
const runOnce = () =>
runQuery({
schema,
queryString: '{ testString }',
plugins: [
{
requestDidStart,
},
],
request: new MockReq(),
});

await runOnce();
expect(requestDidStart.mock.calls.length).toBe(1);
await runOnce();
expect(requestDidStart.mock.calls.length).toBe(2);
});
});

describe('parsingDidStart', () => {
const parsingDidStart = jest.fn();
it('called when parsing will result in an error', async () => {
await runQuery({
schema,
queryString: '{ testStringWithParseError: }',
plugins: [{
requestDidStart() {
return {
parsingDidStart,
};
}
}],
request: new MockReq(),
});

expect(parsingDidStart).toBeCalled();
});

it('called when a successful parse happens', async () => {
await runQuery({
schema,
queryString: '{ testString }',
plugins: [{
requestDidStart() {
return {
parsingDidStart,
};
}
}],
request: new MockReq(),
});

expect(parsingDidStart).toBeCalled();
});
});

describe('didEncounterErrors', () => {
const didEncounterErrors = jest.fn();
it('called when an error occurs', async () => {
await runQuery({
schema,
queryString: '{ testStringWithParseError: }',
plugins: [{
requestDidStart() {
return {
didEncounterErrors,
};
}
}],
request: new MockReq(),
});

expect(didEncounterErrors).toBeCalledWith(
expect.objectContaining({
errors: expect.arrayContaining([expect.any(Error)]),
}),
);
});

it('not called when an error does not occur', async () => {
await runQuery({
schema,
queryString: '{ testString }',
plugins: [{
requestDidStart() {
return {
didEncounterErrors,
};
}
}],
request: new MockReq(),
});

expect(didEncounterErrors).not.toBeCalled();
});
});
});

describe('parsing and validation cache', () => {
function createLifecyclePluginMocks() {
const validationDidStart = jest.fn();
const parsingDidStart = jest.fn();
Expand Down

0 comments on commit f056840

Please sign in to comment.