Repository navigation
use string search rather than regexp to find moduleid in defs - #349
Merged
iambumblehead merged 1 commit intoOct 15, 2026
Merged
Conversation
a regexp built per import from the module url was quadratic for failed lookups and treated '(' and ')' in paths as regexp syntax, see iambumblehead#348
Owner
|
@ekremney this looks like a great change. thanks for submitting this |
Owner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #348.
The loader's
resolvehook looked up each import's mock definition by building a regexp from the module url,.*(<url>(\?<treeid>(?:(?!#-#).)*)).*, and running it againstdefs/gdefs. This replaces that with a literal search that returns the same result:lastIndexOfmatches the last occurrence, as the greedy leading.*did, and the result stops at the next#-#, as(?:(?!#-#).)*did.isfound=falsebranch used capture group 2, the part after the url, so it now slices the url prefix off the result.moduleIdEsc(the+escaping from escape + char at regexp to resolve pnpn issue #315) is removed: nothing is interpreted as regexp syntax any more.esmockLoader.jsandesmockLoader.deprecated.js.Why
Performance. The unanchored leading
.*makes every failed lookup quadratic in the length ofdefs, and most lookups fail because most imports aren't mocked.defsgrows with every mocked module and its export names, soesmock()calls get quadratically slower as the mock map grows. Benchmark from #348 (warmesmock()calls; the target imports N mocked modules with 30 exports each, plus 20 unmocked modules; Node 24.16, macOS arm64):In a ~25k-test suite this took total unit-test time from ~32 min to ~6.5 min.
Correctness. Only
+was escaped, so(and)in a module path were read as regexp groups. The mock was never matched and the real module was used silently. That affects, for example, Next.js route-group folders likeapp/(marketing)/.Tests
tests-nodetest: "should mock file that has parentheses in path", with fixturestests/local/(parens) in path/parens-file.jsandtests/local/usesParensPath.js. It fails onmainand passes here.npm run lint: passes.npm run test:all(Node 24.16): passes.tests-pnpm(pnpm i && npm run test): passes. That's the scoped-package case behind the+escaping.Unrelated, in case it's useful: on Node 24,
node --loader=esmock --testintests-nodehas 5 failing tests onmainas well. They are "should import mock objects, local / global", "should mock fetch as shown in README", "should mock files with hashbangs" and "should work when modules have CJS imports". They're hidden becausetest:defaultfalls back totest:currentvia||. This branch fails the same 5 and no others.I left
CHANGELOG.mdalone, since you write the entries at release time.