Skip to content

Commit

Permalink
馃挌 Stabilize tests on jsonValue
Browse files Browse the repository at this point in the history
THe current implementation of the test suffered from the fact that `fc.stringify` was producing valid JS values while `JSON.parse` was expecting valid JSON values. As the two specs are not fully aligned being a valid JS value does not imply being a valid JSON one.

Fixes #3478
  • Loading branch information
dubzzz committed May 17, 2023
1 parent a77a15c commit c54e901
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/fast-check/test/unit/arbitrary/jsonValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ describe('jsonValue (integration)', () => {
);

const isCorrect = (v: unknown, extra: Extra) => {
expect(JSON.parse(fc.stringify(v))).toEqual(v); // JSON.stringify does not handle the -0 properly
// JSON.stringify does not handle the -0 properly
// And fc.stringify produces '["__proto__"]:' whenever it encounters __proto__ as a key
// Our 'safeStringified' should hanlde both of the problems!
const intermediateStringified = fc.stringify(v);
const safeStringified = intermediateStringified.replace(/\["__proto__"\]:/g, '"__proto__":');
expect(JSON.parse(safeStringified)).toEqual(v);

if (extra !== undefined && extra.maxDepth !== undefined) {
expect(computeObjectDepth(v)).toBeLessThanOrEqual(extra.maxDepth);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ describe('unicodeJsonValue (integration)', () => {
);

const isCorrect = (v: unknown, extra: Extra) => {
expect(JSON.parse(fc.stringify(v))).toEqual(v); // JSON.stringify does not handle the -0 properly
// JSON.stringify does not handle the -0 properly
// And fc.stringify produces '["__proto__"]:' whenever it encounters __proto__ as a key
// Our 'safeStringified' should hanlde both of the problems!
const intermediateStringified = fc.stringify(v);
const safeStringified = intermediateStringified.replace(/\["__proto__"\]:/g, '"__proto__":');
expect(JSON.parse(safeStringified)).toEqual(v);

if (extra !== undefined && extra.maxDepth !== undefined) {
expect(computeObjectDepth(v)).toBeLessThanOrEqual(extra.maxDepth);
}
Expand Down

0 comments on commit c54e901

Please sign in to comment.