제목: What is the difference between describe and it in Jest?
질문 날짜: 2022-09-29
태그: #jest
관련 글: [[Making Test Code at Controller layer]] [[Making GetByAuther Test Code (Moking)]][[Nest js Create 테스트코드]]
질문 내용
https://stackoverflow.com/questions/32055287/what-is-the-difference-between-describe-and-it-in-jest
질문 답변 (해결 방안)
describe
breaks your test suite into components. Depending on your test strategy, you might have a describe for each function in your class, each module of your plugin, or each user-facing piece of functionality.
You can also nest describes to further subdivide the suite.
it
is where you perform individual tests. You should be able to describe each test like a little sentence, such as "it calculates the area when the radius is set". You shouldn't be able to subdivide tests further-- if you feel like you need to, use describe instead.
describe('Circle class', function() {
describe('area is calculated when', function() {
it('sets the radius', function() { ... });
it('sets the diameter', function() { ... });
it('sets the circumference', function() { ... });
});
});
As the jest docs says, test and it are the same: https://jestjs.io/docs/en/api#testname-fn-timeout
test(name, fn, timeout)
Also under the alias: it(name, fn, timeout)
and describe is just for when you prefer your tests to be organized into groups: https://jestjs.io/docs/en/api#describename-fn
describe(name, fn)
describe(name, fn)
creates a block that groups together several related tests.
For example, if you have a myBeverage
object that is supposed to be delicious but not sour, you could test it with:
const myBeverage = {
delicious: true,
sour: false,
};
describe('my beverage', () => {
test('is delicious', () => {
expect(myBeverage.delicious).toBeTruthy();
});
test('is not sour', () => {
expect(myBeverage.sour).toBeFalsy();
});
});
This isn't required - you can write the test blocks directly at the top level. But this can be handy if you prefer your tests to be organized into groups.
'궁금증 해결 > 개념' 카테고리의 다른 글
Schema와 Table 차이 (mySQL , PostgreSQL) (0) | 2022.12.20 |
---|---|
QnA [Java] 스택 자료구조 쓸 때 뭘 써야하는가 (0) | 2022.12.19 |
QnA [Java] Interface Naming convention (0) | 2022.12.19 |
spyOn()과 jest.fn()의 차이 (1) | 2022.12.05 |
타입스크립트 Interface, Type의 차이와 관련 연산자 (타입 합칠 때 & 사용) (0) | 2022.12.03 |