jest๋ ํ์ด์ค๋ถ์์ ๋ง๋ ํ ์คํธํ ๋๊ตฌ์ด๋ค.
zero-config ์ฒ ํ์ ๊ฐ์ง๊ณ ์์ด์ ๋ณ๋์ ์ค์ ์์ด ๋น ๋ฅด๊ฒ ํ ์คํธ ์ผ์ด์ค๋ฅผ ๋ง๋ค ์ ์๋ ๊ฒ์ด ์ฅ์ ์ด๋ค.
jest ์ค์นํ๊ธฐ
npm install jest --save-dev
๊ฐ๋ฐ ํ ๋๋ง ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ค์ --save-dev๋ฅผ ๋ถ์ธ๋ค.
package.json์์ ์์ ํ๋ค.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
๐
"scripts": {
"test": "jest"
},
~.test.js๋ก ๋๋๊ฑฐ๋ __test__ํด๋์ ๋ด๊ฒจ์์ผ๋ฉด ์๋์ผ๋ก ํ ์คํธ ํ์ผ๋ก ์ธ์ํ๋ค!
ํน์ ํ์ผ๋ง ํ ์คํ ํ๊ณ ์ถ์ผ๋ฉด npm test ๋ค์ ํ์ผ๋ช ์ด๋ ํน์ ๊ฒฝ๋ก๋ฅผ ๋ถ์ด๋ฉด ๋๋ค.
//fn.js
const fn = {
add: (num1, num2) => {
num1 + num2;
},
};
module.exports = fn;
// package file์์ ์ฌ์ฉํ ์ ์๋๋ก export ํด์ค๋ค.
//fn.test.js
const fn = require("./fn");
test("1์ 1์ด์ผ", () => {
expect(1).toBe(1);
});
expect ๋ค์ ๊ฒ์ฆํ ๋์์ ๋ฃ์ด์ฃผ๊ณ toBe์ ๊ธฐ๋๋๋ ๊ฐ์ ๋ฃ๋๋ค.
๋ถ์ ์ ๋ํ ํ ์คํธ
test("3 ๋ํ๊ธฐ 3์ 5๊ฐ ์๋์ผ!", () => {
expect(fn.add(3, 3)).not.toBe(5);
});
toBe์์ ์ฌ์ฉํ๋ ํจ์๋ฅผ Matcher๋ผ๊ณ ํ๋ค.
Matcher์๋ toEqual๋ ์๋ค.
๊ฐ์ฒด๋ ๋ฐฐ์ด์ ์ฌ๊ท์ ์ผ๋ก ๋๋ฉด์ ๊ฐ์ ํ์ธํด์ผํ๊ธฐ ๋๋ฌธ์ 'toEqual'์ ์ฌ์ฉํด์ค์ผํ๋ค.
Matcher ์ข ๋ฅ
- toBeNull()
- toBeTruthy()
- toBeFalsy()
- toBeUmdefined
- toBeDefined
- toBeGreaterThan ํฌ๋ค
- toBeGreaterThanEqual ํฌ๊ฑฐ๋ ๊ฐ๋ค
- toBeLessThan ์๋ค
- toBeLessThanOrEqual ์๊ฑฐ๋ ๊ฐ๋ค
- toBeCloseTo() ๊ทผ์ฌ๊ฐ์ธ์ง ํ๋ณ
- toMatch(์ ๊ทํํ์) ํน์ ๋ฌธ์์ด์ด ์๋์ง ํ๋ณ
- toContain()
๋ฐฐ์ด์์ ํน์ ์์๊ฐ ์๋์ง ํ๋ณ
- ํน์ ์๋ฌ์ธ์ง ํ์ธ
const fn = {
add: (num1, num2) => num1 + num2,
//๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ฃผ๋ ํจ์
makeUser: (name, age) => ({ name, age }),
//ํน์ ์๋ฌ์ธ์ง ํ์ธํ๋ ํจ์
throwErr: () => {
throw new Error("xx");
},
};
module.exports = fn;
// package file์์ ์ฌ์ฉํ ์ ์๋๋ก export ํด์ค๋ค.
test("์๋ฌ๊ฐ ๋ฐ์ํ๋์?", () => {
expect(() => fn.throwErr()).toThrow("์๋ฌ๋ฐ์!!!");
});
๋ ๋ง์ Matcher๋ ๊ณต์ ํํ์ด์ง์์ ํ์ธํ๊ธฐ!
https://jestjs.io/docs/using-matchers
Using Matchers · Jest
Jest uses "matchers" to let you test values in different ways. This document will introduce some commonly used matchers. For the full list, see the expect API doc.
jestjs.io
๋น๋๊ธฐ jest
const fn = {
add: (num1, num2) => num1 + num2,
//๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ฃผ๋ ํจ์
makeUser: (name, age) => ({ name, age }),
getName: (callback) => {
const name = "Hailie";
setTimeout(() => {
callback(name);
// throw new Error("error");
}, 3000);
},
getAge: () => {
const age = 30;
return new Promise((res, rej) => {
setTimeout(() => {
res(age);
// rej("err");
}, 3000);
});
},
};
module.exports = fn;
// package file์์ ์ฌ์ฉํ ์ ์๋๋ก export ํด์ค๋ค.
test("3์ด ํ์ ๋ฐ๋ ์ด๋ฆ์ Hailie", (done) => {
function callback(name) {
expect(name).toBe("Hailie");
done();
}
fn.getName(callback);
});
// ๋ง์ฝ api์ ์๋ฌ๋ฅผ ๊ฐ์งํ๊ณ ์ถ๋ค๋ฉด try catch๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
test("3์ด ํ์ ๋ฐ๋ ์ด๋ฆ์ Hailie", (done) => {
function callback(name) {
try {
expect(name).toBe("Hailie");
done();
} catch (error) {
done();
}
}
fn.getName(callback);
});
// promise๋ done์ ์๋๊ฒจ์ค๋ ๋๋ค. ๋์ return์ ํด์ค์ผํ๋ค.
// test("3์ด ํ์ ๋ฐ์ ๋์ด๋ 30", () => {
// return fn.getAge().then((age) => {
// expect(age).toBe(30);
// });
// });
//๊ฐ๋จํ๊ฒ ์ฌ์ฉํ๊ธฐ resolves, rejects
test("3์ด ํ์ ๋ฐ์ ๋์ด๋ 30", () => {
return expect(fn.getAge()).resolves.toBe(30);
});
//๊ฐ๋จํ๊ฒ ์ฌ์ฉํ๊ธฐ resolves, rejects
test("3์ด ํ์ ๋ฐ์ ๋์ด๋ 30", async () => {
// const age = await fn.getAge();
// expect(age).toBe(30);
await expect(fn.getAge()).resolves.toBe(30);
});
ํ ์คํธ ์ง์ ์ ์์ ์ด๊ธฐํ ํ๊ธฐ (๋ชจ๋ ํ ์คํธ ์ง์ ์ ์คํ๋จ)
beforeEach(() => {
num = 0;
});
ํ ์คํธ ์งํ์ ์์ ์ด๊ธฐํ ํ๊ธฐ (๋ชจ๋ ํ ์คํธ ์งํ์ ์คํ๋จ)
afterEach(() => {
num = 0;
});
'๐ฅFrontEnd' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
jest ์ฉ๋๋ณ ๋งค์ฒ ์์๋ณด๊ธฐ (0) | 2024.09.21 |
---|---|
Jest ๊ธฐ๋ณธ ๊ณต๋ถํ๊ธฐ, ์ฝ๊ฒ Jest๋ก ํ ์คํธํ๋ ์ต์คํ ์ ์ถ์ฒ (0) | 2024.09.09 |
IntersectionObserver ๋ก ์คํฌ๋กค ์ด๋ฒคํธ (0) | 2024.08.28 |
์น ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํ ๋๊ตฌ ์ถ์ฒ (0) | 2024.07.27 |
jest, beforeAll, afterAll (0) | 2023.05.01 |