Jest是一款由Facebook所開發出的 JavaScript測試框架,專門用於測試React應用程式、React Native應用程式以及任何其他的JavaScript專案。Jest具有易上手且功能豐富的特點,它不需要額外的配置文件即可開始使用,並且支持快照測試、Mock、斷言、覆蓋率等測試類型。
以下是一個簡單的Jest測試案例:
// sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
// sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
在這個案例中,我們首先定義了一個簡單的sum函數,接著建立了一個測試檔案sum.test.js,在這個測試檔案中,我們使用了Jest提供的test函數來描述這個測試案例。在這個測試案例中,我們期望sum(1, 2)的結果會等於3,這個期望值透過Jest提供的斷言函數expect和匹配器toBe來實現。
執行Jest測試需要在終端機中輸入以下指令:
npm test
執行測試之後,Jest將會輸出以下結果:
PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)
Jest指示測試通過,並且提供了測試的描述以及耗時。這個簡單的測試案例展示了Jest在JavaScript專案中的使用方式。
function add(a, b) {
return a + b;
}
答案:
test("add function works correctly", () => {
expect(add(2, 2)).toBe(4);
expect(add(0, 4)).toBe(4);
expect(add(-2, 3)).toBe(1);
expect(add(1.2, 2.3)).toBeCloseTo(3.5);
});
function calculateTotalPrice(price, quantity) {
const total = price * quantity;
return `Total price is ${total}`;
}
答案:
test("calculateTotalPrice function works correctly", () => {
expect(calculateTotalPrice(10, 3)).toBe("Total price is 30");
expect(calculateTotalPrice(5, 0)).toBe("Total price is 0");
expect(calculateTotalPrice(2.5, 4)).toBe("Total price is 10");
expect(calculateTotalPrice(8.99, 1)).toBe("Total price is 8.99");
});
function isLeapYear(year) {
if (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) {
return true;
} else {
return false;
}
}
答案:
test("isLeapYear function works correctly", () => {
expect(isLeapYear(2000)).toBe(true);
expect(isLeapYear(1900)).toBe(false);
expect(isLeapYear(2020)).toBe(true);
expect(isLeapYear(2022)).toBe(false);
});
function isRegisterValid(username, password, confirmPassword) {
if (username.length >= 3 && password.length >= 6 && password === confirmPassword) {
return true;
} else {
return false;
}
}
答案:
test("isRegisterValid function works correctly", () => {
expect(isRegisterValid("john", "123456", "123456")).toBe(true);
expect(isRegisterValid("joe123", "password", "password")).toBe(false);
expect(isRegisterValid("user", "123456", "654321")).toBe(false);
expect(isRegisterValid("admin", "password", "password")).toBe(false);
});
function getDayOfWeek(year, month, day) {
const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const date = new Date(`${month}/${day}/${year}`);
const dayOfWeek = daysOfWeek[date.getDay()];
return dayOfWeek;
}
答案:
test("getDayOfWeek function works correctly", () => {
expect(getDayOfWeek(2022, 11, 16)).toBe("Wednesday");
expect(getDayOfWeek(1970, 1, 1)).toBe("Thursday");
expect(getDayOfWeek(2021, 7, 4)).toBe("Sunday");
expect(getDayOfWeek(2030, 12, 25)).toBe("Wednesday");
});