如何使用 Jest 覆盖(或模拟)类方法以测试函数?

Cyr*_*skt 5 javascript unit-testing node.js jestjs

我对调用类的函数进行单元测试时遇到问题。它似乎总是调用“官方”类实例而不是我的模拟类。我无法强制我的函数使用我的模拟实例......

有一个包含我要测试的功能的文件:

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;
}
Run Code Online (Sandbox Code Playgroud)

有一个包含我的类定义的文件:

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

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

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

有一个规范文件:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我的测试通过了(因为 myMethod 返回“true”)并记录“我们使用了原始的 myMethod...模拟失败。”

所以我想通过模拟 myMethod 返回 false 来使我的测试总是失败。

你能帮助我吗 ?谢谢你的时间。

Cyr*_*skt 4

嗯。我找到了解决方案。

看。我的文件中的目标函数发生了变化。

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;
} 
Run Code Online (Sandbox Code Playgroud)

我的规格文件:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)