Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest mocking - Mock named exports except for one

I have a redux file that contains my reducer and all of my actions via named exports.

export const reducer = (state, action) => ...

export myAction = action => {
    return {type: 'ACTION', action}
}
...

When in my test file I import the reducer and the actions. I have a renderWithRedux which takes in the reducer and creates a real store inside.

function renderWithRedux(ui, reducer = combineReducers(reducers), initialState = {}) {
    const store = createStore(reducer, initialState)
    const utils = render(<Provider store={store}>{ui}</Provider>)

    return {
        ...utils,
        store,
    }
}

The question I have is that the component I'm rendering is passed actions in the mapDispatchToProps in the connected component.

export default connect(mapStateToProps, { myAction })(MyComponent)

I want to mock the myAction in my particular use case this action would actually be a thunk and I want to see if the store updated.

The problem I have is how do I mock myAction but not the reducer. I've tried

jest.mock('./reducerFile', () => {
    return jest.fn().mockImplementation(() => {
        return {
            reducer: require.requireActual('./reducerFile').reducer,
            myAction: jest.fn()
        }
    })
})

But the reducer is still mocked somehow.

Is this even possible or am I just wanderlust.

like image 856
JeffBaumgardt Avatar asked Aug 27 '18 20:08

JeffBaumgardt


People also ask

How do you mock a named export in Jest?

In order to mock named exports in Jest, you need to import all named exports from a file with an * and assign jest. fn to the method that needs to be mocked. mockImplementation expects a callback function to be passed that describes the implementation.

How do you mock only one function from a module in Jest?

fn(). mockReturnValue({ someObjectProperty: 42 }); What we're doing here is first importing all the imports from @module/api , bundling it into an object, and storing it into the variable called moduleApi . Then, we're overwriting the function that we want to mock functionToMock with a Jest mock function.

How do you mock a particular function in Jest?

There are two ways to mock functions: Either by creating a mock function to use in test code, or writing a manual mock to override a module dependency.

How do you mock a default export component in Jest?

In order to successfully mock a module with a default export, we need to return an object that contains a property for __esModule: true and then a property for the default export. This helps Jest correctly mock an ES6 module that uses a default export. expect(method1()). toBe('You have called a mocked method 1!


1 Answers

You can try this workaround:

jest.mock('./reducerFile', () => {
  const moduleMock = jest.requireActual(
    './reducerFile'
  );

  return {
    ...moduleMock,
    myAction: jest.fn(),
  };
});
like image 160
Krzysztof Grzybek Avatar answered Sep 28 '22 01:09

Krzysztof Grzybek