Skip to content

Commit 975d553

Browse files
author
Pepper Pancoast
committed
test: make snapshot path matching CWD-agnostic
Fixes: #61303 The snapshot path transformation was incorrectly matching partial paths like '/node' within '/node_modules' or 'nodejs.org', causing false replacements. Added negative lookahead (?![\w/]) to the regex patterns to ensure only complete path segments are matched, preventing partial matches in directory names and URLs.
1 parent 5babc8d commit 975d553

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

test/common/assertSnapshot.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,18 @@ function transformPath(dirname, replacement) {
108108
// On Windows, paths are case-insensitive, so we need to use case-insensitive
109109
// regex replacement to handle cases where the drive letter case differs.
110110
const flags = common.isWindows ? 'gi' : 'g';
111-
const urlEncodedRegex = new RegExp(RegExp.escape(urlEncoded), flags);
112-
const dirnameRegex = new RegExp(RegExp.escape(dirname), flags);
113-
const winPathRegex = new RegExp(RegExp.escape(winPath), flags);
111+
112+
// Escape and add word boundaries to prevent partial matches
113+
// (e.g., /node shouldn't match /node_modules or nodejs.org)
114+
const escapedUrlEncoded = RegExp.escape(urlEncoded);
115+
const escapedDirname = RegExp.escape(dirname);
116+
const escapedWinPath = RegExp.escape(winPath);
117+
118+
// Use negative lookahead to prevent matching if followed by alphanumeric or underscore
119+
const urlEncodedRegex = new RegExp(escapedUrlEncoded + '(?![\\w/])', flags);
120+
const dirnameRegex = new RegExp(escapedDirname + '(?![\\w/])', flags);
121+
const winPathRegex = new RegExp(escapedWinPath + '(?![\\w/])', flags);
122+
114123
return (str) => {
115124
return str.replaceAll('\\\'', "'")
116125
// Replace fileUrl first as `winPath` could be a substring of the fileUrl.

0 commit comments

Comments
 (0)