最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin9浏览0评论

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();
    }
}
发布评论

评论列表(0)

  1. 暂无评论