Testing 如何使用GRAPHQL测试e2e Nestjs API

Testing 如何使用GRAPHQL测试e2e Nestjs API,testing,graphql,nestjs,e2e-testing,graphql-mutation,Testing,Graphql,Nestjs,E2e Testing,Graphql Mutation,当我通过graphql游乐场创建我的所有者时,效果很好, 但我的测试失败,并回答我“body.data.createOwner未定义”,没有数据 // owner.e2e.spec.ts describe('Owner test (e2e)', () => { let app: INestApplication; beforeAll(async () => { const moduleRef = await Test.createTestingMod

当我通过graphql游乐场创建我的所有者时,效果很好, 但我的测试失败,并回答我“body.data.createOwner未定义”,没有数据

// 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)
    })

我的问题来自CLI插件,该插件自动生成可为空的Graphql字段,而无需在我的ObjectType中添加@Field decorator

鸟巢的创造者就这个问题开了个玩笑

-->


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

所有者是一个模块还是整个应用程序?它是一个模块(也是我的实体)。请尝试只导入你的
AppModule
,它将包含正常运行应用程序所需的所有设置。
// 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.