Skip to content

Commit edf90ce

Browse files
committed
test: use RegExp.escape to improve test reliability
PR-URL: #60803 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent f5f9b2d commit edf90ce

13 files changed

Lines changed: 26 additions & 27 deletions

‎test/common/assertSnapshot.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function replaceTestDuration(str) {
109109

110110
const root = path.resolve(__dirname, '..', '..');
111111
const color = '(\\[\\d+m)';
112-
const stackTraceBasePath = new RegExp(`${color}\\(${root.replaceAll(/[\\^$*+?.()|[\]{}]/g, '\\$&')}/?${color}(.*)${color}\\)`, 'g');
112+
const stackTraceBasePath = new RegExp(`${color}\\(${RegExp.escape(root)}/?${color}(.*)${color}\\)`, 'g');
113113

114114
function replaceSpecDuration(str) {
115115
return str

‎test/parallel/test-crypto-argon2.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ for (const [algorithm, overrides, expected] of good) {
112112
for (const [algorithm, overrides, param] of bad) {
113113
const expected = {
114114
code: 'ERR_OUT_OF_RANGE',
115-
message: new RegExp(`The value of "${param}" is out of range`),
115+
message: new RegExp(`The value of "${RegExp.escape(param)}" is out of range`),
116116
};
117117
const parameters = { ...defaults, ...overrides };
118118
assert.throws(() => crypto.argon2(algorithm, parameters, () => {}), expected);
@@ -122,7 +122,7 @@ for (const [algorithm, overrides, param] of bad) {
122122
for (const key of Object.keys(defaults)) {
123123
const expected = {
124124
code: 'ERR_INVALID_ARG_TYPE',
125-
message: new RegExp(`"parameters\\.${key}"`),
125+
message: new RegExp(`"parameters\\.${RegExp.escape(key)}"`),
126126
};
127127
const parameters = { ...defaults }