Jest mock module multiple times with different values

前端 未结 2 1700
小鲜肉
小鲜肉 2021-01-05 09:14

I have a function that I want to test and this function uses an imported module:

var a = require(\'./a\');

function add(b) {
  return a + b;
}

module.expor         


        
2条回答
  •  不知归路
    2021-01-05 09:54

    Add

    beforeEach(() => {
        jest.resetModules();
    });
    

    Final tests

    describe('add', () => {
    
        beforeEach(() => {
            jest.resetModules();
        });
    
        it('should add the mock number 5 to 2', () => {
            jest.setMock('./a', 5);
            const add = require('./add');
            expect(add(2)).toBe(7);
        });
    
        it('should add the mock number 2 to 2', () => {
            jest.setMock('./a', 2);
            const add = require('./add');
            expect(add(2)).toBe(4);
        });
    });
    

    Demo: https://repl.it/repls/TrustingBelatedProprietarysoftware

提交回复
热议问题