
์ฐธ๊ณ : Expect Reference
expect(value).toEqual(expected)์ฒ๋ผ ์ผ์ชฝ(actual)์ ๊ฒ์ฌํ๋ค.expect(getUser()).toEqual({
id: expect.any(Number), // โ ๊ฐ ๋์ ์กฐ๊ฑด(ํ์
)
name: expect.any(String),
email: expect.stringMatching(/@/), // โ ํจํด(์ ๊ท์)
});
์ฅ์ : โ๊ฐ์ด ์ ํํ X์ฌ์ผ ํ๋คโ ๋์ โ์ด๋ฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด OKโ ํํ๋ก ์๋๋ฅผ ๋๋ฌ๋ผ ์ ์๋ค.
expect.any(constructor)expect.anything()expect.stringContaining(substr)expect.stringMatching(regexOrString)expect.any(constructor) โ ํด๋น ํ์
์ด๋ฉด ํต๊ณผtest('ํ์
๋ง ๋ณด์ฅ', () => {
const user = { id: 42, name: 'Kim', createdAt: new Date() };
expect(user).toEqual({
id: expect.any(Number),
name: expect.any(String),
createdAt: expect.any(Date),
});
});
// ๋ฐฐ์ด ๋ด์์๋ ์ฌ์ฉ ๊ฐ๋ฅ (๋ถ๋ถ์ผ์น ์๋: ์ ์ฒด ๊ตฌ์กฐ๊ฐ ์ผ์นํด์ผ ํจ)
expect([123, 'abc']).toEqual([expect.any(Number), expect.any(String)]);
๋น๊ต ํฌ์ธํธ:
typeof value === 'number'๋ฅผ ํ ์คํธ ๋ณธ๋ฌธ์์ ๊ตณ์ด ์ฐ์ง ์๊ณ , ๊ฒ์ฆ ๋ฌธ์ฅ์ ๋ฐ์ดํฐ ๋ชจ์ ์์ ๋ถ์ฌ ๊ฐ๋ ์ฑ์ ๋์ธ๋ค.
expect.anything() โ null/undefined ์ ์ธํ ์์์ ๊ฐ์ด๋ฉด ํต๊ณผtest('์กด์ฌ์ฑ๋ง ํ์ธ', () => {
const res = { token: 'abc123', payload: { /* ... */ } };
expect(res).toEqual({
token: expect.anything(), // null/undefined๋ง ์๋๋ฉด OK
payload: expect.any(Object)
});
});
ํ: ๋คํธ์ํฌ ์๋ต์
id,token,requestId์ฒ๋ผ ์๊ธฐ๋ง ํ๋ฉด ๋๋ ํ๋์ ์ ํฉ.
expect.stringContaining(substr) โ ๋ฌธ์์ด์ ๋ถ๋ถ ๋ฌธ์์ด ํฌํจtest('๋ถ๋ถ ๋ฌธ์์ด ํฌํจ', () => {
const msg = 'Hello, Jest and Testing Library!';
expect(msg).toEqual(expect.stringContaining('Jest'));
expect(msg).not.toEqual(expect.stringContaining('Mocha'));
});
์ธ์ ์ฐ๋: ๋ผ๋ฒจ/๋ฉ์์ง/๋ก๊ทธ ๋ผ์ธ์ฒ๋ผ โ์ด ๋จ์ด๋ง ๋ค์ด๊ฐ๋ฉด ์ถฉ๋ถโํ ๊ฒ์ฆ.
expect.stringMatching(regexOrString) โ ๋ฌธ์์ด์ด ํจํด์ ๋งค์นญstringContaining๋ณด๋ค ํํ๋ ฅ์ด ๋ ํ๋ถtest('์ ๊ท์ ํจํด ์ผ์น', () => {
const code = 'user-001';
expect(code).toEqual(expect.stringMatching(/^user-\d+$/));
const email = 'dev@ex.com';
expect(email).toEqual(expect.stringMatching(/.+@.+\..+/));
});
์ธ์ ์ฐ๋: ์์ด๋ ํฌ๋งท, ๋ฒ์ ๋ฌธ์์ด, ๋ ์ง/์๊ฐ ํฌ๋งท ๋ฑ ํ์์ ๊ฒ์ฆํ ๋.
const makeOrder = () => ({
orderId: 'ORD-2025-0001',
buyer: { id: 7, name: 'Lee' },
items: [
{ sku: 'A-01', qty: 2 },
{ sku: 'B-99', qty: 1 },
],
createdAt: new Date().toISOString(),
});
test('์ฃผ๋ฌธ ๊ฐ์ฒด์ ํ์
/ํจํด๋ง ๋ณด์ฅ (๋ถ๋ถ์ผ์น ๋ฏธ์ฌ์ฉ)', () => {
const order = makeOrder();
expect(order).toEqual({
orderId: expect.stringMatching(/^ORD-\d{4}-\d{4}$/),
buyer: {
id: expect.any(Number),
name: expect.any(String),
},
items: [
{ sku: expect.any(String), qty: expect.any(Number) },
{ sku: expect.any(String), qty: expect.any(Number) },
],
createdAt: expect.any(String), // ISO ๋ฌธ์์ด์ด๋ผ๊ณ ๊ฐ์ (๋ ์๊ฒฉํ ํ๋ ค๋ฉด ์ ๊ท์ ์ฌ์ฉ)
});
});
ํฌ์ธํธ: ์ ์ฒด ๊ตฌ์กฐ๋ ๊ทธ๋๋ก ๋๊ณ , ๋ด๋ถ ๊ฐ๋ค๋ง ํ์ /ํจํด์ผ๋ก ์ ์ฐํ๊ฒ ํํ.
๋ฐฐ์ด๋ ์ ํํ ๊ทธ ๊ธธ์ด/์์๋ฅผ ๊ธฐ๋ํ๋ค๋ฉด ์ด๋ ๊ฒ ์ฐ๋ฉด ๋๋ค(๋ถ๋ถ์ผ์น ์๋).
expect.any(Number)๋ ํ์
๋ง ๋ณธ๋ค. ๊ตฌ๊ฐ/๋ฒ์ ๊ฒ์ฌ๋ toBeGreaterThan/toBeCloseTo ๋ฑ ์ซ์ ๋งค์ฒ๋ฅผ ํ์ฉ.expect.anything()์ null/undefined๋ง ๊ฑฐ๋ฅธ๋ค. ๋น ๋ฌธ์์ด ''์ด๋ 0์ ํ์ฉ๋๋ค๋ ์ ์ฃผ์.stringContaining/stringMatching์ ์ฐํญ์๋ง ์ด๋ค. ์ฆ, toEqual(์ฌ๊ธฐ)์ expected ์ชฝ.| ๋งค์ฒ | ํต์ฌ | ์์ |
|---|---|---|
expect.any(Ctor) | ํ์ ๋ง ๋ณด์ฅ | { id: expect.any(Number) } |
expect.anything() | ์กด์ฌ์ฑ๋ง ๋ณด์ฅ(null/undefined ์ ์ธ) | { token: expect.anything() } |
expect.stringContaining(substr) | ๋ถ๋ถ ๋ฌธ์์ด ํฌํจ | expect(str).toEqual(expect.stringContaining('Jest')) |
expect.stringMatching(re) | ์ ๊ท์/๋ฌธ์์ด ํจํด ์ผ์น | expect(code).toEqual(expect.stringMatching(/^ID-\d+$/)) |