Skip to content

feat: support resolve.moduleNameMapper#990

Open
9aoy wants to merge 5 commits intomainfrom
moduleNameMapper
Open

feat: support resolve.moduleNameMapper#990
9aoy wants to merge 5 commits intomainfrom
moduleNameMapper

Conversation

@9aoy
Copy link
Copy Markdown
Collaborator

@9aoy 9aoy commented Feb 27, 2026

Summary

Implement resolve.moduleNameMapper configuration option for Jest compatibility, allowing users to map module imports using regex patterns. This is similar to Jest's moduleNameMapper configuration.

import { defineConfig } from '@rstest/core';

export default defineConfig({
  resolve: {
    moduleNameMapper: {
      // Map exact module name
      '^module-a$': '<rootDir>/mocks/module',
      // Map with capture groups
      '^@utils/(.*)$': '<rootDir>/src/utils/$1',
      // Stub CSS imports
      '\\.(css|less|scss)$': 'identity-obj-proxy',
    },
  },
});

Related Links

close #649

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Feb 27, 2026

Deploying rstest with  Cloudflare Pages  Cloudflare Pages

Latest commit: 001b4a0
Status: ✅  Deploy successful!
Preview URL: https://55e64a66.rstest.pages.dev
Branch Preview URL: https://modulenamemapper.rstest.pages.dev

View logs

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 001b4a01b9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +40 to +41
for (const [pattern, replacement] of Object.entries(moduleNameMapper)) {
const resourceRegExp = new RegExp(pattern);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Stop remapping a request after first matching pattern

Jest-style moduleNameMapper semantics depend on first-match-wins order, but this loop creates a separate NormalModuleReplacementPlugin for every pattern, so later patterns still run on a request that was already rewritten by an earlier one. In overlapping configurations (for example, a specific rule plus a broader fallback), the later plugin can override the intended mapping and resolve to the wrong module.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

@fi3ework fi3ework left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how could user choose between resolve.alias and resolve.moduleNameMapper.

@9aoy
Copy link
Copy Markdown
Collaborator Author

9aoy commented Feb 28, 2026

how could user choose between resolve.alias and resolve.moduleNameMapper.

Compared to resolve.alias, resolve.moduleNameMapper is more suitable for regular expression scenarios and Jest migrations.

@fi3ework
Copy link
Copy Markdown
Member

i feel like these two options are too similar for users, especially since they are almost identical when not using regular expressions. what if we provide a built-in plugin called moduleNameMapper? this way, we can avoid extending resolve while ensuring an almost identical Jest API.

@9aoy
Copy link
Copy Markdown
Collaborator Author

9aoy commented Mar 5, 2026

i feel like these two options are too similar for users, especially since they are almost identical when not using regular expressions. what if we provide a built-in plugin called moduleNameMapper? this way, we can avoid extending resolve while ensuring an almost identical Jest API.

Good idea. We can extract it into a standalone plugin, such as rsbuild-plugin-module-name-mapper?

@fi3ework
Copy link
Copy Markdown
Member

fi3ework commented Mar 5, 2026

Good idea. We can extract it into a standalone plugin, such as rsbuild-plugin-module-name-mapper?

maybe rsbuild-plugin-jest-compatible for future Jest fields, or rsbuild-plugin-test-compatible for even Vitest compatible.

@9aoy
Copy link
Copy Markdown
Collaborator Author

9aoy commented Mar 6, 2026

maybe rsbuild-plugin-jest-compatible for future Jest fields, or rsbuild-plugin-test-compatible for even Vitest compatible.

I prefer rsbuild-plugin-module-name-mapper. It accurately describes the capability, aligns with Rsbuild's plugin- naming convention, and keeps the scope honest. The regex-based path replacement is genuinely useful beyond testing, so a focused name avoids boxing the plugin into a "Jest compat" niche.

@fi3ework
Copy link
Copy Markdown
Member

fi3ework commented Mar 6, 2026

maybe rsbuild-plugin-jest-compatible for future Jest fields, or rsbuild-plugin-test-compatible for even Vitest compatible.

I prefer rsbuild-plugin-module-name-mapper. It accurately describes the capability, aligns with Rsbuild's plugin- naming convention, and keeps the scope honest. The regex-based path replacement is genuinely useful beyond testing, so a focused name avoids boxing the plugin into a "Jest compat" niche.

makes sense to me.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Mock all files with certain extension

2 participants