Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk/testing): fix value stringification in harnesses #23421

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/cdk/testing/component-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,15 +561,18 @@ function _valueAsString(value: unknown) {
if (value === undefined) {
return 'undefined';
}
// `JSON.stringify` doesn't handle RegExp properly, so we need a custom replacer.
try {
return JSON.stringify(value, (_, v) => {
if (v instanceof RegExp) {
return `/${v.toString()}/`;
}

return typeof v === 'string' ? v.replace('/\//g', '\\/') : v;
}).replace(/"\/\//g, '\\/').replace(/\/\/"/g, '\\/').replace(/\\\//g, '/');
// `JSON.stringify` doesn't handle RegExp properly, so we need a custom replacer.
// Use a character that is unlikely to appear in real strings to denote the start and end of
// the regex. This allows us to strip out the extra quotes around the value added by
// `JSON.stringify`. Also do custom escaping on `"` characters to prevent `JSON.stringify`
// from escaping them as if they were part of a string.
const stringifiedValue = JSON.stringify(value, (_, v) => v instanceof RegExp ?
`◬MAT_RE_ESCAPE◬${v.toString().replace(/"/g, '◬MAT_RE_ESCAPE◬')}◬MAT_RE_ESCAPE◬` : v);
// Strip out the extra quotes around regexes and put back the manually escaped `"` characters.
return stringifiedValue
.replace(/"◬MAT_RE_ESCAPE◬|◬MAT_RE_ESCAPE◬"/g, '')
.replace(/◬MAT_RE_ESCAPE◬/g, '"');
} catch {
// `JSON.stringify` will throw if the object is cyclical,
// in this case the best we can do is report the value as `{...}`.
Expand Down
15 changes: 14 additions & 1 deletion src/cdk/testing/tests/cross-environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {
ComponentHarness,
ComponentHarnessConstructor,
HarnessLoader,
HarnessLoader, HarnessPredicate,
parallel,
TestElement,
} from '@angular/cdk/testing';
Expand Down Expand Up @@ -311,6 +311,19 @@ export function crossEnvironmentSpecs(
' the constraints: title = "List of test tools", item count = 4)');
}
});

it('should have correct description for debugging', () => {
const predicate = new HarnessPredicate(MainComponentHarness, {})
.addOption('test', {
regexes: [/test/gim, /"test"/],
strings: [`test`, `"test"`],
numbers: [10]
}, async () => true);
expect(predicate.getDescription()).toBe(`test = {` +
`"regexes":[/test/gim,/"test"/],` +
`"strings":["test","\\"test\\""],` +
`"numbers":[10]}`);
});
});

describe('TestElement', () => {
Expand Down