Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snapshot): throw useful error when an array is passed as property matchers #13263

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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