@orderlycode/nest-test-tools
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

nest-test-tools

  1. 모듈을 테스트 할때는 moduleTest 를 사용한다.
describe("test", () => {
  const test = moduleTest(AppModule)
  // 여기서 바로 사용 가능
  // 내부적으로 automock을 사용한다.
  const dependencyService = context.get(DependencyService);
  // 주의 dependencyService는 모두 beforeEach후에 초기화 된다.
})
  1. 유닛 테스트 할때는 unitTest 를 사용한다.
describe("test", () => {
  // 테스트마다 다시 setup된다. (beforeEach)
  const test = moduleTest(AppModule)
  // 여기서 바로 사용 가능, constructor에 있는 것만 쓸 수 있다.
  // 내부적으로 automock을 사용한다.
  const service = context.unit;
  const dependencyService = context.get(DependencyService);
  // 주의 service, dependencyService는 모두 beforeEach후에 초기화 된다.
})
  1. mocking이 필요하다면 가져다 쓴다.
// <jestRootDir>/__mocks__/bcryptjs.ts
export * from '@orderlycode/nest-test-tools/dist/mocks/bcryptjs';
// <jestRootDir>/__mocks__/@nestjs/typeorm.ts
export * from '@orderlycode/nest-test-tools/dist/mocks/@nestjs/typeorm';

테스트 도구

jest setupAfterEnv 파일 내에 아래 내용을 추가한다.

import '@orderlycode/nest-test-tools/dist/register';

이것은 .env.test 파일을 로드하고, nock을 통해 네트워크를 차단시키며 (supertest를 위한 127.0.0.1은 허용), context를 삽입한다.

nock 네트워크 활용

네트워크 데이터 수집 (테스트가 들어있는 폴더의 _recorded폴더에 저장됨)

import {record} from '@orderlycode/nest-test-tools';

describe('test', () => {
  record('test', () => {
    // 네트워크 콜
  })
})

수집된 데이터 사용하여 테스트

import {recorded} from '@orderlycode/nest-test-tools';

describe('test', () => {
  recorded('test', () => {
    // 네트워크 콜
  })
})

Readme

Keywords

none

Package Sidebar

Install

npm i @orderlycode/nest-test-tools

Weekly Downloads

22

Version

0.1.1

License

ISC

Unpacked Size

44.2 kB

Total Files

33

Last publish

Collaborators

  • inithink