vendredi 20 juillet 2018

How to test a return value of a mock function in jest

I want to test the return value of a mock function in jest.

In following code i want to mock fs module's mkdirSync function

const fs = require("fs");


describe("Testing mock fs.mkdirSync", () => {
    fs.mkdirSync = jest.fn().mockReturnValue(5);
    fs.mkdirSync("./apple");

    it("fs.mkdirSync must have been called", () => {
        expect(fs.mkdirSync).toHaveBeenCalled();
    });

    //This test fails with TypeError: Cannot read property '0' of undefined

    it("fs.mkdirSync last return value must be 5", () => {
        expect(fs.mkdirSync.mock.results[0].value).toBe(5);
    });



});

Second test fails with TypeError: Cannot read property '0' of undefined

Aucun commentaire:

Enregistrer un commentaire