Hey there,
I wonder if there is a way to use supertest to test the graphQL endpoint.
I did not find a way to expose the http object for test, like supertest wants it : https://www.npmjs.com/package/supertest
Is there a way ? Because with Jest i cannot bind the server app on any port.
For those who may wonder, i used to make it work this way :
async function app(): Promise<GraphQLServer> {
const schema = await getSchema();
const server = new GraphQLServer({ schema });
server.use('/health', healthController);
server.use('/version', versionController);
return server;
}
// and in my tests
const server = (await app).createHttpServer({});
const req: any = request(server)
.post(url)
.set('Accept', 'application/json');
And it work for both express and gaphql server middlewares
@DavidBabel But how does the server know where to serve the graphql endpoint from?
I tried
(await app).createHttpServer({ endpoint: '/graphql' })
But the supertest request cannot find anything at /graphql.
Here is what we use as utils (note this is typescript, but can be easily adapted) :
import * as request from 'supertest';
import app from '../app';
interface IRequestResponse {
errors: any[];
data: object;
}
export async function getAPIResponse(
query?: string
): Promise<IRequestResponse> {
const server = (await app).createHttpServer({});
const res = await request(server)
.post('/')
.set('Accept', 'application/json')
.send({ query })
.expect(200)
.expect('Content-Type', /json/);
return res.body;
}
@DavidBabel how to include the validationRules and formatError in supertest? As these options are passed to the start method, is there a way to include them in tests?
I guess you can pass it to createHttpServer directly : https://github.com/prisma/graphql-yoga/blob/766e71b9e0fafb1869f157fc4e408c5902cd4ac4/src/index.ts#L355-L377
@DavidBabel thanks!
Most helpful comment
For those who may wonder, i used to make it work this way :
And it work for both express and gaphql server middlewares