본문 바로가기
개발공부/NestJS

[노마드 코더 NestJS로 API 만들기] 3 Testing E2E

by bzerome240 2023. 8. 20.

 

npm run test:e2e

 

테스팅 어플리케이션도 실제 어플리케이션처럼 설정하기 - main.ts의 pipe

beforeAll(async () => {
  const moduleFixture: TestingModule = await Test.createTestingModule({
    imports: [AppModule],
  }).compile();
  
  app = moduleFixture.createNestApplication();
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      forbidNonWhitelisted: true,
      transform: true,
    }),
  );
  await app.init();
});

 

 app.e2e-spec.ts - 유저가 사용할 수 있는 기능을 e2e test 하기 

it('/ (GET)', () => {
  return request(app.getHttpServer())
    .get('/')
    .expect(200)
    .expect('welcome to my movie api');
});

describe('/movies', () => {
  it('GET', () => {
    return request(app.getHttpServer())
      .get('/movies')
      .expect(200)
      .expect([]);
  });
  
  it('POST 201', () => {
    return request(app.getHttpServer())
      .post('/movies')
      .send({
        title: 'Test',
        year: 2000,
        genres: ['test'],
      })
      .expect(201); 
  });  
  
  it('POST 400', () => {
    return request(app.getHttpServer())
      .post('/movies')
      .send({
        title: 'Test',
        year: 2000,
        genres: ['test'],
        other: 'thing',
      })
      .expect(400); 
  });  
  
  it('DELETE', () => {
    return request(app.getHttpServer()).
      .delete('/movies')
      .expect(404); 
  });  
});

 

describe('/movies/:id', () => {
  it('GET 200', () => {
    return request(app.getHttpServer())
      .get('/movies/1')
      .expect(200);
  });
  
  it('GET 404', () => {
    return request(app.getHttpServer())
      .get('/movies/999')
      .expect(404);
  });
  
  it('PATCH 200', () => {
    return request(app.getHttpServer())
      .patch('/movies/1')
      .send( { title: 'updated test' })
      .expect(200);
  });
  
  it('DELETE 200', () => {
    return request(app.getHttpServer())
      .delete('/movies/1')
      .expect(200);
  });

});
728x90
반응형

댓글