2

我正在尝试在 Nest.js 应用程序中使用 supertest + jest 端到端测试我的 graphql 服务,但不断收到此错误:

错误:无法 POST /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)
4

0 回答 0