Skip to content

Commit

Permalink
tests: Use a fake HTTP context when using executeOperation.
Browse files Browse the repository at this point in the history
This resolves a number of runtime errors which surfaced in the test suite
for this repository, appearing as:

  TypeError: Cannot read property 'method' of undefined

This was overlooked or not addressed within #158 / #246.

While not ideal, today, Apollo Server has a very real expectation of an HTTP
request context.  That will change in the future.  While we can sometimes
make by without it, that is no longer the case when Engine Reporting is
enabled since it relies on the HTTP "method" property of the HTTP context
when building the traces.  Therefore, we'll need to make sure that we
provide a fake HTTP context to `executeOperation` when testing with Engine
enabled, to ensure that it doesn't fail.

cc @jsegaran
  • Loading branch information
abernix committed Mar 19, 2020
1 parent 50ae02d commit a132c2e
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ import {
nockGoodManifestsUnderStorageSecret,
genericStorageSecret,
} from './helpers.test-helpers';
import { Headers } from 'apollo-server-env';
import { GraphQLRequest } from 'apollo-server-plugin-base';

// While not ideal, today, Apollo Server has a very real expectation of an HTTP
// request context. That will change in the future. While we can sometimes
// make by without it, that is no longer the case when Engine Reporting is
// enabled since it relies on the HTTP "method" property of the HTTP context
// when building the traces. Therefore, we'll need to make sure that we provide
// a fake HTTP context to `executeOperation` when testing with Engine enabled,
// to ensure that it doesn't fail.
const mockHttpRequestContextForExecuteOperation: Required<Pick<
GraphQLRequest,
'http'
>> = {
http: { method: 'GET', headers: new Headers(), url: '/mocked' },
};

describe('Operation registry plugin', () => {
it('will instantiate when not called with options', () => {
Expand Down Expand Up @@ -84,6 +100,7 @@ describe('Operation registry plugin', () => {
});
await server.willStart();
const result = await server.executeOperation({
...mockHttpRequestContextForExecuteOperation,
query: print(query),
operationName: 'HelloFam',
});
Expand Down Expand Up @@ -136,6 +153,7 @@ describe('Operation registry plugin', () => {
});
await server.willStart();
const result = await server.executeOperation({
...mockHttpRequestContextForExecuteOperation,
query: print(query),
operationName: 'HelloFam',
});
Expand Down Expand Up @@ -175,6 +193,7 @@ describe('Operation registry plugin', () => {
});
await server.willStart();
const result = await server.executeOperation({
...mockHttpRequestContextForExecuteOperation,
query: print(query),
operationName: 'HelloFam',
});
Expand Down Expand Up @@ -230,6 +249,7 @@ describe('Operation registry plugin', () => {
});
await server.willStart();
const result = await server.executeOperation({
...mockHttpRequestContextForExecuteOperation,
query: print(query),
operationName: 'HelloFam',
});
Expand Down Expand Up @@ -269,6 +289,7 @@ describe('Operation registry plugin', () => {
});
await server.willStart();
const result = await server.executeOperation({
...mockHttpRequestContextForExecuteOperation,
query: print(query),
operationName: 'HelloFam',
});
Expand Down

0 comments on commit a132c2e

Please sign in to comment.