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

req object is empty in ContextFunction in tests #502

Closed
alexandr2110pro opened this issue Dec 3, 2019 · 5 comments
Closed

req object is empty in ContextFunction in tests #502

alexandr2110pro opened this issue Dec 3, 2019 · 5 comments

Comments

@alexandr2110pro
Copy link

alexandr2110pro commented Dec 3, 2019

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
Copy link
Author

@kamilmysliwiec

@alexandr2110pro
Copy link
Author

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
Copy link
Author

alexandr2110pro commented Dec 3, 2019

Just found a related discussion

apollographql/apollo-server#2277

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

@kamilmysliwiec
Copy link
Member

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
Copy link

lock bot commented Apr 25, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
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