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");
});