Search code examples
testinggraphqlnestjse2e-testinggraphql-mutation

How Test e2e Nestjs API with GRAPHQL


When I create my Owner via graphql-playground it works fine, but my test fail and response me that 'body.data.createOwner is undefined', there no data.

// 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: '[email protected]',
                        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.

Solution

  • My problem was from CLI plugin which generate automatically nullable Graphql fields without putting the @Field decorator in my ObjectType.

    The creator of nest gave a trick about this issue.

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

    but this doesn't works for me, so I simply added "@Field" decorator to all attributs in my model @ObjectType().