Jest est un Framwork fait par Facebook.
yarn add jest
yarn add @types/jest
const sum = (a,b) => a+b;
const sum = require("./sum");
test("1 + 2 = 3", () => {
expect(sum(1, 2)).toBe(3);
});
ajouter "scripts" comme cela :
{
"name": "jest",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@types/jest": "^26.0.23",
"jest": "^26.6.3"
},
"scripts" :{
"test":"jest --watchAll verbose"
}
}
$yarn test
PASS ./sum.test.js ✓ 1 + 2 = 3 (3 ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 1.9 s Ran all test suites. Watch Usage: Press w to show more.
const sum = require("./sum");
//it() === test()
it("1 + 2 = 3", () => {
expect(sum(1, 2)).toBe(3);
});
describe("", () =>{});
describe("sum test", () => {
it("1 + 2 == 3", () => {
expect(sum(1, 2)).toBe(3);
});
it("calculate all numbers in array", () => {
expect(sumOf([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toBe(55);
});
});
Reformer la function sumOf avec .reduce()
cf/ array.reduce([callback], [initial_value])
callback = (accumulator, currentValue) => accumulator + currentValue
const sum = (a, b) => a + b;
const sumOf = (numbers) => {
return numbers.reduce((acc, current) => acc + current, 0);
};
exports.sum = sum;
exports.sumOf = sumOf;
- test === it
- describe() est servi pour diviser, grouper les tests.