Skip to content

Commit 829e095

Browse files
authored
fix(cdk/testing): fix value stringification in harnesses (#23421)
* fix(cdk/testing): fix value stringification in harnesses * fixup! fix(cdk/testing): fix value stringification in harnesses
1 parent 40f0674 commit 829e095

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/cdk/testing/component-harness.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -561,15 +561,18 @@ function _valueAsString(value: unknown) {
561561
if (value === undefined) {
562562
return 'undefined';
563563
}
564-
// `JSON.stringify` doesn't handle RegExp properly, so we need a custom replacer.
565564
try {
566-
return JSON.stringify(value, (_, v) => {
567-
if (v instanceof RegExp) {
568-
return `/${v.toString()}/`;
569-
}
570-
571-
return typeof v === 'string' ? v.replace('/\//g', '\\/') : v;
572-
}).replace(/"\/\//g, '\\/').replace(/\/\/"/g, '\\/').replace(/\\\//g, '/');
565+
// `JSON.stringify` doesn't handle RegExp properly, so we need a custom replacer.
566+
// Use a character that is unlikely to appear in real strings to denote the start and end of
567+
// the regex. This allows us to strip out the extra quotes around the value added by
568+
// `JSON.stringify`. Also do custom escaping on `"` characters to prevent `JSON.stringify`
569+
// from escaping them as if they were part of a string.
570+
const stringifiedValue = JSON.stringify(value, (_, v) => v instanceof RegExp ?
571+
`◬MAT_RE_ESCAPE◬${v.toString().replace(/"/g, '◬MAT_RE_ESCAPE◬')}◬MAT_RE_ESCAPE◬` : v);
572+
// Strip out the extra quotes around regexes and put back the manually escaped `"` characters.
573+
return stringifiedValue
574+
.replace(/"MAT_RE_ESCAPE|MAT_RE_ESCAPE"/g, '')
575+
.replace(/MAT_RE_ESCAPE/g, '"');
573576
} catch {
574577
// `JSON.stringify` will throw if the object is cyclical,
575578
// in this case the best we can do is report the value as `{...}`.

src/cdk/testing/tests/cross-environment.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {
1010
ComponentHarness,
1111
ComponentHarnessConstructor,
12-
HarnessLoader,
12+
HarnessLoader, HarnessPredicate,
1313
parallel,
1414
TestElement,
1515
} from '@angular/cdk/testing';
@@ -311,6 +311,19 @@ export function crossEnvironmentSpecs(
311311
' the constraints: title = "List of test tools", item count = 4)');
312312
}
313313
});
314+
315+
it('should have correct description for debugging', () => {
316+
const predicate = new HarnessPredicate(MainComponentHarness, {})
317+
.addOption('test', {
318+
regexes: [/test/gim, /"test"/],
319+
strings: [`test`, `"test"`],
320+
numbers: [10]
321+
}, async () => true);
322+
expect(predicate.getDescription()).toBe(`test = {` +
323+
`"regexes":[/test/gim,/"test"/],` +
324+
`"strings":["test","\\"test\\""],` +
325+
`"numbers":[10]}`);
326+
});
314327
});
315328

316329
describe('TestElement', () => {

0 commit comments

Comments
 (0)