Skip to content

TypeScript · testing

prefer-whole-object-assertion

eslint:prefer-whole-object-assertion

Collapse consecutive assertions on one object into a whole-object assertion so related mismatches are reported together.

Default
error
Fix
safe
Languages
typescript

Why

One whole-object assertion presents related expectations together and produces a complete structural diff.

Fix

Replace consecutive member assertions with one `toMatchObject` assertion.

Before / after

Executed by this rule’s unit tests.

Before

Do not split one object across assertions

src/user.test.ts · focus
expect(user.id).toBe(1);
expect(user.name).toBe('Ada');
Autofix output
src/user.test.ts
expect(user).toMatchObject({ id: 1, name: 'Ada' });

After

Assert the object once

src/user.test.ts · focus
expect(user).toMatchObject({ id: 1, name: 'Ada' });

Message IDs

assertArrayOncecombineAssertions

Formerly: strict-test-assertions