Skip to content

Commit

Permalink
fix(snapshot): throw useful error when an array is passed as property…
Browse files Browse the repository at this point in the history
… matchers (#13263)
  • Loading branch information
SimenB authored Sep 14, 2022
1 parent 8ef4d86 commit c68f8ca
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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`] = `
<d>expect(</><r>received</><d>).</>toMatchInlineSnapshot<d>(</><g>properties</><d>, </>snapshot<d>)</>

<b>Matcher error</>: Expected <g>properties</> must be an object

Expected properties has type: array
Expected properties has value: <g>[]</>
`;

exports[`matcher error toMatchInlineSnapshot Expected properties must be an object (non-null) without snapshot 1`] = `
<d>expect(</><r>received</><d>).</>toMatchInlineSnapshot<d>(</><g>properties</><d>)</>

Expand Down Expand Up @@ -32,6 +41,15 @@ exports[`matcher error toMatchInlineSnapshot Snapshot matchers cannot be used wi
<b>Matcher error</>: Snapshot matchers cannot be used with <b>not</>
`;

exports[`matcher error toMatchSnapshot Expected properties must be an object (array) 1`] = `
<d>expect(</><r>received</><d>).</>toMatchSnapshot<d>(</><g>properties</><d>)</>

<b>Matcher error</>: Expected <g>properties</> must be an object

Expected properties has type: array
Expected properties has value: <g>[]</>
`;

exports[`matcher error toMatchSnapshot Expected properties must be an object (non-null) 1`] = `
<d>expect(</><r>received</><d>).</>toMatchSnapshot<d>(</><g>properties</><d>)</>

Expand Down
25 changes: 25 additions & 0 deletions packages/jest-snapshot/src/__tests__/printSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> = [];
const snapshot = '';

expect(() => {
toMatchInlineSnapshot.call(context, received, properties, snapshot);
}).toThrowErrorMatchingSnapshot();
});

test('Inline snapshot must be a string', () => {
const context = {
isNot: false,
Expand Down Expand Up @@ -312,6 +325,18 @@ describe('matcher error', () => {
}).toThrowErrorMatchingSnapshot();
});

test('Expected properties must be an object (array)', () => {
const context = {
isNot: false,
promise: '',
};
const properties: Array<unknown> = [];

expect(() => {
toMatchSnapshot.call(context, received, properties);
}).toThrowErrorMatchingSnapshot();
});

describe('received value must be an object', () => {
const context = {
currentTestName: '',
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -231,6 +235,7 @@ export const toMatchInlineSnapshot: MatcherFunctionWithContext<
}

if (
Array.isArray(propertiesOrSnapshot) ||
typeof propertiesOrSnapshot !== 'object' ||
propertiesOrSnapshot === null
) {
Expand Down

0 comments on commit c68f8ca

Please sign in to comment.