< Unit Testing < Jest

Create a JavaScript Program

Create the following JavaScript program. Save it as multiply.js

function multiply(x, y) {
    let result = x * y;
    return result;
}

module.exports = multiply;

Create a Test Program

Create the following Jest test program. Save it as multiply.test.js

const multiply = require("./multiply");

test("Multiply 2 * 2 = 4", () => {
    expect(multiply(2, 2)).toBe(4);
});

Test Success

Test the program by running Jest in a terminal or command prompt in the same folder as the two programs above and observe the results.

npm test

Test Failure

Change the multiply source code somehow, such as multiplying by 0 rather than multiplying by y. Test the program by running Jest again and observe the results.

This article is issued from Wikiversity. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.