Skip to content

feat: run web tests from a webpack or Rspack config - #308

Open
manapard wants to merge 15 commits into
callstackincubator:mainfrom
manapard:feat/webpack-bundler
Open

manapard wants to merge 15 commits into
callstackincubator:mainfrom
manapard:feat/webpack-bundler

Conversation

@manapard

Copy link
Copy Markdown

What is this?

Harness can now run web tests in a project that is bundled by webpack or Rspack and has no Metro config. Until now the web platform needed Metro, so a react-native-web app built only with webpack could not adopt Harness (#194).

A project opts in with a new bundler option and a new package:

import { webpackBundler } from '@react-native-harness/bundler-webpack';

export default {
  entryPoint: './src/index.tsx',
  appRegistryComponentName: 'App',
  bundler: webpackBundler({ config: './webpack.config.js' }),
  // or rspackBundler({ config: './rspack.config.js' })
  runners: [/* webPlatform(...) */],
};

Metro stays the default and nothing changes for projects that use it. Native apps bundled with Re.Pack are not covered; that needs its own design.

How does it work?

Harness loads the project's own webpack or Rspack config, runs the compiler in-process and serves the result from its own server, next to the bridge the tests talk to. The project's loaders, aliases, plugins and constants apply as they do in the app. Harness changes only what testing needs: development mode, a source map it can read, optimizations that merge modules turned off, and the app's entry replaced by one that starts the Harness runtime. Each test file is built as a chunk of its own and loaded when its turn comes.

To make that possible, two parts of Harness stop assuming Metro:

  • The Jest side talks to a small bundler interface (@react-native-harness/bundlers). Metro is the first implementation and webpack/Rspack the second. Plugins get bundler:* hooks that fire for every bundler; the metro:* hooks still fire for Metro.
  • The runtime reaches the module system (loading a test file, mock, requireActual, resetModules) and the host (dev server URL, reload, stack parsing) through two seams, with a Metro and a webpack implementation of each.

Watch mode, code frames for failures, --coverage, setup files and module mocking work on both new bundlers. Two mocking behaviors differ from Metro and are documented with a pattern that works everywhere: call mock() before the consumer is first loaded, and return { __esModule: true, default: fn } when mocking a default export.

docs/adr/0002-webpack-bundler-backend.md records the decision, the options that were rejected and the known limits. apps/playground-webpack is a small react-native-web app with a webpack and an Rspack config that shows the integration and runs in CI on both.

Why is this useful?

Teams whose web app is built by webpack or Rspack can run Harness tests against the build their users get, with the same aliases, loaders and constants, without adding and maintaining a Metro config only for tests. The bundler interface and the runtime seams are also the groundwork Re.Pack support would need.

Closes #194

Record the design for issue callstackincubator#194: a bundler setting with Metro as the
default, a bundler-webpack package that runs the compiler in-process from
the project's config, test files delivered as async chunks from a generated
manifest, and two seams in the runtime for the module system and the host.

Refs callstackincubator#194
A small react-native-web app with no Metro config, built by webpack
(babel-loader) and by Rspack (builtin:swc-loader). It uses a path alias, a
DefinePlugin constant and an asset module, and carries Harness tests for
those features, rendering, module mocking and setup files.

It is the acceptance fixture for the bundler described in ADR 0002. The
Harness tests cannot run until that bundler exists.

Refs callstackincubator#194
Add @react-native-harness/bundlers with the bundler descriptor, the
bundler-neutral instance and events, and the startup detection moved out of
bundler-metro. bundler-metro implements it through metroBundler() and a
module that wraps the Metro instance and translates its reporter events.

The Harness config gains an optional bundler field; without it the Jest
session uses Metro as before. New bundler:* plugin hooks fire for every
bundler, next to the existing metro:* hooks when the bundler is Metro.

No change in behavior for Metro. This is step 1 of ADR 0002.

Refs callstackincubator#194
The neutral interface has no HTTP server, health is reported as a status
object, and the descriptor carries a display name.

Refs callstackincubator#194
Split the runtime along a module system (load a file and run it, release
it, create the mock registry) and a host (dev-server URL, reload, HMR,
LogBox, the global error handler, stack parsing). The Metro and React
Native implementations are the existing code moved into src/metro and
installed by the Metro entry before the common startup runs.

A lint rule keeps files outside src/metro from importing that folder,
React Native deep imports, or Metro's module registry.

No change in behavior. The playground suite gives identical results before
and after on Chromium, on a physical Android device and on the iOS
simulator. This is step 2 of ADR 0002.

Refs callstackincubator#194
Loading a file and running it stay separate steps, because a test file is
fetched before collection and executed inside it. Note what that requires
of the webpack implementation, and update paths that moved.

Refs callstackincubator#194
Add @react-native-harness/bundler-webpack. webpackBundler() and
rspackBundler() load the project's own config, swap the app entry for the
Harness runtime, and serve the compilation and the bridge from one
Harness-owned server. Test and setup files are async chunks listed in a
generated manifest, and a pre-loader applies the mock id rewrite whatever
transpiler the project uses. Web platform only; watch mode fails with a
clear error until it is implemented.

The runtime gains a webpack module system and a browser host next to the
Metro ones. The bundler interface gains an optional setRunFiles hook that
the session calls at the start of each run. Server connection tracking is
shared with the Metro bundler through @react-native-harness/tools.

The sandbox app now runs its Harness tests on both bundlers, selected with
HARNESS_BUNDLER. Metro behavior is unchanged: the playground suite gives
identical results on Chromium, a physical Android device and the iOS
simulator. This is step 3 of ADR 0002.

Refs callstackincubator#194
Describe the generated entry and manifest as built, require.ensure and its
four-argument form, the swap before resolution and its two errors, the
loader's rules, page-request detection and setRunFiles. Record the two
measured mocking differences from Metro and why they are documented rather
than removed. Rspack moves into step 3.

Refs callstackincubator#194
…nerated page

Brings the webpack and Rspack bundler to parity with Metro for web runs:

- Watch mode. Each watch run writes a marker into the generated manifest
  and waits for the build that picks it up, so the build holds every edit
  made before the run. The bundler then asks the session to reload the app,
  which still holds the chunks of the build before. A watcher that ignores
  the .harness folder fails the run with an error that says so.
- /symbolicate. Failures get a code frame from the build's source maps,
  whatever the config names the map files and their sources.
- Coverage. The loader instruments the project's source as written, with
  the settings the Babel preset gives Metro, and a persistent cache gets an
  identity of its own while coverage is collected.
- A project whose build emits no HTML gets a generated page that loads
  every entrypoint.

BundlerInstance.setRunFiles now resolves with { reloadRequired }, and the
session performs the reload through its reset strategies. Metro does not
implement it and behaves as before.

Refs callstackincubator#194
…bpack

Describes what step 4 built: which devtool settings are kept, the cache
identity while coverage is collected, what the loader instruments, how a
frame is mapped back to a file, the generated page, and the watch-mode
wait on the compiler's own watcher. Adds the costs and limits that came
out of it to the risks.

Refs callstackincubator#194
…g users cannot act on

The two build errors for an entryPoint that matches no entry told users to
copy the path as written in the config's entry, which is wrong when the
config's context is not the project root. They now say what is compared:
entryPoint resolved from the project root against the entry request.

A project installed from npm printed a missing-export warning on every run,
from Harness's own packages importing native-only exports of react-native,
which a web build aliases to react-native-web. That one warning is now
ignored. The project's own ignoreWarnings entries are kept, and no other
warning is hidden.

Refs callstackincubator#194
Adds a matrix job next to the web end-to-end job that runs
apps/playground-webpack in Chromium on each bundler, under the same
trigger. Screenshot artifacts are off for it: the app has no screenshot
tests, and the action's artifact name is the same for every job.

Refs callstackincubator#194
Adds a guide for the webpack and Rspack bundler, the bundler option, the
bundler:* plugin hooks, and the mocking patterns that behave the same on
every bundler. Corrects the web platform's requirements: Harness starts the
bundler itself, and a Metro project's page has to be served by its Metro
config. Brings the sandbox app's and the package's README up to date.

Refs callstackincubator#194
@vercel

vercel Bot commented Nov 17, 2026

Copy link
Copy Markdown

@manapard is attempting to deploy a commit to the Callstack Team on Vercel.

A member of the Team first needs to authorize it.

@manapard

manapard commented Nov 17, 2026 •

Copy link
Copy Markdown
Author

Demo: the sandbox app from this PR (apps/playground-webpack, a react-native-web app with no Metro config) running its Harness tests in headless Chromium.

  1. The Harness config points at the project's own webpack or Rspack config.
  2. pnpm test:harness on webpack.
  3. HARNESS_BUNDLER=rspack pnpm test:harness on Rspack.
  4. A failing assertion, with a code frame that points into the TypeScript source.
demo.mp4

@manapard
manapard marked this pull request as ready for review November 18, 2026 08:36

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support projects with Webpack-only bundle setup

1 participant