如何用GraphQl端到头测试Nestjs?

发布于 2025-02-06 07:55:04 字数 1677 浏览 2 评论 0原文

在测试/帖子/posts.e2e-spec.ts文件中,

import { INestApplication } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Test, TestingModule } from '@nestjs/testing';
import request = require('supertest');
import { PostsModule } from '../../src/posts/posts.module';

describe('Posts (e2e)', () => {
  const posts = {
    id: 1,
    name: 'FirstPost #1',
  };

  let app: INestApplication;

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [
        TypeOrmModule.forRoot({
          type: 'mysql',
          ...
        }),
        PostModule,
      ],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });

  afterAll(async () => {
    await app.close();
  });

  describe('post', () => {
    it('should retrieve all post data', async () => {
      request(app.getHttpServer())
      .post('/graphql')
      .send({
        query:
          `{findPosts() {
            name
          }}`,
      })
      .expect(200)
      .expect((res) => {
        console.log(res.body.data)
        expect(res.body.data.post.length).toEqual(posts.length)
      })
    })
  })
});

我创建的迁移并首先将数据插入数据库,然后运行此测试,它不能转到Expect项目。即使设置了控制台日志,我在输出中看不到任何内容。

因此,也许/graphQl无法以这种方式访问​​?我可以以http:// localhost:3000/graphql访问端点。


如果导入supertest如

import * as request from 'supertest';

行请求中所示:

此表达不可呼应。类型的“ type of supertest”没有呼叫签名。

它们的版本:

  • supertest:6.1.3
  • @types/supertest:2.0.11

In the test/posts/posts.e2e-spec.ts file

import { INestApplication } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Test, TestingModule } from '@nestjs/testing';
import request = require('supertest');
import { PostsModule } from '../../src/posts/posts.module';

describe('Posts (e2e)', () => {
  const posts = {
    id: 1,
    name: 'FirstPost #1',
  };

  let app: INestApplication;

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [
        TypeOrmModule.forRoot({
          type: 'mysql',
          ...
        }),
        PostModule,
      ],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });

  afterAll(async () => {
    await app.close();
  });

  describe('post', () => {
    it('should retrieve all post data', async () => {
      request(app.getHttpServer())
      .post('/graphql')
      .send({
        query:
          `{findPosts() {
            name
          }}`,
      })
      .expect(200)
      .expect((res) => {
        console.log(res.body.data)
        expect(res.body.data.post.length).toEqual(posts.length)
      })
    })
  })
});

I created migration and inserted data into database first, then run this test, it can't go to the expect items. Even set console log I can't see anything in the output.

So maybe the /graphql can't be access in this way? I can access the endpoint from browser as http://localhost:3000/graphql.


If import supertest as

import * as request from 'supertest';

In the line request it showed:

This expression is not callable. Type ‘typeof supertest’ has no call signatures.

The version of them:

  • supertest: 6.1.3
  • @types/supertest: 2.0.11

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2025-02-13 07:55:04

查看此非常有用的链接 https://github.com/ JMCDO29/testing-nestjs/tree/main/apps/graphql-sample 。它解释了许多有关测试的事情,包括GraphQl Nestjs测试以及示例应用程序

Check out this very useful link https://github.com/jmcdo29/testing-nestjs/tree/main/apps/graphql-sample. It explains a lot of things regarding tests including graphql nestjs testing along with sample application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文