Testing ES6 functions is pretty straightforward. Let's consider a function that calculates the total bill for a person when the hourly rate and the number of hours are provided. Save the snippet in a file called something like calculateBill.js
.
Let's consider the following function, calculateBill
. It takes totalHours
and ratePerHours
as the arguments, and returns the total bill:
const calculateBill = (totalHours, ratePerHours) => totalHours * ratePerHours;
module.exports = calculateBill;
To test, it let us create a file and call it calculateBill-test.js
:
const calculateBill = require("../calculateBill"); test("calculate bills when the number of hours and the hourly rate is provided.", () => { expect(calculateBill(10, 40)).toBe(400); });
We can run the test by simply executing a yarn test
command as:
yarn test yarn run v1.12.1 $ jest PASS app/JS/__tests__/calculateBill-test.js calculate bills when the number of hours and hourly rate are provided...