Apollo-server: running createTestClient with jest

Created on 29 Nov 2018  路  6Comments  路  Source: apollographql/apollo-server

I ran into the following error when attempting to run createTestClient with jest

console.error node_modules/apollo-server-express/node_modules/graphql-extensions/dist/index.js:67
      TypeError: Cannot read property 'method' of undefined
          at EngineReportingExtension.requestDidStart (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/apollo-engine-reporting/src/extension.ts:112:37)
          at handleDidStart.ext (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/graphql-extensions/src/index.ts:89:41)
          at extensions.forEach.extension (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/graphql-extensions/src/index.ts:165:28)
          at Array.forEach (<anonymous>)
          at GraphQLExtensionStack.handleDidStart (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/graphql-extensions/src/index.ts:162:21)
          at GraphQLExtensionStack.requestDidStart (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/graphql-extensions/src/index.ts:88:17)
          at Object.<anonymous> (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/apollo-server-core/src/requestPipeline.ts:158:40)
          at Generator.next (<anonymous>)
          at /Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/requestPipeline.js:7:71
          at new Promise (<anonymous>)
          at Object.<anonymous>.__awaiter (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/requestPipeline.js:3:12)
          at Object.processGraphQLRequest (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/requestPipeline.js:34:12)
          at ApolloServer.<anonymous> (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/apollo-server-core/src/ApolloServer.ts:542:12)
          at Generator.next (<anonymous>)
          at fulfilled (/Users/jonathanmcaboy/Desktop/Dev/Tagg/server/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/ApolloServer.js:4:58)
          at <anonymous>
          at process._tickCallback (internal/process/next_tick.js:188:7)

This seems to be an issue with Apollo engine. When running createTestClient, make sure you leave define engine as null or undefined.

  const testServer = new ApolloServer({
    ...server,
    context: () => ({
      user: {
        id: TEST_ID,
        email: TEST_ID,
      },
    }),
    engine: null,
  });
  const { query } = createTestClient(testServer);

Most helpful comment

Simply set engine to false:

new ApolloServer({
  // ....
  engine: false,
});

Source:

https://github.com/apollographql/apollo-server/blob/b189c1c1565240d9ae58e57396d1a075e8301a84/packages/apollo-server-core/src/ApolloServer.ts#L79-L81

Docs:

We set engine to false, so that the new agent is not used.

https://www.apollographql.com/docs/apollo-server/migration-engine

All 6 comments

Posting for awareness

I faced the same issue, but the workaround suggested did not work for me.

Instead, I removed the .env file in the project and the test passed with this configuration for the server, without specifying anything for engine:

const server = new ApolloServer({
  typeDefs,
  mocks
})

Still, we need a fix for this, so that the regular server could connect to apollo engine, but not it's test counterpart.

The issue for me was sort of both of the above. I didn't remove the entire .env file but removed the ENGINE_API_KEY.
So, I assume the OP's solution would basically set it back to undefined(that alone didn't work though)

I've simply added something like that:

engine: {apiKey: '123'}

No idea what's the correct solution though. Testing modules seem to be heavily unpolished 馃

Simply set engine to false:

new ApolloServer({
  // ....
  engine: false,
});

Source:

https://github.com/apollographql/apollo-server/blob/b189c1c1565240d9ae58e57396d1a075e8301a84/packages/apollo-server-core/src/ApolloServer.ts#L79-L81

Docs:

We set engine to false, so that the new agent is not used.

https://www.apollographql.com/docs/apollo-server/migration-engine

There's an associated bug here where this failure causes the apollo server to hang onto a thread and not let jest exit gracefully - this should probably be addressed.

Was this page helpful?
0 / 5 - 0 ratings