(str, testDirectory)
| 28 | * @returns {string} normalized string |
| 29 | */ |
| 30 | const normalizeString = (str, testDirectory) => { |
| 31 | if (!str) return str; |
| 32 | const root = getWebpackRoot(testDirectory); |
| 33 | // Replace more-specific test dir first, then the broader root |
| 34 | str = str.replace(new RegExp(quoteMeta(testDirectory), "g"), "<TEST_DIR>"); |
| 35 | if (root) { |
| 36 | str = str.replace(new RegExp(quoteMeta(root), "g"), "<WEBPACK_ROOT>"); |
| 37 | // Replace ancestor directories above webpack root. |
| 38 | // The resolver walks up to the filesystem root looking for |
| 39 | // node_modules, producing paths like /Users/x/node_modules. |
| 40 | let ancestor = path.dirname(root); |
| 41 | while (ancestor !== path.dirname(ancestor)) { |
| 42 | str = str.replace(new RegExp(quoteMeta(ancestor), "g"), "<ANCESTOR>"); |
| 43 | ancestor = path.dirname(ancestor); |
| 44 | } |
| 45 | } |
| 46 | // Normalize the output directory suite name (e.g. test/js/ConfigTestCases/ |
| 47 | // vs test/js/ConfigCacheTestCases/) so all suites produce identical snapshots. |
| 48 | str = str.replace(/(<WEBPACK_ROOT>\/test\/js\/)[^/]+\//g, "$1<OUTPUT>/"); |
| 49 | // Strip stack trace lines — line numbers vary across Node.js versions |
| 50 | // and between runs (e.g. processTicksAndRejections). |
| 51 | str = str |
| 52 | .split("\n") |
| 53 | .filter((line) => !/^\s+at\s/.test(line)) |
| 54 | .join("\n") |
| 55 | .replace(/\n{3,}/g, "\n\n") |
| 56 | .trim(); |
| 57 | // Normalize engine-specific JSON.parse error text (V8 "Unexpected token … |
| 58 | // (0x..)" / "… in JSON …" vs JSC/Bun "JSON Parse error: …") so JSON |
| 59 | // type-import "Module parse failed" snapshots match across runtimes. The |
| 60 | // acorn "(line:col)" parse errors are engine-independent and left as-is. |
| 61 | str = str.replace( |
| 62 | /Module parse failed: (?:JSON Parse error:|Unexpected token "[^"]*" \(0x|Unexpected \S+ in JSON|Unexpected end of JSON input|Unexpected non-whitespace character after JSON)[\s\S]*?(?=\nYou may need an appropriate loader)/g, |
| 63 | "Module parse failed: <JSON parse error>" |
| 64 | ); |
| 65 | // Normalize JSC (Bun) magic-comment parse phrasing to the V8 form: JSC |
| 66 | // quotes the token and appends "Expected …", V8 does neither. |
| 67 | str = str.replace(/(Unexpected token) '([^']*)'\. Expected[^\n]*/g, "$1 $2"); |
| 68 | str = str.replace(/(Unexpected identifier)\. Expected[^\n]*/g, "$1"); |
| 69 | // Normalize "Unexpected token" messages — quoting and detail |
| 70 | // format varies across Node.js versions (e.g. with/without quotes, |
| 71 | // hex codes, trailing context). |
| 72 | str = str.replace(/(Unexpected token) '([^']*)'$/gm, "$1 $2"); |
| 73 | str = str.replace(/(Unexpected token[^)]*\))[^\n]*(?:\n['"])?/g, "$1"); |
| 74 | str = str.replace(/\\/g, "/"); |
| 75 | return str; |
| 76 | }; |
| 77 | |
| 78 | /** |
| 79 | * Serializes an array of error/warning objects into a normalized form |
no test coverage detected