• Home
  • About
    • Lajancia Workspace photo

      Lajancia Workspace

      .

    • Learn More
    • Email
    • LinkedIn
    • Instagram
    • Tumblr
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects
  • Record
  • Blender
  • Blockchain

Nest.js - 5

06 Jul 2022

Reading time ~1 minute

5. nest.js


package.json

  • test
  • watch
  • cov
  • debug
  • e2e

jest

자바스크립트를 쉽게 테스팅할 수 있도록 도와주는 npm 패키지 (자동으로 세팅해줌)

파일명 중 spec을 포함하는 파일은 테스팅을 포함하는 파일을 말한다.

예) movies.controller.ts 파일을 테스트하고 싶으면, movies.controller.spec.ts라는 파일이 있어야 한다.

test

npm run test:cov

npm run test:watch

unit test

모든 function을 따로 테스트 한다.

it("should be 4", () => {
  expect(2 + 2).toEqual(4);
});

이러한 형태로 테스트를 작성하고 실행할 때 정상적으로 통과되었음을 확인할 수 있다.

MoviesService
    ✓ should be defined (9 ms)
    ✕ should be 4 (3 ms)

  ● MoviesService › should be 4

    expect(received).toEqual(expected) // deep equality

    Expected: 5
    Received: 4

      18 |
      19 |   it("should be 4", () => {
    > 20 |     expect(2+2).toEqual(5)
         |                 ^
      21 |   })
      22 | });

만약 실패하면 어디서 실패했는지도 알려준다.

describe("getAll", () => {
  it("should return array", () => {
    const result = service.getAll();
    expect(result).toBeInstanceOf(Array);
  });
});

getAll을 테스트하는 방법이다. 함수가 실행되었을 때 영화에 대한 배열을 반환하는지에 대하여 확인한다.

describe("getOne", () => {
  it("should return a movie", () => {
    service.create({
      title: "test MOvie",
      genres: ["test"],
      year: 2000,
    });
    const movie = service.getOne(1);
    expect(movie).toBeDefined();
    expect(movie.id).toEqual(1);
  });
});

getOne을 테스트하는 함수

실행하기 전 테스트 데이터를 만들어주는 작업을 수행한다.

it("should throw 404 error", () => {
  try {
    service.getOne(999);
  } catch (e) {
    expect(e).toBeInstanceOf(NotFoundException);
    expect(e.message).toEqual("movie with id 999 not found");
  }
});

404 에러를 테스트할 수 있도록 존재하지 않는 값을 넣었다.

에러 메세지 출력이 일치하는지 확인할 수 있다.

end-to-end 테스트

모든 시스템을 테스팅하는 것

예) getAll function 하나만 테스트하고 싶을 때 사용



studyNest.jstypescript Share Tweet +1