how to mock import es6 export methods with Jest an

2019-07-30 08:26发布

I have the following module called index.js

export function iWantToMockThis(a) {
    return a + 1;
}

export function foo(a) {
    return iWantToMockThis(a);
}

and the following test

import {foo} from '../src/index';

describe("testing mock es6", () => {
    jest.doMock('../src/index', () => ({
        foo: jest.requireActual('../src/index').foo,
        iWantToMockThis: jest.fn(() => 99)
    }));

    test('mocking barProp', ()=> {
        expect(foo(2)).toBe(99); // <-- it should be 99 but its 3
    })

});

This is just a small example of what I'm trying to achieve a bigger scale, by design iWantToMockThis has to be in the same module, but I do not find the way to mock it and return what I want to the foo method.

Any idea?

Some context:

  • I tried many things, eg jest.mock before the es6 import.
  • Default jest configuration

complete example: https://github.com/juanpicado/jest_mock_es6_example

0条回答
登录 后发表回答