#jestjs #graphql #nestjs #supertest #graphql-federation
Вопрос:
Я пытаюсь полностью протестировать свою службу graphql, используя supertest jest в Nest.js приложение, но продолжайте получать эту ошибку:
Ошибка: не удается опубликовать /graphql (404)
Я много чего перепробовал, но не смог запустить тест полностью из-за этой ошибки.
Ниже приведен мой тестовый файл:
import request from 'supertest';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { GatewayAppModule } from '../../../app-gateway.module';
import { AppModule } from '../../../app.module';
import { CreateAccountArgs } from '../dto/create-account.args';
jest.mock('graphql-moment', () => ({
GraphQLDate: jest.fn(),
}));
describe('Auth resolver: Authentication module e2e test', () => {
let mainApp: INestApplication;
let gatewayApp: INestApplication;
beforeAll(async () => {
const mainAppModuleRef: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
mainApp = mainAppModuleRef.createNestApplication();
await mainApp.init();
const gatewayModuleRef: TestingModule = await Test.createTestingModule({
imports: [GatewayAppModule]
}).compile();
gatewayApp = gatewayModuleRef.createNestApplication();
await gatewayApp.init();
});
afterAll(async () => {
jest.clearAllMocks();
await mainApp.close();
await gatewayApp.close();
});
describe('registerAccount mutation: Create a new account', () => {
const mockUser: CreateAccountArgs = {
firstName: 'TestUserFirstName',
lastName: 'TestUserLastName',
email: "TestUser@email.com",
password: 'TestUserPassword',
};
it('should register a new user account with correct user information', async () => {
const response = await request(mainApp.getHttpServer())
.post('/graphql')
.send({
query:
`mutation {registerAccount(registerAccount: ${mockUser}) {accessToken}}`,
})
console.log(response.error, '==========================>')
expect(response.status).toBe(201);
});
});
});
Ниже приведена ошибка, которую я получаю после входа в консоль:
Expected: 201
Received: 404
53 | })
54 | console.log(response.error, '==========================>')
> 55 | expect(response.status).toBe(201);
| ^
56 | });
57 | });
58 | });
at Object.<anonymous> (src/modules/auth/tests/auth.e2e.spec.ts:55:31)
console.log
Error: cannot POST /graphql (404)
at Response.toError (/Users/michaelowolabi/Desktop/YoungM/sz/sz-graphql-api/node_modules/superagent/src/node/response.js:95:15)
at Response._setStatusProperties (/Users/michaelowolabi/Desktop/YoungM/sz/sz-graphql-api/node_modules/superagent/src/response-base.js:126:48)
at new Response (/Users/michaelowolabi/Desktop/YoungM/sz/sz-graphql-api/node_modules/superagent/src/node/response.js:41:8)
at Test._emitResponse (/Users/michaelowolabi/Desktop/YoungM/sz/sz-graphql-api/node_modules/superagent/src/node/index.js:928:20)
at IncomingMessage.<anonymous> (/Users/michaelowolabi/Desktop/YoungM/sz/sz-graphql-api/node_modules/superagent/src/node/index.js:1130:38)
at IncomingMessage.emit (events.js:327:22)
at IncomingMessage.EventEmitter.emit (domain.js:482:12)
at endReadableNT (_stream_readable.js:1221:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
status: 404,
text: '<!DOCTYPE html>n'
'<html lang="en">n'
'<head>n'
'<meta charset="utf-8">n'
'<title>Error</title>n'
'</head>n'
'<body>n'
'<pre>Cannot POST /graphql</pre>n'
'</body>n'
'</html>n',
method: 'POST',
path: '/graphql'
} ==========================>
at Object.<anonymous> (src/modules/auth/tests/auth.e2e.spec.ts:54:15)