admin管理员组

文章数量:1327806

I have an issue with a unit test of a function which calls a class. It seems that it always calls 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();
    }
}

Unfortunately 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.

I have an issue with a unit test of a function which calls a class. It seems that it always calls 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();
    }
}

Unfortunately 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.

Share Improve this question edited Jan 11, 2023 at 1:50 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Jun 6, 2018 at 9:07 CyrilHsktCyrilHskt 1511 gold badge1 silver badge8 bronze badges 1
  • Possible duplicate of Jest: How to mock one specific method of a class – blade Commented Jun 12, 2019 at 15:46
Add a ment  | 

1 Answer 1

Reset to default 4

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();
    }
}

本文标签: javascriptHow to overwrite (or mock) a class method with Jest in order to test a functionStack Overflow