[Vue.js] 단위 테스트 - Jest Globals

2022. 4. 12. 21:18공부/Vue.js

단위 테스트를 하게되면 jest만의 함수인 test()등을 사용한다.

이것들은 전부 Jest의 Globals에 정의된 함수들이다.

참조) Globals · Jest (jestjs.io)

 

Globals · Jest

In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'.

jestjs.io

수많은 Methods가 있지만, 큰 틀은 몇 개 안 된다.

afterAll, afterEach, beforeAll, beforeEach, describe, test 정도가 중요하고, 나머지는 여기서 확장된 개념.

 

그룹화

describe()

여러개의 테스트 케이스를 그룹화 시켜주는 함수.

before & after

그룹을 사용하면 before나 after 함수를 사용할 수 있다.

beforeAll / afterAll : 모든 테스트가 시작하기 전 / 후 에 단 한 번 실행된다.

beforeEach / afterEach : 각 테스트가 시작하기 전 / 후 에 실행된다.

이런식으로 일종의 테스트 라이프사이클로 활용할 수 있다.