Skip to content

Commit 91eb89d

Browse files
Normalizes return value for recorded tests
1 parent 1fcb104 commit 91eb89d

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

packages/lib-common/src/testUtil/serialize.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CustomDump {
1010
private readonly opts: yaml.DumpOptions,
1111
) {}
1212

13-
represent() {
13+
represent(): string {
1414
let result = yaml.dump(
1515
this.data,
1616
Object.assign({ replacer, schema }, this.opts),
@@ -35,7 +35,7 @@ const schema = yaml.DEFAULT_SCHEMA.extend({ implicit: [customDumpType] });
3535
const isObject = (value: unknown): value is object =>
3636
typeof value === "object" && value != null;
3737

38-
function hasSimpleChildren(value: unknown) {
38+
function hasSimpleChildren(value: unknown): boolean {
3939
if (isObject(value)) {
4040
return Object.values(value).every(
4141
(value) => !isObject(value) && !Array.isArray(value),
@@ -44,9 +44,10 @@ function hasSimpleChildren(value: unknown) {
4444
if (Array.isArray(value)) {
4545
return value.every((value) => !isObject(value) && !Array.isArray(value));
4646
}
47+
return false;
4748
}
4849

49-
function replacer(key: string, value: unknown) {
50+
function replacer(key: string, value: unknown): unknown {
5051
if (key === "") {
5152
return value;
5253
} // top-level, don't change this
@@ -58,6 +59,9 @@ function replacer(key: string, value: unknown) {
5859
return value; // default
5960
}
6061

61-
export const serialize = (obj: unknown) =>
62-
new CustomDump(obj, { noRefs: true, quotingType: '"' }).represent().trim() +
63-
"\n";
62+
export function serialize(obj: unknown): string {
63+
return (
64+
new CustomDump(obj, { noRefs: true, quotingType: '"' }).represent().trim() +
65+
"\n"
66+
);
67+
}

packages/lib-node-common/src/runRecordedTest.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ function createSelection(selection: SelectionPlainObject): Selection {
4040
return new Selection(anchor, active);
4141
}
4242

43+
function normalizeRecordedReturnValue(returnValue: unknown) {
44+
// Recorded fixtures store plain serialized data rather than runtime instances.
45+
return returnValue != null && typeof returnValue === "object"
46+
? JSON.parse(JSON.stringify(returnValue))
47+
: returnValue;
48+
}
49+
4350
interface RunRecordedTestOpts {
4451
/**
4552
* The path to the test fixture
@@ -218,14 +225,14 @@ export async function runRecordedTest({
218225
} else {
219226
if (fixture.thrownError != null) {
220227
throw new Error(
221-
`Expected error ${fixture.thrownError.name} but none was thrown`,
228+
`Expected error ${fixture.thrownError.name}, but none was thrown`,
222229
);
223230
}
224231

225232
assert.deepEqual(resultState, fixture.finalState, "Unexpected final state");
226233

227234
assert.deepEqual(
228-
returnValue,
235+
normalizeRecordedReturnValue(returnValue),
229236
fixture.returnValue,
230237
"Unexpected return value",
231238
);

0 commit comments

Comments
 (0)