The MFE React apps I work on all load the design system as a Module Federation remote called shared_components and are tested with Jest and React Testing Library, so every new app's first test fails because Jest has no idea what shared_components/NavBar is. The starter template we copy for new apps solves it with one line of config and a folder of very small files, and since the reasons behind both aren't written down anywhere, here they are.
Why Jest can't import a remote
In the app, an import from the remote looks like any other import:
import NavBar, { NavBarItem } from 'shared_components/NavBar'; import { ControlledInput, HookForm } from 'shared_components/Form'; When webpack builds the app, ModuleFederationPlugin knows that shared_components is a remote, so these imports become code that loads the remote's remoteEntry.js at runtime and asks its container for the ./NavBar module. Jest doesn't run webpack, it resolves modules the way Node does, so it goes looking for a shared_components package in node_modules, doesn't find one, and the test fails before it renders anything. Even if it could follow the import, we wouldn't want unit tests to download a deployed remote over the network, so we need Jest to resolve shared_components/<anything> to something local, and moduleNameMapper does that.
Mapping the remote to a folder
moduleNameMapper maps regular expressions to module paths, and captured groups can be used in the path with $1, so one entry covers every module the remote exposes. This is the relevant part of the template's jest.config.js:
moduleNameMapper: { '^shared_components/(.*)': '<rootDir>/node_modules/@org/dev-utils/src/wmf-mocks/shared_components/$1', }, With that, shared_components/NavBar resolves to wmf-mocks/shared_components/NavBar inside our dev utils package (a small internal package with dev-only helpers, which every app has as a dev dependency), and Jest picks the extension the same way it would for any other import.
Keep the
^in the pattern, since without it the entry would also match any module path that merely containsshared_components/somewhere in it, which Jest's own docs formoduleNameMapperwarn about.
The folder has a file for each module the remote exposes, named after the key in the remote's exposes block, so './NavBar' gets NavBar.tsx, './Form' gets Form.tsx, and so on (with a couple of exceptions like hooks, more on those below).
The mocks are the real components
Despite the folder's name, every file in it re-exports a real component from @org/shared_components, the remote's regular npm package, which is built with tsc into a CommonJS lib/ folder next to its federated build (I wrote about why in the post about the design system remote). The dev utils package depends on it, so a file in the folder looks like this:
// wmf-mocks/shared_components/NavBar.tsx import { NavBar, NavBarItem } from '@org/shared_components'; export default NavBar; export { NavBarItem }; The other files look the same, most of them with only named exports. So a test renders the real nav bar and the real form inputs, and getByRole('textbox', { name: /email/i }) finds the same input a user would see, instead of a stand-in someone wrote to make a test pass. Both packages are compiled to CommonJS before they're published (the dev utils package with tsc too, which is why the mapper points at src/, the compiled output keeps that folder), and they need to be, since our transformIgnorePatterns skips everything in node_modules, so Jest loads these files as they are without running them through SWC.
Older apps didn't have this, and you can still see what they did instead in a couple of them, a src/test/__mocks__/shared_components/ folder with hand-written fakes like this one:
export default function Login({ onSubmit }) { return <button onClick={() => onSubmit('username', 'password')}>Submit</button>; } That works because Jest treats a __mocks__ folder inside one of the roots as manual mocks for node modules, matched by the path under __mocks__, so __mocks__/shared_components/Login.tsx is used for shared_components/Login without any jest.mock() call. The problem is that each app kept its own copies, they drifted away from the real components, and a test could pass against a fake Login whose props the real one stopped accepting months ago.
Keeping the folder in step with the remote
When a test breaks for no obvious reason, the first thing to check is the shape of the file it imports, since a federated module and the package's barrel file don't export things the same way. shared_components/NavBar has NavBar as its default export and NavBarItem as a named one, while the package exports both as named exports from index.ts, so each mock file has to rebuild the shape of the exposed module from the barrel, and when someone adds a new expose to the remote, or a new named export to an existing one, the matching mock file needs the same change, which in practice means a new release of the dev utils package for most changes to the remote. If an app imports something the folder doesn't have yet, Jest says so quite clearly:
Configuration error: Could not locate module shared_components/hooks mapped as: .../node_modules/@org/dev-utils/src/wmf-mocks/shared_components/hooks. Every new expose in the remote needs a file in wmf-mocks, with the same default and named exports.
A local __mocks__ folder still works next to the mapper, because Jest looks for a manual mock in the module map before it applies moduleNameMapper. We use that for the few modules with no file in the shared folder, like hooks, where an app that needs it keeps its own src/test/__mocks__/shared_components/hooks.ts and everything else comes from the shared mocks.
The other thing that goes out of step is the version, since tests run against whatever version of @org/shared_components is in the app's lockfile, while the app in the browser always gets whatever is deployed. If the remote changed a component's props last week and the app hasn't updated its dev dependencies since, the tests are checking against the old component, so that's worth a look whenever a test and the running app disagree about a component.
The re-exports have a cost too, since every file requires the package's whole barrel, and the compiled CommonJS requires are eager, so a test file that imports only the nav bar still loads every component in the library (the rich text editor included) once per test file. We've accepted that in exchange for real components, but pointing the files at per-component paths under lib/ would avoid it.
Vitest has no moduleNameMapper, but its alias (merged into Vite's resolve.alias) accepts a regular expression in find and $1 in replacement, so the same mapping is a single entry there:
// vitest.config.ts, with path from 'node:path' test: { alias: [ { find: /^shared_components\/(.*)$/, replacement: path.resolve( __dirname, 'node_modules/@org/dev-utils/src/wmf-mocks/shared_components/$1', ), }, ], }, I haven't moved any of our federated apps to Vitest, so take that one as what the docs say rather than something we run.
A federated app's tests need a local module for every path the remote exposes, and publishing the remote as a normal package too, then mapping shared_components/* to thin files that re-export from it, gets us modules that are as close to production as tests can get without a network. The maintenance that comes with it is a file with the right default and named exports for each new expose, and a dev dependency that doesn't fall too far behind the deployed remote.