Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Mar 21, 2023
1 parent 1ce889c commit 4697a7a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
10 changes: 5 additions & 5 deletions packages/workflow/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ const getReplaceCircularReferencesFn = (options: JSONStringifyOptions) => {
};
};

export const jsonStringify = (obj: unknown, options?: JSONStringifyOptions): string => {
if (options?.replaceCircularRefs === true) {
return JSON.stringify(obj, getReplaceCircularReferencesFn(options));
}
return JSON.stringify(obj);
export const jsonStringify = (obj: unknown, options: JSONStringifyOptions = {}): string => {
const replacer = options?.replaceCircularRefs
? getReplaceCircularReferencesFn(options)
: undefined;
return JSON.stringify(obj, replacer);
};

export const sleep = async (ms: number): Promise<void> =>
Expand Down
17 changes: 16 additions & 1 deletion packages/workflow/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jsonParse, deepCopy } from '@/utils';
import { jsonParse, jsonStringify, deepCopy } from '@/utils';

describe('jsonParse', () => {
it('parses JSON', () => {
Expand All @@ -17,6 +17,21 @@ describe('jsonParse', () => {
});
});

describe('jsonStringify', () => {
const source: any = { a: 1, b: 2 };
source.c = source;

it('should throw errors on circular references by default', () => {
expect(() => jsonStringify(source)).toThrow('Converting circular structure to JSON');
});

it('should break circular references when requested', () => {
expect(jsonStringify(source, { replaceCircularRefs: true })).toEqual(
'{"a":1,"b":2,"c":"[Circular Reference]"}',
);
});
});

describe('deepCopy', () => {
it('should deep copy an object', () => {
const serializable = {
Expand Down

0 comments on commit 4697a7a

Please sign in to comment.