๋ฐ์ํ
const fn = {
add: (num1, num2) => num1 + num2,
connectUserDB: () => {
return new Promise((res) => {
setTimeout(() => {
res({
name: "Hailie",
age: 30,
gender: "female",
});
}, 500);
});
},
disconnectDB: () => {
return new Promise((res) => {
setTimeout(() => {
res();
}, 500);
});
},
};
module.exports = fn;
const fn = require("./fn");
let user;
beforeEach(async () => {
user = await fn.connectUserDB();
console.log(user);
});
afterEach(() => {
return fn.disconnectDB();
});
test("์ด๋ฆ์ Hailie", () => {
expect(user.name).toBe("Hailie");
});
test("๋์ด๋ 30", () => {
expect(user.age).toBe(30);
});
test("์ฑ์ female", () => {
expect(user.gender).toBe("female");
});
npm run test๋ก test๋ฅผ ์คํ์ํค๋ฉด ๊ฐ ํ ์คํธ๋ง๋ค ์ฝ 1์ด์ ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค.
์๋๋ฉด ๊ฐ๊ฐ์ ํ ์คํธ ์ ํ์ 0.5์ด์ฉ ์์ ์ด ์๊ธฐ ๋๋ฌธ์ด๋ค.
ํ์ง๋ง ์ค์ ๋ก๋ DB์์ ํ๋ฒ์ ๊ฐ์ ธ์ค๊ธฐ ๋๋ฌธ์ ํ ์คํธ ์ , ํ๋ง๋ค ์๊ฐ์ด ์ค๋๊ฑธ๋ฆด ํ์๊ฐ ์๋ค.
์ด๋ ์ฌ์ฉํ๋ ๊ฒ์ด beforeEach, afterEach๊ฐ ์๋ beforeAll, afterAll ์ด๋ค.
const fn = require("./fn");
let user;
beforeAll(async () => {
user = await fn.connectUserDB();
console.log(user);
});
afterAll(() => {
return fn.disconnectDB();
});
test("์ด๋ฆ์ Hailie", () => {
expect(user.name).toBe("Hailie");
});
test("๋์ด๋ 30", () => {
expect(user.age).toBe(30);
});
test("์ฑ์ female", () => {
expect(user.gender).toBe("female");
});
๋ฐ์ํ
'๐ฅFrontEnd' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
jest ์ฉ๋๋ณ ๋งค์ฒ ์์๋ณด๊ธฐ (0) | 2024.09.21 |
---|---|
Jest ๊ธฐ๋ณธ ๊ณต๋ถํ๊ธฐ, ์ฝ๊ฒ Jest๋ก ํ ์คํธํ๋ ์ต์คํ ์ ์ถ์ฒ (0) | 2024.09.09 |
IntersectionObserver ๋ก ์คํฌ๋กค ์ด๋ฒคํธ (0) | 2024.08.28 |
์น ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํ ๋๊ตฌ ์ถ์ฒ (0) | 2024.07.27 |
jest ๊ธฐ๋ณธ ๊ณต๋ถ (0) | 2023.04.28 |