如何使用 GRAPHQL 测试 e2e Nestjs API

How Test e2e Nestjs API with GRAPHQL

当我通过 graphql-playground 创建我的所有者时,它工作正常, 但我的测试失败并回复我 'body.data.createOwner is undefined',没有数据。

// owner.e2e.spec.ts
describe('Owner test (e2e)', () => {
    let app: INestApplication;

    beforeAll(async () => {
        const moduleRef = await Test.createTestingModule({
            imports: [
                GraphQLModule.forRoot({
                    autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
                }),
                OwnerModule,
                DatabaseModule
            ]
        }).compile();
        app = moduleRef.createNestApplication();
        await app.init();
    });

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

    const createOwnerQuery = `
           mutation createOwner($OwnerInput: OwnerInput!) {
            createOwner(ownerInput: $OwnerInput) {
              _id
              name
              firstname
              email
              password
              firstsub
              expsub
              createdAt
              updatedAt
            }
          }
    `;
   
    let id: string = '';

    it('createOwner', () => {
        return request(app.getHttpServer())
            .post('/graphql')
            .send({
                operationName: 'createOwner',
                variables: {
                   OwnerInput: {
                        name: 'adar',
                        firstname: 'adar',
                        email: 'adar@test.com',
                        password: 'testing',
                        firstsub: '2020-08-14',
                        expsub: '2020-07-13'
                    }
                },
                query: createOwnerQuery,
            })
            .expect(({ body }) => {
                const data = body.data.createOwner <-- test fail at this line
                id = data._id
                expect(data.name).toBe(owner.name)
                expect(data.email).toBe(owner.email)
                expect(data.firstsub).toBe(owner.firstsub)
            })
            .expect(200)
    })
// Output terminal

 FAIL  test/owner.e2e-spec.ts (9.567 s)
  Owner test (e2e)
    ✕ createOwner (79 ms)

  ● Owner test (e2e) › createOwner

    TypeError: Cannot read property 'createOwner' of undefined

       98 |             })
       99 |             .expect(({ body }) => {
    > 100 |                 const data = body.data.createOwner
          |                                        ^
      101 |                 id = data._id
      102 |                 expect(data.name).toBe(owner.name)
      103 |                 expect(data.email).toBe(owner.email)

      at owner.e2e-spec.ts:100:40
      at Test._assertFunction (../node_modules/supertest/lib/test.js:283:11)
      at Test.assert (../node_modules/supertest/lib/test.js:173:18)
      at Server.localAssert (../node_modules/supertest/lib/test.js:131:12)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        9.645 s, estimated 10 s
Ran all test suites.

我的问题来自 CLI 插件,它自动生成可为空的 Graphql 字段,而没有将 @Field 装饰器放入我的 ObjectType。

nest的作者针对这个问题给出了一个技巧

--> https://github.com/nestjs/graphql/issues/810

但这对我不起作用,所以我只是将“@Field”装饰器添加到我的模型@ObjectType() 中的所有属性。