npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

vitest-plugin-module-federation

v1.0.0

Published

Auto-mock Module Federation remote imports in Vitest — no more unresolvable 'remoteApp/Component' imports in unit tests.

Readme

Vitest Plugin Module Federation

CI npm version

Vitest plugin to auto-mock Module Federation remote imports in unit tests.

The Problem

If your app consumes federated remotes, imports like shop/Button only exist at runtime:

Error: Failed to resolve import "shop/Button" from "src/Basket.tsx"

The usual fix is a vi.mock('shop/Button', ...) in every test file that touches a remote. That gets old quickly.

The Solution

Register the plugin once and tell it which remotes you have:

// vitest.config.ts
import { defineConfig } from 'vitest/config';
import moduleFederationMock from 'vitest-plugin-module-federation';

export default defineConfig({
	plugins: [
		moduleFederationMock({
			remotes: ['shop', 'checkout'],
		}),
	],
	test: {
		environment: 'jsdom',
	},
});

Anything imported from shop/* or checkout/* now resolves to a generated mock. By default that's a React component which renders its props and children with a predictable test id:

import Button from 'shop/Button';

render(<Button aria-label="checkout">Go</Button>);
screen.getByTestId('mock-shop-button');

No vi.mock calls and no changes to your components.

Requirements

  • Node.js >= 20
  • Vitest 2, 3 or 4 (it's a plain Vite plugin, so any Vite-based runner works)

Installation

pnpm

pnpm add -D vitest-plugin-module-federation

npm

npm install --save-dev vitest-plugin-module-federation

Usage

Telling the plugin about your remotes

Pass remote names directly, a RegExp, or reuse the federation config you already have. At least one of remotes or federationConfig is required and they merge when both are given:

moduleFederationMock({ remotes: ['shop', 'checkout'] });

// or match specifiers with a RegExp
moduleFederationMock({ remotes: /^apps_[a-z]+\// });

// or read the remote names from your Module Federation config
import { federationConfig } from './module-federation.config';
moduleFederationMock({ federationConfig });

Choosing the default mock

defaultMock controls what gets generated for a matched import:

  • 'react-component' (the default) renders <div data-testid="mock-<remote>-<module>" {...props}>{children}</div>. It's exported as default and as a PascalCased named export derived from the module path, so shop/user-profile also exports UserProfile. Note react needs to be installed; it isn't a dependency of this plugin.
  • 'empty' gives you export default {} with no React involved.
  • A function gives you full control and should return the module source:
moduleFederationMock({
	remotes: ['shop'],
	defaultMock: ({ remote, modulePath }) => `export default () => null;`,
});

Overriding individual modules

Sometimes a generated component isn't enough, for example a remote that exports utilities. Use mocks to override specific imports:

moduleFederationMock({
	remotes: ['shop', 'checkout'],
	mocks: {
		// inline module source, served as-is
		'checkout/config': `export const currency = 'GBP';`,

		// point the import at a real file (relative to the Vite root)
		'checkout/formatPrice': { file: './test/mocks/formatPrice.ts' },

		// keep the default mock but add named exports
		'shop/Header': { namedExports: ['HeaderNav'] },
	},
});

TypeScript

Your app probably declares its remote modules already. If not:

// remotes.d.ts
declare module 'shop/Button' {
	import type { ComponentType, PropsWithChildren } from 'react';
	const Button: ComponentType<PropsWithChildren<Record<string, unknown>>>;
	export default Button;
}

How it works

The plugin runs before Vite's own resolver (enforce: 'pre') and matches import specifiers against your remote names. Matches resolve to a virtual module (or to your file override) and the mock source is generated at load time. Because the interception happens at the resolver level, it makes no difference which bundler builds the app itself.

Credits

MIT © Chris Boakes