Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock specific function in a module

Let's say I have a method bestFunction in module.js as so:

//module.js

export const bestFunction = x => { 
    return x + 1
}

How do I mock bestFunction if my code itself is importing as so:

import { bestFunction } from './module.js'
like image 912
Alejandro Avatar asked Nov 18 '25 08:11

Alejandro


1 Answers

In your test:

  import * as depends from './module.js';

    describe("when testing..", () => {
        beforeEach(() => {
            depends.bestFunction = jest.fn().mockImplementation(() => 22); 
        })

        it ('doStuff', () => {
            expect(objectUnderTest.foo()).toEqual('22 red balloons');

        });
    });
like image 162
dashton Avatar answered Nov 20 '25 22:11

dashton