diff --git a/CHANGELOG.md b/CHANGELOG.md index c03a2274fc3e..88cfba26a809 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `[jest-haste-map]` Remove `__proto__` usage ([#13256](https://github.com/facebook/jest/pull/13256)) - `[jest-mock]` Improve `spyOn` typings to handle optional properties ([#13247](https://github.com/facebook/jest/pull/13247)) +- `[jest-snapshot]` Throw useful error when an array is passed as property matchers ([#13263](https://github.com/facebook/jest/pull/13263)) ### Chore & Maintenance diff --git a/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap b/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap index 0a6ee670fc5e..4fa135cd940a 100644 --- a/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap +++ b/packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap @@ -1,5 +1,14 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`matcher error toMatchInlineSnapshot Expected properties must be an object (array) with snapshot 1`] = ` +expect(received).toMatchInlineSnapshot(properties, snapshot) + +Matcher error: Expected properties must be an object + +Expected properties has type: array +Expected properties has value: [] +`; + exports[`matcher error toMatchInlineSnapshot Expected properties must be an object (non-null) without snapshot 1`] = ` expect(received).toMatchInlineSnapshot(properties) @@ -32,6 +41,15 @@ exports[`matcher error toMatchInlineSnapshot Snapshot matchers cannot be used wi Matcher error: Snapshot matchers cannot be used with not `; +exports[`matcher error toMatchSnapshot Expected properties must be an object (array) 1`] = ` +expect(received).toMatchSnapshot(properties) + +Matcher error: Expected properties must be an object + +Expected properties has type: array +Expected properties has value: [] +`; + exports[`matcher error toMatchSnapshot Expected properties must be an object (non-null) 1`] = ` expect(received).toMatchSnapshot(properties) diff --git a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts index ada1cf671ea2..37a9760a6fcf 100644 --- a/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts +++ b/packages/jest-snapshot/src/__tests__/printSnapshot.test.ts @@ -241,6 +241,19 @@ describe('matcher error', () => { }).toThrowErrorMatchingSnapshot(); }); + test('Expected properties must be an object (array) with snapshot', () => { + const context = { + isNot: false, + promise: '', + }; + const properties: Array = []; + const snapshot = ''; + + expect(() => { + toMatchInlineSnapshot.call(context, received, properties, snapshot); + }).toThrowErrorMatchingSnapshot(); + }); + test('Inline snapshot must be a string', () => { const context = { isNot: false, @@ -312,6 +325,18 @@ describe('matcher error', () => { }).toThrowErrorMatchingSnapshot(); }); + test('Expected properties must be an object (array)', () => { + const context = { + isNot: false, + promise: '', + }; + const properties: Array = []; + + expect(() => { + toMatchSnapshot.call(context, received, properties); + }).toThrowErrorMatchingSnapshot(); + }); + describe('received value must be an object', () => { const context = { currentTestName: '', diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 7cae97297d95..e5cb59da1534 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -164,7 +164,11 @@ export const toMatchSnapshot: MatcherFunctionWithContext< if (length === 2 && typeof propertiesOrHint === 'string') { hint = propertiesOrHint; } else if (length >= 2) { - if (typeof propertiesOrHint !== 'object' || propertiesOrHint === null) { + if ( + Array.isArray(propertiesOrHint) || + typeof propertiesOrHint !== 'object' || + propertiesOrHint === null + ) { const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, @@ -231,6 +235,7 @@ export const toMatchInlineSnapshot: MatcherFunctionWithContext< } if ( + Array.isArray(propertiesOrSnapshot) || typeof propertiesOrSnapshot !== 'object' || propertiesOrSnapshot === null ) {