Skip to content

Commit e291c5c

Browse files
committed
test: make snapshot path matching CWD-agnostic
1 parent 3dff7ef commit e291c5c

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, underscore, or slash
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)