mercredi 8 juillet 2020

How Test e2e Nestjs API with GRAPHQL

When I create my Owner via graphql-playground it works fine, [enter image description here][1]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: '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.

Aucun commentaire:

Enregistrer un commentaire