nestjs / graphql

GraphQL (TypeScript) module for Nest framework (node.js) 🍷
https://docs.nestjs.com/graphql/quick-start
MIT License
1.46k stars 396 forks source link

req object is empty in ContextFunction in tests #502

Closed alexandr2110pro closed 4 years ago

alexandr2110pro commented 4 years ago

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Hi. I have troubles testing auth with @nestjs/graphl and apollo-server-testing.

I'm creating my apollo test client as:

  const testingModule = await Test.createTestingModule({
    imports: [
      // ... other imports
      GraphQLModule.forRoot({
        autoSchemaFile: 'src/application/schema.gql',
        installSubscriptionHandlers: true,
        debug: true,
        context: ({ req, res }) => ({ req, res }),   // <-- the ContextFunction 
      }),
    ]
  }).compile();

  const app = testingModule.createNestApplication();
  await app.init();

  const graphQLModule = app.get(GraphQLModule);
  const testClient = createTestClient(graphQLModule.apolloServer);

then I have a guard, where I'm using the req object from the context.

And in the compiled app it works, but when I'm running the test for it, both req and res are undefined

Screen Shot 2019-12-03 at 20 34 10

I'd really appreciate if you can point out how to make it work.

Expected behavior

req and res object should be available in the ContextFunction

Minimal reproduction of the problem with instructions

Configure the GraphQLModule and the testClient as shown in the snippet and run tests.

What is the motivation / use case for changing the behavior?

Environment


Nest version: 6.7.1



For Tooling issues:
- Node version:  v13.2.0  
- Platform:  Mac 

Others:
- @nestjs/graphql: 6.5.1
- apollo-server-testing: 2.9.12


alexandr2110pro commented 4 years ago

@kamilmysliwiec

alexandr2110pro commented 4 years ago

In apollo docs here

they say that

... createTestClient provides a single hook to run operations through the request pipeline, enabling the most thorough tests possible without starting up an HTTP server.

Does it mean that I can't test auth done with http cookies/headers this way?

Does anyone know the best way to test graphql api with auth?

alexandr2110pro commented 4 years ago

Just found a related discussion

https://github.com/apollographql/apollo-server/issues/2277

So it is not related to nestjs. But still, how do you guys do integration tests for your graphql apis?

kamilmysliwiec commented 4 years ago

It's not related to NestJS specifically.

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.