
๊ณต์ ๋ฌธ์ ์ฐธ๊ณ : https://jestjs.io/docs/using-matchers
toBe โ ์์๊ฐ ์ผ์น (===)toBe๋ ์๊ฒฉํ ์ผ์น(===) ๋น๊ต๋ฅผ ์ํํ๋ค.
์ฆ, ๊ฐ๋ฟ ์๋๋ผ ํ์
๊น์ง ๋์ผํด์ผ ํ
์คํธ๊ฐ ํต๊ณผํ๋ค.
test('toBe๋ === ๋น๊ต๋ฅผ ์ฌ์ฉํ๋ค', () => {
expect(2 + 2).toBe(4); // โ
4 === 4
expect('hello').toBe('hello'); // โ
๋ฌธ์์ด ๋์ผ
expect(true).not.toBe(false); // โ
๋ฐ๋๊ฐ์ด๋ฏ๋ก ํต๊ณผ
});
expect({ a: 1 }).toBe({ a: 1 }); // โ ๋ค๋ฅธ ๊ฐ์ฒด
์ด๋ด ๋ toEqual์ ์ฌ์ฉํด์ผ ํ๋ค.
toEqual โ ๊ตฌ์กฐ์ ์ผ์น (deep equality)toEqual์ ๊ฐ์ฒด๋ ๋ฐฐ์ด ๋ด๋ถ์ ๊ฐ๊น์ง ๋น๊ตํ๋ค.
์ฆ, ์ฐธ์กฐ๊ฐ ๋ค๋ฅด๋๋ผ๋ ๊ตฌ์กฐ์ ๊ฐ์ด ๊ฐ์ผ๋ฉด ํต๊ณผํ๋ค.
test('toEqual์ ๊ฐ์ฒด/๋ฐฐ์ด ๋ด๋ถ ๊ฐ ๋น๊ต', () => {
expect({ a: 1, b: 2 }).toEqual({ a: 1, b: 2 });
expect([1, 2, 3]).toEqual([1, 2, 3]);
});
toStrictEqual์ toEqual๋ณด๋ค ๋ ์๊ฒฉํ๊ฒundefined ํ๋กํผํฐ, ํ๋กํ ํ์
์ฐจ์ด๊น์ง ๊ฒ์ฌ)toBeTruthy / toBeFalsy โ Truthiness ํ๋ณJS์์ true๋ก ํ๊ฐ๋๋ ๊ฐ / false๋ก ํ๊ฐ๋๋ ๊ฐ์ ๊ฒ์ฆํ ๋ ์ฌ์ฉํ๋ค.
(Truthy / Falsy ๊ฐ๋
๊ธฐ๋ฐ)
test('Truthy / Falsy ๊ฐ ํ๋ณ', () => {
expect('hello').toBeTruthy(); // โ
๋ฌธ์์ด์ Truthy
expect(1).toBeTruthy();
expect(0).toBeFalsy(); // โ
0์ Falsy
expect(null).toBeFalsy();
});
toBeTruthy()๋ value == true๊ฐ ์๋ toBeNull / toBeUndefined โ ๋ช
ํํ ์ํ ๊ตฌ๋ถ๊ฐ์ด ์ ํํ null ๋๋ undefined์ธ์ง ๊ตฌ๋ถํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค.
test('null / undefined ๋ช
ํํ ๊ตฌ๋ถ', () => {
const data = { name: null };
expect(data.name).toBeNull(); // โ
์ ํํ null
expect(data.age).toBeUndefined(); // โ
์กด์ฌํ์ง ์์
expect(data.name).not.toBeUndefined();
});
toBeNull()์ value === nulltoBeUndefined()๋ value === undefinedtoContain โ ๋ฐฐ์ด ๋๋ ๋ฌธ์์ด ํฌํจ ์ฌ๋ถ๋ฐฐ์ด, ๋ฌธ์์ด, Set ๋ฑ iterable ๊ฐ์ฒด์ ํน์ ์์๊ฐ ํฌํจ๋์ด ์๋์ง ๊ฒ์ฌํ๋ค.
test('๋ฐฐ์ด ๋๋ ๋ฌธ์์ด ํฌํจ ์ฌ๋ถ', () => {
const shoppingList = ['milk', 'bread', 'butter'];
expect(shoppingList).toContain('bread'); // โ
ํฌํจ
expect('Hello Jest').toContain('Jest'); // โ
๋ฌธ์์ด ํฌํจ
expect(['a', 'b', 'c']).not.toContain('d'); // โ
์์
});
toContainEqual์ ์ฌ์ฉํ๋ค.expect([{ id: 1 }]).toContainEqual({ id: 1 });
toThrow โ ์์ธ ๋ฐ์ ๊ฒ์ฆํจ์๊ฐ ์คํ๋ ๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋์ง ํ
์คํธํ๋ค.
๋ฉ์์ง๋ ์๋ฌ ํ์
๊น์ง ๊ฒ์ฆ ๊ฐ๋ฅํ๋ค.
function compileCode() {
throw new Error('syntax error: missing ;');
}
test('compileCode๋ ์๋ฌ๋ฅผ ๋์ ธ์ผ ํ๋ค', () => {
expect(compileCode).toThrow(); // โ
์ด๋ค ์๋ฌ๋ ๋ฐ์ํ๋ฉด ํต๊ณผ
expect(compileCode).toThrow('syntax'); // โ
๋ฉ์์ง ๋ถ๋ถ ์ผ์น
expect(compileCode).toThrow(/missing ;/); // โ
์ ๊ท์ ์ผ์น
expect(compileCode).toThrow(Error); // โ
Error ํ์
expect(compileCode).toThrow(new Error('syntax error: missing ;')); // โ
์์ ์ผ์น
});
function checkPermission(role) {
if (role !== 'admin') throw new TypeError('Access denied');
}
test('checkPermission ํจ์ ํ
์คํธ', () => {
expect(() => checkPermission('user')).toThrow('Access denied'); // โ
๋ฌธ์์ด ํฌํจ
expect(() => checkPermission('user')).toThrow(/Access denied/); // โ
์ ๊ท์
expect(() => checkPermission('user')).toThrow(TypeError); // โ
ํ์
๊ฒ์ฌ
expect(() => checkPermission('user')).toThrow(new TypeError('Access denied')); // โ
ํ์
+๋ฉ์์ง ์ผ์น
expect(() => checkPermission('admin')).not.toThrow(); // โ
์๋ฌ ๋ฐ์ํ์ง ์์์ผ ํจ
});
expect()์๋ ํจ์๋ฅผ ์ง์ ํธ์ถํ์ง ๋ง๊ณ ,expect(fn) expect(() => fn(args))| ๋งค์ฒ | ์ค๋ช | ์์ |
|---|---|---|
toBe | ์์๊ฐ ์ผ์น (===) | expect(2+2).toBe(4) |
toEqual | ๊ตฌ์กฐ์ ์ผ์น | expect({a:1}).toEqual({a:1}) |
toBeTruthy / toBeFalsy | Truthiness ํ๋ณ | expect(value).toBeFalsy() |
toBeNull / toBeUndefined | ๋ช ํํ null/undefined | expect(null).toBeNull() |
toContain | ๋ฐฐ์ด/๋ฌธ์์ด ํฌํจ | expect(arr).toContain(3) |
toThrow | ์์ธ ๋ฐ์ ๊ฒ์ฌ | expect(fn).toThrow() |