Skip to content

test: add comprehensive tests for NormalModuleReplacementPlugin#20709

Merged
alexander-akait merged 2 commits intowebpack:mainfrom
aryanraj45:test/normal-module-replacement-plugin
Mar 26, 2026
Merged

test: add comprehensive tests for NormalModuleReplacementPlugin#20709
alexander-akait merged 2 commits intowebpack:mainfrom
aryanraj45:test/normal-module-replacement-plugin

Conversation

@aryanraj45
Copy link
Copy Markdown
Contributor

Summary

Adds comprehensive test coverage for NormalModuleReplacementPlugin, ensuring that both beforeResolve and afterResolve hooks are fully tested. This PR covers:

  • String and function-based replacements.
  • Relative and absolute path-joining logic in the afterResolve phase.

What kind of change does this PR introduce?

test

Did you add tests for your changes?

Yes.

Does this PR introduce a breaking change?

No.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

N/A.

Use of AI
NO

Copilot AI review requested due to automatic review settings March 26, 2026 16:53
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 26, 2026

⚠️ No Changeset found

Latest commit: 17e62d4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new config case to exercise NormalModuleReplacementPlugin behaviors via bundled runtime assertions, aiming to cover both beforeResolve and afterResolve replacement paths.

Changes:

  • Add a new configCases fixture with a webpack.config.js that configures multiple NormalModuleReplacementPlugin scenarios (string + function replacements).
  • Add runtime assertions (it/expect) that require modules expected to be replaced.
  • Add fixture modules for original and replacement targets used by the plugin.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/configCases/plugins/normal-module-replacement-plugin/webpack.config.js Adds plugin setup to test string/function replacements and afterResolve resource rewriting.
test/configCases/plugins/normal-module-replacement-plugin/index.js Adds runtime assertions validating the replacement outcomes.
test/configCases/plugins/normal-module-replacement-plugin/b.js Replacement module for string-based replacement.
test/configCases/plugins/normal-module-replacement-plugin/c.js Replacement module for function-based replacement.
test/configCases/plugins/normal-module-replacement-plugin/after.js Replacement module for after-resolve scenarios.
test/configCases/plugins/normal-module-replacement-plugin/before-string.js “Should be replaced” sentinel module for beforeResolve string case.
test/configCases/plugins/normal-module-replacement-plugin/before-function.js “Should be replaced” sentinel module for beforeResolve function case.
test/configCases/plugins/normal-module-replacement-plugin/after-function.js “Should be replaced” sentinel module for afterResolve function case.
test/configCases/plugins/normal-module-replacement-plugin/after-relative.js “Should be replaced” sentinel module for afterResolve relative-string case.
test/configCases/plugins/normal-module-replacement-plugin/a.js Adds an extra fixture module (currently not referenced in the shown diffs).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +15 to +21
new NormalModuleReplacementPlugin(/\/after-function\.js$/, (result) => {
if (result.createData && result.createData.resource) {
const dir = path.dirname(result.createData.resource);
result.createData.resource = path.join(dir, "after.js");
}
}),
new NormalModuleReplacementPlugin(/\/after-relative\.js$/, "./after.js")
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

The /\/after-.../ regexes are POSIX-specific and won’t match Windows paths (which typically contain \ in resource / createData.resource during afterResolve). Use a cross-platform separator pattern (e.g., [/\\\\]after-function\\.js$ and [/\\\\]after-relative\\.js$) so the test behaves consistently across OSes.

Suggested change
new NormalModuleReplacementPlugin(/\/after-function\.js$/, (result) => {
if (result.createData && result.createData.resource) {
const dir = path.dirname(result.createData.resource);
result.createData.resource = path.join(dir, "after.js");
}
}),
new NormalModuleReplacementPlugin(/\/after-relative\.js$/, "./after.js")
new NormalModuleReplacementPlugin(/[\/\\]after-function\.js$/, (result) => {
if (result.createData && result.createData.resource) {
const dir = path.dirname(result.createData.resource);
result.createData.resource = path.join(dir, "after.js");
}
}),
new NormalModuleReplacementPlugin(/[\/\\]after-relative\.js$/, "./after.js")

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's fix it, test case looks good

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks! done

Comment on lines +15 to +21
new NormalModuleReplacementPlugin(/\/after-function\.js$/, (result) => {
if (result.createData && result.createData.resource) {
const dir = path.dirname(result.createData.resource);
result.createData.resource = path.join(dir, "after.js");
}
}),
new NormalModuleReplacementPlugin(/\/after-relative\.js$/, "./after.js")
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

The PR description mentions covering “relative and absolute path-joining logic in the afterResolve phase,” but the added fixtures only explicitly exercise a relative string replacement ("./after.js"). If you intended to also cover the plugin’s handling of an absolute replacement string, add a dedicated case that sets the replacement to an absolute path (e.g., path.resolve(__dirname, "after.js")) and assert it resolves correctly.

Copilot uses AI. Check for mistakes.
@aryanraj45
Copy link
Copy Markdown
Contributor Author

@alexander-akait done .please have a look thanks!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 26, 2026

Merging this PR will degrade performance by 25.64%

⚡ 2 improved benchmarks
❌ 1 regressed benchmark
✅ 141 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory benchmark "lodash", scenario '{"name":"mode-development","mode":"development"}' 5.3 MB 4.1 MB +30.63%
Memory benchmark "many-modules-esm", scenario '{"name":"mode-production","mode":"production"}' 9.1 MB 7 MB +30.14%
Memory benchmark "react", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 643.8 KB 865.8 KB -25.64%

Comparing aryanraj45:test/normal-module-replacement-plugin (17e62d4) with main (7bde8ab)

Open in CodSpeed

@alexander-akait
Copy link
Copy Markdown
Member

91.39% (+0.05%)

Great! We need more such tests, thanks

@alexander-akait alexander-akait merged commit 118e0c5 into webpack:main Mar 26, 2026
55 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

This PR is packaged and the instant preview is available (118e0c5).

Install it locally:

  • npm
npm i -D webpack@https://pkg.pr.new/webpack@118e0c5
  • yarn
yarn add -D webpack@https://pkg.pr.new/webpack@118e0c5
  • pnpm
pnpm add -D webpack@https://pkg.pr.new/webpack@118e0c5

@aryanraj45
Copy link
Copy Markdown
Contributor Author

91.39% (+0.05%)

Great! We need more such tests, thanks

Alright on it Alexander, Thanks!

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.

3 participants