Skip to content

Commit 5414340

Browse files
committed
test(commons): simplify assertions and remove unnecessary type casts in deepMerge tests
Replace verbose as-casts with type annotations, use direct value checks (toBe, toEqual, toHaveProperty, in-operator) instead of intermediate casts and property lookups.
1 parent d98d362 commit 5414340

1 file changed

Lines changed: 33 additions & 43 deletions

File tree

packages/commons/tests/unit/deepMerge.test.ts

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('Function: deepMerge', () => {
123123

124124
it('replaces non-object target values with source objects', () => {
125125
// Prepare
126-
const target = { a: 'string' } as Record<string, unknown>;
126+
const target: Record<string, unknown> = { a: 'string' };
127127
const source = { a: { nested: 1 } };
128128

129129
// Act
@@ -207,7 +207,7 @@ describe('Function: deepMerge', () => {
207207

208208
it('replaces non-array target with array source', () => {
209209
// Prepare
210-
const target = { arr: 'not an array' } as Record<string, unknown>;
210+
const target: Record<string, unknown> = { arr: 'not an array' };
211211
const source = { arr: [1, 2, 3] };
212212

213213
// Act
@@ -241,16 +241,16 @@ describe('Function: deepMerge', () => {
241241

242242
// Assess
243243
expect(result).toEqual({ a: 1, b: 2 });
244-
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
244+
expect('polluted' in {}).toBe(false);
245245
});
246246

247247
it('skips constructor keys from source', () => {
248248
// Prepare
249249
const target = { a: 1 };
250-
const source = { constructor: { polluted: true }, b: 2 } as Record<
251-
string,
252-
unknown
253-
>;
250+
const source: Record<string, unknown> = {
251+
constructor: { polluted: true },
252+
b: 2,
253+
};
254254

255255
// Act
256256
const result = deepMerge(target, source);
@@ -262,9 +262,10 @@ describe('Function: deepMerge', () => {
262262

263263
it('does not indirectly pollute via toString.constructor.prototype', () => {
264264
// Prepare & Act
265-
deepMerge({}, {
265+
const source: Record<string, unknown> = {
266266
toString: { constructor: { prototype: { polluted: true } } },
267-
} as Record<string, unknown>);
267+
};
268+
deepMerge({}, source);
268269

269270
// Assess
270271
expect('polluted' in Function.prototype).toBe(false);
@@ -282,7 +283,7 @@ describe('Function: deepMerge', () => {
282283

283284
// Assess
284285
expect(result).toEqual({ nested: { a: 1, b: 2 } });
285-
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
286+
expect('polluted' in {}).toBe(false);
286287
});
287288
});
288289

@@ -304,11 +305,9 @@ describe('Function: deepMerge', () => {
304305
it('handles deep circular references', () => {
305306
// Prepare
306307
const target = { a: 1 };
307-
const source: Record<string, unknown> = {
308-
b: 2,
309-
nested: { c: 3 },
310-
};
311-
(source.nested as Record<string, unknown>).circular = source;
308+
const nested: Record<string, unknown> = { c: 3 };
309+
const source: Record<string, unknown> = { b: 2, nested };
310+
nested.circular = source;
312311

313312
// Act
314313
const result = deepMerge(target, source);
@@ -328,10 +327,9 @@ describe('Function: deepMerge', () => {
328327
const result = deepMerge(target, source);
329328

330329
// Assess
331-
expect(result.a).toBe(1);
332-
expect(Array.isArray(result.arr)).toBe(true);
333-
expect((result.arr as unknown[])[0]).toBe(1);
334-
expect((result.arr as unknown[])[1]).toBe(2);
330+
expect(result).toHaveProperty('a', 1);
331+
expect(result).toHaveProperty('arr[0]', 1);
332+
expect(result).toHaveProperty('arr[1]', 2);
335333
});
336334

337335
it('skips array that references an ancestor array', () => {
@@ -346,10 +344,7 @@ describe('Function: deepMerge', () => {
346344
const result = deepMerge(target, source);
347345

348346
// Assess
349-
expect(result.arr).toBeDefined();
350-
expect((result.arr as Record<string, unknown>[])[0]).not.toHaveProperty(
351-
'backRef'
352-
);
347+
expect(result).toEqual({ arr: [{ a: 1 }] });
353348
});
354349

355350
it('skips circular plain objects inside arrays', () => {
@@ -399,14 +394,12 @@ describe('Function: deepMerge', () => {
399394
const result = deepMerge(target, source);
400395

401396
// Assess
402-
expect(result.prop).toEqual({ shared: true });
403-
expect((result.arr as Record<string, unknown>[])[0]).toEqual({
404-
a: 1,
405-
shared: true,
406-
});
407-
expect((result.arr as Record<string, unknown>[])[1]).toEqual({
408-
b: 2,
409-
c: 3,
397+
expect(result).toEqual({
398+
prop: { shared: true },
399+
arr: [
400+
{ a: 1, shared: true },
401+
{ b: 2, c: 3 },
402+
],
410403
});
411404
});
412405

@@ -465,14 +458,12 @@ describe('Function: deepMerge', () => {
465458
const result = deepMerge(target, source);
466459

467460
// Assess
468-
expect(result.prop).toEqual({ shared: true });
469-
expect((result.arr as Record<string, unknown>[])[0]).toEqual({
470-
a: 1,
471-
shared: true,
472-
});
473-
expect((result.arr as Record<string, unknown>[])[1]).toEqual({
474-
b: 2,
475-
c: 3,
461+
expect(result).toEqual({
462+
prop: { shared: true },
463+
arr: [
464+
{ a: 1, shared: true },
465+
{ b: 2, c: 3 },
466+
],
476467
});
477468
});
478469

@@ -674,7 +665,7 @@ describe('Function: deepMerge', () => {
674665

675666
// Assess
676667
expect(result).toEqual({ a: 1, b: 2 });
677-
expect((result as Record<symbol, unknown>)[sym]).toBeUndefined();
668+
expect(sym in result).toBe(false);
678669
});
679670

680671
it('handles numeric keys', () => {
@@ -718,21 +709,20 @@ describe('Function: deepMerge', () => {
718709

719710
// Assess
720711
expect(result.fn).toBe(fn);
721-
expect((result.fn as () => number)()).toBe(42);
722712
});
723713

724714
it('replaces function target value with object from later source', () => {
725715
// Prepare
726716
const fn = () => 42;
727-
const source1 = { a: fn } as Record<string, unknown>;
717+
const source1: Record<string, unknown> = { a: fn };
728718
const source2 = { a: { b: 2 } };
729719

730720
// Act
731721
const result = deepMerge({}, source1, source2);
732722

733723
// Assess
734724
expect(result).toEqual({ a: { b: 2 } });
735-
expect('b' in (source1.a as object)).toBe(false);
725+
expect(source1.a).toBe(fn);
736726
});
737727

738728
it('handles self-merge without infinite loop', () => {

0 commit comments

Comments
 (0)