Skip to content

feat: add unmockRequire / doUnmockRequire API#1098

Merged
9aoy merged 1 commit intomainfrom
unmockRequire
Mar 25, 2026
Merged

feat: add unmockRequire / doUnmockRequire API#1098
9aoy merged 1 commit intomainfrom
unmockRequire

Conversation

@9aoy
Copy link
Copy Markdown
Collaborator

@9aoy 9aoy commented Mar 25, 2026

Summary

add rs.unmockRequire / rs.doUnmockRequire API.

rs.unmockRequire

  • Type: (path: string) => void

Cancels the mock implementation for modules loaded through require(). Like rs.mockRequire, this call is hoisted to the top of the file.

Use this API when you want later require() calls to load the original CommonJS module again.

const { sum } = require('./math.cjs');

rs.mockRequire('./math.cjs', () => ({
  sum: (a, b) => a + b + 100,
}));

rs.unmockRequire('./math.cjs');

test('unmockRequire restores the original CommonJS module', () => {
  expect(sum(1, 2)).toBe(3);
});

rs.doUnmockRequire

  • Type: (path: string) => void

Same as rs.unmockRequire, but it is not hoisted. Only later require() calls will load the original module again.

test('doUnmockRequire only affects later require calls', () => {
  rs.doMockRequire('./math.cjs', () => ({
    sum: (a, b) => a + b + 100,
  }));

  const { sum: mockedSum } = require('./math.cjs');
  expect(mockedSum(1, 2)).toBe(103);

  rs.doUnmockRequire('./math.cjs');

  const { sum } = require('./math.cjs');
  expect(sum(1, 2)).toBe(3);
});

Related Links

Checklist

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

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

Deploying rstest with  Cloudflare Pages  Cloudflare Pages

Latest commit: 17f8275
Status: ✅  Deploy successful!
Preview URL: https://42f7fa7a.rstest.pages.dev
Branch Preview URL: https://unmockrequire.rstest.pages.dev

View logs

@github-actions
Copy link
Copy Markdown

Rsdoctor Bundle Diff Analysis

Found 12 projects in monorepo, 2 projects with changes.

📊 Quick Summary
Project Total Size Change
adapter-rsbuild 2.1 KB 0
adapter-rslib 23.1 KB 0
adapter-rspack 5.9 KB 0
browser 2.0 MB 0
browser-react 3.7 KB 0
browser-ui 804.3 KB 0
coverage-istanbul 9.1 KB 0
core/rsdoctor-browser 971.6 KB +72.0 B (0.0%)
core/rsdoctor-loaders 869.0 B 0
core/rsdoctor-main 1.5 MB +236.0 B (0.0%)
vscode/rsdoctor-extension 60.2 MB 0
vscode/rsdoctor-worker 14.4 KB 0
📋 Detailed Reports (Click to expand)

📁 core/rsdoctor-browser

Path: packages/core/dist/rsdoctor-browser/rsdoctor-data.json

📌 Baseline Commit: a0e6b76001 | PR: #1097

Metric Current Baseline Change
📊 Total Size 971.6 KB 971.5 KB +72.0 B (0.0%)
📄 JavaScript 963.5 KB 963.4 KB +72.0 B (0.0%)
🎨 CSS 0 B 0 B 0
🌐 HTML 0 B 0 B 0
📁 Other Assets 8.1 KB 8.1 KB 0

📦 Download Diff Report: core/rsdoctor-browser Bundle Diff

📁 core/rsdoctor-main

Path: packages/core/dist/rsdoctor-main/rsdoctor-data.json

📌 Baseline Commit: a0e6b76001 | PR: #1097

Metric Current Baseline Change
📊 Total Size 1.5 MB 1.5 MB +236.0 B (0.0%)
📄 JavaScript 1.5 MB 1.5 MB +236.0 B (0.0%)
🎨 CSS 0 B 0 B 0
🌐 HTML 0 B 0 B 0
📁 Other Assets 75.4 KB 75.4 KB 0

📦 Download Diff Report: core/rsdoctor-main Bundle Diff

Generated by Rsdoctor GitHub Action

@9aoy 9aoy merged commit 66d0d53 into main Mar 25, 2026
19 checks passed
@9aoy 9aoy deleted the unmockRequire branch March 25, 2026 06:15
@9aoy 9aoy mentioned this pull request Mar 26, 2026
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.

1 participant