
๊ณต์ ๋ฌธ์: https://jestjs.io/docs/expect#reference
expect()๋ ํ
์คํธํ ์ค์ ๊ฐ(actual) ์ ๊ฐ์ธ๋ ํจ์๋ค.
์ด ํจ์๋ ๋ด๋ถ์ ์ผ๋ก Expectation ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
์ด ๊ฐ์ฒด์๋ ์ฌ๋ฌ ๋งค์ฒ(Matcher) ๋ฉ์๋์ ์์ ์(Modifier) ์์ฑ์ด ํฌํจ๋์ด ์๋ค.
Matcher๋ expect()๊ฐ ๋ฐํํ Expectation ๊ฐ์ฒด์ ๋ฉ์๋๋ก,
์ค์ ๊ฐ(actual)๊ณผ ๊ธฐ๋ ๊ฐ(expected)์ ๋น๊ต(Assertion)ํ๋ค.
expect(2 + 2).toBe(4);
expect(2 + 2) โ ์ค์ ๊ฐ(4)์ ๊ฐ์ผ Expectation ๊ฐ์ฒด.toBe(4) โ Matcher ๋ฉ์๋, ์ค์ ๊ฐ๊ณผ ๊ธฐ๋๊ฐ ๋น๊ต ์ํ์กฐ๊ฑด์ด ๋ง์ง ์์ผ๋ฉด Jest๊ฐ ํ ์คํธ๋ฅผ ์คํจ๋ก ์ฒ๋ฆฌํ๋ค.
Modifier(์์ ์) ๋ ๋งค์ฒ์ ๋์์ ๋ฐ๊พธ๋ ๋ณด์กฐ ์์ฑ์ด๋ค.
๋งค์ฒ๋ฅผ ํธ์ถํ๊ธฐ ์ ์ ์ฒด์ธ์ผ๋ก ๋ถ์ฌ์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์์ ํ๋ค.
| Modifier | ์ค๋ช | ์์ |
|---|---|---|
.not | ๋งค์ฒ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋๋ก ๋ค์ง์ | expect(3 + 2).not.toBe(6) |
.resolves | Promise๊ฐ resolve๋ ๋ ๊ฒฐ๊ณผ ๊ฒ์ฆ | await expect(Promise.resolve('ok')).resolves.toBe('ok') |
.rejects | Promise๊ฐ reject๋ ๋ ์๋ฌ ๊ฒ์ฆ | await expect(Promise.reject(new Error('fail'))).rejects.toThrow('fail') |
expect(value)
โ
Expectation ๊ฐ์ฒด
โโโ .toBe() โ Matcher
โโโ .toEqual() โ Matcher
โโโ .toContain() โ Matcher
โโโ .not โ Modifier
โโโ .resolves โ Modifier
โโโ .rejects โ Modifier
์๋ ์์๋ Jest์ ์ค์ ๊ตฌ์กฐ๋ฅผ ๋จ์ํํ ๊ฐ๋
์ ๋ชจ๋ธ์ด๋ค.
๋ด๋ถ์ ์ผ๋ก๋ ํจ์ฌ ๋ณต์กํ์ง๋ง, ์ ์ฒด ํ๋ฆ์ ์๋ ์ฝ๋์ฒ๋ผ ๋์ํ๋ค ๐
function expect(actualValue) {
return {
// โ
๋งค์ฒ๋ค
toBe(expected) {
if (actualValue !== expected) {
throw new Error(`โ Expected ${actualValue} to be ${expected}`);
}
},
toEqual(expected) {
const isEqual = JSON.stringify(actualValue) === JSON.stringify(expected);
if (!isEqual) {
throw new Error(
`โ Expected ${JSON.stringify(actualValue)} to equal ${JSON.stringify(
expected
)}`
);
}
},
toContain(expected) {
if (!actualValue.includes || !actualValue.includes(expected)) {
throw new Error(`โ Expected ${actualValue} to contain ${expected}`);
}
},
// ๐ซ ๋ถ์ Modifier
not: {
toBe(expected) {
if (actualValue === expected) {
throw new Error(`โ Expected ${actualValue} not to be ${expected}`);
}
},
toContain(expected) {
if (actualValue.includes && actualValue.includes(expected)) {
throw new Error(
`โ Expected ${actualValue} not to contain ${expected}`
);
}
},
},
// ๐ ๋น๋๊ธฐ Modifier
resolves: {
async toBe(expected) {
const resolved = await actualValue;
if (resolved !== expected) {
throw new Error(
`โ Expected Promise to resolve to ${expected}, but got ${resolved}`
);
}
},
},
rejects: {
async toThrow(expectedMessage) {
try {
await actualValue;
throw new Error(
`โ Expected Promise to reject, but it resolved successfully`
);
} catch (error) {
if (!error.message.includes(expectedMessage)) {
throw new Error(
`โ Expected rejection message to include "${expectedMessage}", but got "${error.message}"`
);
}
}
},
},
};
}
์ด ์์๋ฅผ ๋ณด๋ฉด ๋ค์ ํ๋ฆ์ด ์์ฐ์ค๋ฝ๊ฒ ๋ณด์ธ๋ค ๐
expect() โ Expectation ๊ฐ์ฒด ์์ฑ.toBe(), .toEqual(), .toContain() โ Matcher ๋ฉ์๋๋ค.not, .resolves, .rejects โ Modifier ์์ฑ๋ค| ๊ฐ๋ | ์ญํ | ํํ | ๋ด๋ถ ๋์ |
|---|---|---|---|
| Expect | ํ ์คํธํ ๊ฐ์ ๊ฐ์ธ๊ณ Expectation ๊ฐ์ฒด ๋ฐํ | ํจ์ | return new Expectation(actual) |
| Matcher | ์ค์ ๊ฐ vs ๊ธฐ๋ ๊ฐ ๋น๊ต | ๋ฉ์๋ | ์คํจ ์ throw Error |
| Modifier | ๋งค์ฒ ๋์ ๋ณ๊ฒฝ (not / resolves / rejects) | ํ๋กํผํฐ | ๋ด๋ถ ํ๋๊ทธ๋ ์ฒด์ธ ๊ตฌ์กฐ๋ก ๋งค์ฒ์ ๊ฒฐ๊ณผ ์์ |
Jest์ ํ ์คํธ ๊ตฌ์กฐ๋
expect() โ (modifier) โ matcher
ํํ์ ์ฒด์ธํ ๊ฒ์ฆ(Assertion Chain) ์ผ๋ก ๊ตฌ์ฑ๋๋ค.