Skip to content

Commit

Permalink
Use jset.fn()
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Segaran committed May 13, 2020
1 parent 116c286 commit f36fcab
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 85 deletions.
160 changes: 76 additions & 84 deletions packages/apollo-engine-reporting/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ it('trace construction', async () => {
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });

const traces: Array<AddTraceArgs> = [];
async function addTrace(args: AddTraceArgs) {
traces.push(args);
}
const addTrace = jest.fn();

const pluginInstance = plugin({ /* no options!*/ }, addTrace);

Expand Down Expand Up @@ -322,96 +319,91 @@ function makeTestHTTP(): Trace.HTTP {
}

describe("tests for the shouldReportQuery reporting option", () => {
it("report no traces", async () => {
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });

async function addTrace(_args: AddTraceArgs) {
throw new Error("Should not add any traces");
}

const pluginInstance = plugin({report: false}, addTrace);

pluginTestHarness({
pluginInstance,
schema,
graphqlRequest: {
query,
operationName: 'q',
extensions: {
clientName: 'testing suite',
},
http: new Request('http://localhost:123/foo'),
it("report no traces", async () => {
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });

const addTrace = jest.fn();

const pluginInstance = plugin({ report: false }, addTrace);

pluginTestHarness({
pluginInstance,
schema,
graphqlRequest: {
query,
operationName: 'q',
extensions: {
clientName: 'testing suite',
},
executor: async ({ request: { query: source }}) => {
return await graphql({
schema,
source,
});
},
});
http: new Request('http://localhost:123/foo'),
},
executor: async ({ request: { query: source } }) => {
return await graphql({
schema,
source,
});
},
});
});

it("report traces based on operation name", async () => {
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });

let tracesAdded = 0;

async function addTrace(_args: AddTraceArgs) {
tracesAdded += 1;
}
it('report traces based on operation name', async () => {
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });

const pluginInstance = plugin(
{
report: async (request) => {
return request.operationName === 'report'
}
}, addTrace);
const addTrace = jest.fn();

pluginTestHarness({
pluginInstance,
schema,
graphqlRequest: {
query: queryReport,
operationName: 'report',
extensions: {
clientName: 'testing suite',
},
http: new Request('http://localhost:123/foo'),
},
executor: async ({ request: { query: source }}) => {
return await graphql({
schema,
source,
});
const pluginInstance = plugin(
{
report: async (request) => {
return request.operationName === 'report'
}
}, addTrace);

pluginTestHarness({
pluginInstance,
schema,
graphqlRequest: {
query: queryReport,
operationName: 'report',
extensions: {
clientName: 'testing suite',
},
});
expect(tracesAdded).toEqual(1);
http: new Request('http://localhost:123/foo'),
},
executor: async ({ request: { query: source } }) => {
return await graphql({
schema,
source,
});
},
});

pluginTestHarness({
pluginInstance,
schema,
graphqlRequest: {
query,
operationName: 'q',
extensions: {
clientName: 'testing suite',
},
http: new Request('http://localhost:123/foo'),
},
executor: async ({ request: { query: source }}) => {
return await graphql({
schema,
source,
});
expect(addTrace).toBeCalledTimes(1);
addTrace.mockReset()

pluginTestHarness({
pluginInstance,
schema,
graphqlRequest: {
query,
operationName: 'q',
extensions: {
clientName: 'testing suite',
},
});

expect(tracesAdded).toEqual(1);

http: new Request('http://localhost:123/foo'),
},
executor: async ({ request: { query: source } }) => {
return await graphql({
schema,
source,
});
},
});

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


/**
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-engine-reporting/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export class EngineReportingAgent<TContext = any> {
this.reportSizes[schemaHash] +=
encodedTrace.length + Buffer.byteLength(statsReportKey);

// If the buffer gets big (according to our estimate), send.
// If the buffer gets big (accordin to our estimate), send.
if (
this.sendReportsImmediately ||
this.reportSizes[schemaHash] >=
Expand Down

0 comments on commit f36fcab

Please sign in to comment.