Skip to content

Commit

Permalink
test(utils): fix minor issues (#3174)
Browse files Browse the repository at this point in the history
- `fc.jsonObject()` might produce `-0` which serializes to `"0"` which then parses as `0` which Jest `toEqual` considers different from `-0`
- stop using `any` to get rid of warnings
  • Loading branch information
eventualbuddha committed Mar 21, 2023
1 parent 35adbe3 commit dd54ef0
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions libs/utils/src/json_stream.test.ts
Expand Up @@ -74,11 +74,11 @@ test('iterable', () => {
});

test('fails with circular references', () => {
const obj: any = { a: 1 };
const obj: { a: number; b?: unknown } = { a: 1 };
obj.b = obj;
expect(() => asString(obj)).toThrowError();

const arr: any[] = [];
const arr: unknown[] = [];
arr.push(arr);
expect(() => asString(arr)).toThrowError();
});
Expand All @@ -94,10 +94,15 @@ test('fails with non-serializable objects', () => {

test('generates correct JSON', () => {
fc.assert(
fc.property(fc.jsonObject(), fc.boolean(), (input, compact) => {
expect(
JSON.parse(asString(input as JsonStreamInput<unknown>, { compact }))
).toEqual(input);
})
fc.property(
// filter out -0 because JSON.stringify() converts it to 0
fc.jsonObject().filter((v) => !Object.is(v, -0)),
fc.boolean(),
(input, compact) => {
expect(
JSON.parse(asString(input as JsonStreamInput<unknown>, { compact }))
).toEqual(input);
}
)
);
});

0 comments on commit dd54ef0

Please sign in to comment.