1

I have a React-application which makes use of Webpack5 and ModuleFederationPlugin. Since I don't have access to the module while running the Jest test suite I've set up a "global" set of mock functions for each component within the module in a separate file used by every test. eg.

jest.mock('MyFedModule', () => ({
component1: (} => 'Mock',
component2: (} => 'Mock',
})

That all works fine.

Now in a particular Jest test script file I need to override the mock implementation to suit a different test case that needs different return values. If I repeat the above syntax in my test file and provide a different implementation for the one component, I lose all the mocks set up in my "global" file, which causes other things to fail. It's as though I'm completely redefining the "global" mock instead of adding to, or updating it.

I've tried lots of variations including importing the single component and somehow using that in the mock, but I still can't get the syntax correct. I know this should be possible, any ideas how to do this?

I mainly followed the details in this posting here but it didn't work for me.

0 Answers0