Skip to content

Commit 68ff3df

Browse files
Merge pull request #1392 from open-circle/improve-test-coverage
Enhance test coverage of library
2 parents b507228 + 17c092c commit 68ff3df

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

library/src/actions/parseJson/parseJson.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,17 @@ describe('parseJson', () => {
151151
});
152152
});
153153
});
154+
155+
describe('should throw non-Error exceptions', () => {
156+
test('for non-Error thrown by reviver', () => {
157+
const action = parseJson({
158+
reviver: () => {
159+
throw 'custom string error';
160+
},
161+
});
162+
expect(() => action['~run']({ typed: true, value: '{}' }, {})).toThrow(
163+
'custom string error'
164+
);
165+
});
166+
});
154167
});

library/src/actions/stringifyJson/stringifyJson.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,17 @@ describe('stringifyJson', () => {
230230
});
231231
});
232232
});
233+
234+
describe('should throw non-Error exceptions', () => {
235+
test('for non-Error thrown by replacer', () => {
236+
const action = stringifyJson({
237+
replacer: () => {
238+
throw 'custom string error';
239+
},
240+
});
241+
expect(() =>
242+
action['~run']({ typed: true, value: { foo: 'bar' } }, {})
243+
).toThrow('custom string error');
244+
});
245+
});
233246
});

library/src/schemas/record/record.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ describe('record', () => {
6565
test('for simple record', () => {
6666
expectNoSchemaIssue(schema, [{ foo: 1, bar: 2, baz: 3 }]);
6767
});
68+
69+
test('for record with __proto__ key', () => {
70+
const input = JSON.parse('{"__proto__": 123, "foo": 456}');
71+
expect(schema['~run']({ value: input }, {})).toStrictEqual({
72+
typed: true,
73+
value: { foo: 456 },
74+
});
75+
});
6876
});
6977

7078
describe('should return dataset with issues', () => {
@@ -202,6 +210,34 @@ describe('record', () => {
202210
} satisfies FailureDataset<InferIssue<typeof schema>>);
203211
});
204212

213+
test('for first key invalid only', () => {
214+
const keySchema = record(picklist(['foo', 'bar']), number());
215+
const input = { invalid: 1 };
216+
expect(keySchema['~run']({ value: input }, {})).toStrictEqual({
217+
typed: false,
218+
value: {},
219+
issues: [
220+
{
221+
...baseInfo,
222+
kind: 'schema',
223+
type: 'picklist',
224+
input: 'invalid',
225+
expected: '("foo" | "bar")',
226+
received: '"invalid"',
227+
path: [
228+
{
229+
type: 'object',
230+
origin: 'key',
231+
input,
232+
key: 'invalid',
233+
value: 1,
234+
},
235+
],
236+
},
237+
],
238+
});
239+
});
240+
205241
test('with abort early for invalid key', () => {
206242
const input = {
207243
foo: 1,

library/src/utils/_getLastMetadata/_getLastMetadata.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,14 @@ describe('_getLastMetadata', () => {
7272
)
7373
).toBe('bar');
7474
});
75+
76+
test('for nested schema without matching metadata', () => {
77+
expect(
78+
_getLastMetadata(
79+
pipe(pipe(string(), title('found')), pipe(string(), email())),
80+
'title'
81+
)
82+
).toBe('found');
83+
});
7584
});
7685
});

library/vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default defineConfig({
1414
'**/types.ts',
1515
'**/*.test.ts',
1616
'**/*.test-d.ts',
17+
'**/.DS_Store',
1718
],
1819
},
1920
},

0 commit comments

Comments
 (0)