#Split Nest testing e2e?

7 messages · Page 1 of 1 (latest)

umbral zodiac
#

Let say I have test file like bellow

test.spec.ts

import { INestApplication } from '@nestjs/common'
import * as request from 'supertest'
import server from './src/main' //server is bootstrap()
let app: INestApplication

describe('Application', () => {
  beforeAll(async () => {
    app = await server
  })

  it('Application should be defined', async () => {
    expect(app).toBeDefined()

  })

  it('World module', async () => {
    request(app.getHttpServer())
      .get('/api/worlds')
      .expect(200)
  })

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

when I run test, everything works fine but when I split World endpoints to new file like

world.module.spec.ts

import { INestApplication } from '@nestjs/common'
import * as request from 'supertest'
export default function TestWorldModule(app: INestApplication) {
  it('/api/worlds', () => {
    return request(app.getHttpServer()) //Throw error here app is undefined
      .get('/api/worlds')
      .expect(200)
  })
}

and Import TestWorldModule in test.spec.ts

describe('Application', () => {
  beforeAll(async () => {
    app = await server
  })

  it('Application should be defined', async () => {
    expect(app).toBeDefined()

  })

  TestWorldModule(app)

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

It throws an error app is undefined. 

Where did I do wrong?
clever ivy
#

I am not expert here but this await server looks suspicious to me.

I have this for my e2e tests:

  beforeEach(async () => {
    app = await setupApp();

    await app.init();
  });
export const setupApp = async () => {
  const moduleFixture: TestingModule = await Test.createTestingModule({
    imports: [AppModule],
  }).compile();

  const app = moduleFixture.createNestApplication();

  app.useGlobalFilters(new DatabaseExceptionFilter());

  app.enableVersioning({
    defaultVersion: '1',
    type: VersioningType.URI,
  });

  return app;
};
umbral zodiac
#

@clever ivy
await server is boostrap function, I want to use bootstrap for init server in unit test

import { ValidationPipe } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import 'config'
import 'nestjs-common'
import { AppModule } from './modules/app.module'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  app.setGlobalPrefix(global.Config.APP_CONTEXT)
  app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }))
  app.enableCors()

  await app.listen(global.Config.PORT)
  return app
}
const server = bootstrap()
export default server

umbral zodiac
clever ivy
#
import { INestApplication } from '@nestjs/common';
import request from 'supertest';

import { cleanUp, setupApp } from 'src/test-utils';

describe('Health', () => {
  let app: INestApplication;

  beforeEach(async () => {
    app = await setupApp();

    await app.init();
  });

  afterEach(async () => {
    await cleanUp(app);
  });

  it('runs successfull healthcheck', () => {
    return request(app.getHttpServer()).get('/v1/health').expect(200);
  });
});
#

I have e2e test file for each module

#

built like that