Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite (or mock) a class method with Jest in order to test a function?

I've an issue with unit test of a function wich call a class. It seems always call the "official" class instance and not my mocked class. I'm not able to force my function to use my mocked instance...

There is a file with the function I want to test:

const myClass = require('./myClass');
const instance = new myClass();

module.exports.functionToTest = async function () {

    // Some stuff...

    const value = await instance.myMethod();

    // Some stuff that define a result variable (partially with value).

    return result;
}

There is a file with my class definition :

module.exports = class myClass {
    async myMethod() {
        const result = await someStuffWillResolveMaybeTrueOrFalse();

        console.log('We used the original myMethod... Mocking has failed.');

        return result;
    }
}

There is a spec file:

const myFile = require('./myFile');
const myClass = require('./myClass');

describe('My test', async () => {
    it('should mock myClass.myMethod in order to return false', () => {
        const instance = new myClass();
        instance.myMethod = jest.fn().mockResolvedValue(false);

        const result = await myFile.functionToTest();

        expect(result).toBeTruthy();
    }
}

Unfortunatly my test is passing (because myMethod return "true") and log "We used the original myMethod... Mocking has failed."

So I want to make my test always fail by mocking that myMethod to return false.

Can you help me ? Thanks for your time.

like image 615
CyrilHskt Avatar asked Oct 28 '22 11:10

CyrilHskt


1 Answers

Hm. I've found a solution.

See. A change in my file with the target function.

const myClass = require('./myClass');
// const instance = new myClass(); <== Not here...

module.exports.functionToTest = async function () {
    const instance = new myClass(); // <== ...but there.

    // Some stuff...

    const value = await instance.myMethod();

    // Some stuff that define a result variable (partially with value).

    return result;
} 

And my spec file :

const myFile = require('./myFile');

// I specify to Jest that I'll mock a file
jest.mock('./myClass');
const myClass = require('./myClass');

// I prepare the mock function. In that case a promise wich resolve 'false'
const mMock = jest.fn().mockResolvedValue(false);

// I mock the method 'myMethod' in 'myClass'
myClass.mockImplementation(() => {
    return {
        myMethod: mMock
    };
});


// Then, I just take the test
describe('My test', async () => {
    it('should mock myClass.myMethod in order to return false', () => {
        const result = await myFile.functionToTest();

        expect(result).toBeFalsy();
    }
}
like image 138
CyrilHskt Avatar answered Nov 09 '22 09:11

CyrilHskt