Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apollo-server-testing throws error: Unable to create test client (TypeError: Cannot read property 'bind' of undefined) #5111

Closed
bigfish opened this issue Apr 14, 2021 · 3 comments

Comments

@bigfish
Copy link

bigfish commented Apr 14, 2021

Using apollo-server & apollo-server-testing 2.22.2

Following documentation to setup an integration test here https://www.apollographql.com/docs/apollo-server/testing/testing/

I get the error when I run jest:

TypeError: Cannot read property 'bind' of undefined

     104 |         });
     105 |         // console.log(server);
   > 106 |         const { query } = createTestClient(server);
         |                           ^
     107 |
     108 |         const res = await query({
     109 |           query: GET_ROUTES

     at createTestClient (node_modules/apollo-server-testing/src/createTestClient.ts:31:52)
     at Object.<anonymous> (spec/routesResolver.spec.js:106:27)

Looking at the referenced source code, it is attempting to bind a function named executeOperation. Apparently this doesn't exist?

export default (server: ApolloServerBase): ApolloServerTestClient => {
  const executeOperation = server.executeOperation.bind(server);

Is there some mismatch between the testing library and the server?

@glasser
Copy link
Member

glasser commented Apr 14, 2021

This is a method that exists. I can't reproduce your issue; if you can post a complete reproduction (eg, a repo I can git clone or something on codesandbox.io) I can reopen and dig into it.

For what it's worth, apollo-server-testing is a VERY thin layer around executeOperation and I don't think having the separate package is really worth the added complexity of having to install another package, etc. I realize it is what we document but I'd suggest just using server.executeOperation directly (once you figure out why it doesn't exist for you?). We may remove apollo-server-testing in Apollo Server 3 (#4952). Here's an example of a PR that changes a project from using apollo-server-testing to using executeOperation: apollographql/federation#554

@glasser glasser closed this as completed Apr 14, 2021
@bigfish
Copy link
Author

bigfish commented Apr 15, 2021

ah, in the process of preparing the repo for the issue, I found my mistake.. I wasn't destructuring the returned value from my createTestClient() helper function, so I was passing an object like { server } to createTestClient(). After passing the server itself, it is working as expected. Perhaps a more helpful error message like 'The argument provided to createTestClient() is not a valid ApolloServer instance: ' could be thrown?

@glasser
Copy link
Member

glasser commented Apr 15, 2021

Maybe, but I think a better approach would just be to change the docs that mention apollo-server-testing to just document server.executeOperation instead and leave apollo-server-testing as a curio that exists only for backwards compatibility.

glasser added a commit that referenced this issue May 25, 2021
The `apollo-server-testing` package exports one small function which is
just a tiny wrapper around `server.executeOperation`. The one main
advantage it provides is that you can pass in operations as ASTs rather
than only as strings.

This extra layer doesn't add much value but does require us to update
things in two places (which cross a package barrier and thus can be
installed at skewed versions). So for example when adding the second
argument to `executeOperation` in #4166 I did not bother to add it to
`apollo-server-testing` too. We've also found that users have been
confused by the `createTestClient` API (eg #5111) and that some linters
get confused by the unbound methods it returns (#4724).

So the simplest thing is to just teach people how to use the real
`ApolloServer` method instead of an unrelated API.

This PR allows you to pass an AST to `server.executeOperation` (just
like with the `apollo-server-testing` API), and changes the docs to
recommend `executeOperation` instead of `apollo-server-testing`. It also
makes some other suggestions about how to test Apollo Server code in a
more end-to-end fashion, and adds some basic tests for
`executeOperation`.

Fixes #4952.
glasser added a commit that referenced this issue May 25, 2021
The `apollo-server-testing` package exports one small function which is
just a tiny wrapper around `server.executeOperation`. The one main
advantage it provides is that you can pass in operations as ASTs rather
than only as strings.

This extra layer doesn't add much value but does require us to update
things in two places (which cross a package barrier and thus can be
installed at skewed versions). So for example when adding the second
argument to `executeOperation` in #4166 I did not bother to add it to
`apollo-server-testing` too. We've also found that users have been
confused by the `createTestClient` API (eg #5111) and that some linters
get confused by the unbound methods it returns (#4724).

So the simplest thing is to just teach people how to use the real
`ApolloServer` method instead of an unrelated API.

This PR allows you to pass an AST to `server.executeOperation` (just
like with the `apollo-server-testing` API), and changes the docs to
recommend `executeOperation` instead of `apollo-server-testing`. It also
makes some other suggestions about how to test Apollo Server code in a
more end-to-end fashion, and adds some basic tests for
`executeOperation`.

Fixes #4952.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants