Skip to content

Commit 79d3d7d

Browse files
committed
required snapshot name
1 parent 198fe2e commit 79d3d7d

File tree

7 files changed

+50
-69
lines changed

7 files changed

+50
-69
lines changed

packages/jest-snapshot/__typetests__/matchers.test.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ expectError(toMatchInlineSnapshot({received: 'value'}));
8282

8383
// toMatchNamedSnapshot
8484

85-
expectType<ExpectationResult>(
86-
toMatchNamedSnapshot.call({} as Context, {received: 'value'}),
87-
);
88-
89-
expectType<ExpectationResult>(
85+
expectError<ExpectationResult>(
9086
toMatchNamedSnapshot.call({} as Context, {received: 'value'}),
9187
);
9288

@@ -98,7 +94,7 @@ expectType<ExpectationResult>(
9894
),
9995
);
10096

101-
expectType<ExpectationResult>(
97+
expectError<ExpectationResult>(
10298
toMatchNamedSnapshot.call(
10399
{} as Context,
104100
{received: 'value'},
@@ -110,8 +106,8 @@ expectType<ExpectationResult>(
110106
toMatchNamedSnapshot.call(
111107
{} as Context,
112108
{received: 'value'},
113-
{property: 'match'},
114109
'snapshot name',
110+
{property: 'match'},
115111
),
116112
);
117113

@@ -187,7 +183,7 @@ expectError(toThrowErrorMatchingInlineSnapshot({received: 'value'}));
187183

188184
// toThrowErrorMatchingNamedSnapshot
189185

190-
expectType<ExpectationResult>(
186+
expectError<ExpectationResult>(
191187
toThrowErrorMatchingNamedSnapshot.call({} as Context, new Error('received')),
192188
);
193189

@@ -208,13 +204,4 @@ expectType<ExpectationResult>(
208204
),
209205
);
210206

211-
expectType<ExpectationResult>(
212-
toThrowErrorMatchingNamedSnapshot.call(
213-
{} as Context,
214-
new Error('received'),
215-
undefined,
216-
false, // fromPromise
217-
),
218-
);
219-
220207
expectError(toThrowErrorMatchingSnapshot({received: 'value'}));

packages/jest-snapshot/src/__tests__/__snapshots__/printSnapshot.test.ts.snap

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`change text value 1`] = `
4-
<d>expect(</><t>received</><d>).</>toMatchSnapshot<d>(</>properties<d>, </><b>hint</><d>)</>
4+
<d>expect(</><t>received</><d>).</>toMatchNamedSnapshot<d>(</>properties<d>)</>
55

6-
Snapshot name: \`with properties: <b>change text value</> 1\`
6+
Snapshot name: \`with properties 1\`
77

88
<m>- Snapshot - 1</>
99
<t>+ Received + 1</>
@@ -58,21 +58,19 @@ Expected properties has value: <g>[Function]</>
5858
`;
5959

6060
exports[`matcher error toMatchNamedSnapshot Expected properties must be an object (null) with snapshot name 1`] = `
61-
<d>expect(</><r>received</><d>).</>toMatchNamedSnapshot<d>(</><g>properties</><d>, </><b>snapshotName</><d>)</>
62-
63-
<b>Matcher error</>: Expected <g>properties</> must be an object
61+
<d>expect(</><r>received</><d>).</>toMatchNamedSnapshot<d>(</><g>properties</><d>)</>
6462

65-
Expected properties has value: <g>null</>
63+
Snapshot state must be initialized
6664

67-
To provide a snapshot name without properties: toMatchNamedSnapshot('snapshotName')
65+
Snapshot state has value: undefined
6866
`;
6967

7068
exports[`matcher error toMatchNamedSnapshot Expected properties must be an object (null) without snapshot name 1`] = `
7169
<d>expect(</><r>received</><d>).</>toMatchNamedSnapshot<d>(</><g>properties</><d>)</>
7270

73-
<b>Matcher error</>: Expected <g>properties</> must be an object
71+
Snapshot state must be initialized
7472

75-
Expected properties has value: <g>null</>
73+
Snapshot state has value: undefined
7674
`;
7775

7876
exports[`matcher error toMatchNamedSnapshot Snapshot state must be initialized 1`] = `

packages/jest-snapshot/src/__tests__/matcher.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {type Context, toMatchSnapshot} from '../';
8+
import {Context, toMatchSnapshot} from '../';
99

1010
test('returns matcher name, expected and actual values', () => {
1111
const mockedContext = {

packages/jest-snapshot/src/__tests__/printSnapshot.test.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,11 @@ describe('matcher error', () => {
289289
promise: '',
290290
} as Context;
291291
const properties = () => {};
292+
const snapshotName =
293+
'toMatchNamedSnapshot Expected properties must be an object (non-null)';
292294

293295
expect(() => {
294-
toMatchNamedSnapshot.call(context, received, properties);
296+
toMatchNamedSnapshot.call(context, received, snapshotName, properties);
295297
}).toThrowErrorMatchingSnapshot();
296298
});
297299

@@ -305,7 +307,7 @@ describe('matcher error', () => {
305307

306308
expect(() => {
307309
// @ts-expect-error: Testing runtime error
308-
toMatchNamedSnapshot.call(context, received, properties, snapshotName);
310+
toMatchNamedSnapshot.call(context, received, snapshotName, properties);
309311
}).toThrowErrorMatchingSnapshot();
310312
});
311313

@@ -315,10 +317,12 @@ describe('matcher error', () => {
315317
promise: '',
316318
} as Context;
317319
const properties = null;
320+
const snapshotName =
321+
'toMatchNamedSnapshot Expected properties must be an object (null) without snapshot name';
318322

319323
expect(() => {
320324
// @ts-expect-error: Testing runtime error
321-
toMatchNamedSnapshot.call(context, received, properties);
325+
toMatchNamedSnapshot.call(context, received, snapshotName, properties);
322326
}).toThrowErrorMatchingSnapshot();
323327
});
324328

@@ -328,9 +332,11 @@ describe('matcher error', () => {
328332
promise: '',
329333
} as Context;
330334
const properties: Array<unknown> = [];
335+
const snapshotName =
336+
'toMatchNamedSnapshot Expected properties must be an object (null) without snapshot name';
331337

332338
expect(() => {
333-
toMatchNamedSnapshot.call(context, received, properties);
339+
toMatchNamedSnapshot.call(context, received, snapshotName, properties);
334340
}).toThrowErrorMatchingSnapshot();
335341
});
336342

@@ -342,16 +348,25 @@ describe('matcher error', () => {
342348
snapshotState: {},
343349
} as Context;
344350
const properties = {};
351+
const snapshotName =
352+
'toMatchNamedSnapshot received value must be an object';
345353

346354
test('(non-null)', () => {
347355
expect(() => {
348-
toMatchNamedSnapshot.call(context, 'string', properties);
356+
toMatchNamedSnapshot.call(
357+
context,
358+
'string',
359+
snapshotName,
360+
properties,
361+
);
349362
}).toThrowErrorMatchingSnapshot();
350363
});
351364

352365
test('(null)', () => {
366+
const snapshotName = 'toMatchNamedSnapshot (null)';
367+
353368
expect(() => {
354-
toMatchNamedSnapshot.call(context, null, properties);
369+
toMatchNamedSnapshot.call(context, null, snapshotName, properties);
355370
}).toThrowErrorMatchingSnapshot();
356371
});
357372
});
@@ -831,8 +846,8 @@ describe('pass false', () => {
831846
const {message, pass} = toMatchNamedSnapshot.call(
832847
context,
833848
new RangeError('Invalid array length'),
834-
{name: 'Error'},
835849
snapshotName,
850+
{name: 'Error'},
836851
) as SyncExpectationResult;
837852
expect(pass).toBe(false);
838853
expect(message()).toMatchNamedSnapshot(snapshotName);
@@ -849,8 +864,8 @@ describe('pass false', () => {
849864
const {message, pass} = toMatchNamedSnapshot.call(
850865
context,
851866
received,
852-
properties,
853867
snapshotName,
868+
properties,
854869
) as SyncExpectationResult;
855870
expect(pass).toBe(false);
856871
expect(message()).toMatchNamedSnapshot(snapshotName);
@@ -890,11 +905,11 @@ describe('pass false', () => {
890905
};
891906
const snapshotName = 'change text value';
892907

893-
const {message, pass} = toMatchSnapshot.call(
908+
const {message, pass} = toMatchNamedSnapshot.call(
894909
context,
895910
received,
896-
properties,
897911
snapshotName,
912+
properties,
898913
) as SyncExpectationResult;
899914
expect(pass).toBe(false);
900915
expect(message()).toMatchNamedSnapshot(snapshotName);

packages/jest-snapshot/src/__tests__/throwMatcher.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('throw matcher can take func', () => {
4747
() => {
4848
throw new Error('coconut');
4949
},
50-
undefined,
50+
'',
5151
false,
5252
);
5353

packages/jest-snapshot/src/index.ts

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -229,40 +229,26 @@ export const toMatchSnapshot: MatcherFunctionWithContext<
229229

230230
export const toMatchNamedSnapshot: MatcherFunctionWithContext<
231231
Context,
232-
[propertiesOrSnapshotName?: object | string, snapshotName?: string]
233-
> = function (received, propertiesOrSnapshotName, snapshotName) {
232+
[snapshotName: string, properties?: object]
233+
> = function (received, snapshotName, properties?) {
234234
const matcherName = 'toMatchNamedSnapshot';
235-
let properties;
236235

237-
const length = arguments.length;
238-
if (length === 2 && typeof propertiesOrSnapshotName === 'string') {
239-
snapshotName = propertiesOrSnapshotName;
240-
} else if (length >= 2) {
236+
if (properties) {
241237
if (
242-
Array.isArray(propertiesOrSnapshotName) ||
243-
typeof propertiesOrSnapshotName !== 'object' ||
244-
propertiesOrSnapshotName === null
238+
Array.isArray(properties) ||
239+
typeof properties !== 'object' ||
240+
properties === null
245241
) {
246242
const options: MatcherHintOptions = {
247243
isNot: this.isNot,
248244
promise: this.promise,
249245
};
250-
let printedWithType = printWithType(
246+
const printedWithType = printWithType(
251247
'Expected properties',
252-
propertiesOrSnapshotName,
248+
properties,
253249
printExpected,
254250
);
255251

256-
if (length === 3) {
257-
options.secondArgument = 'snapshotName';
258-
options.secondArgumentColor = BOLD_WEIGHT;
259-
260-
if (propertiesOrSnapshotName == null) {
261-
printedWithType +=
262-
"\n\nTo provide a snapshot name without properties: toMatchNamedSnapshot('snapshotName')";
263-
}
264-
}
265-
266252
throw new Error(
267253
matcherErrorMessage(
268254
matcherHint(matcherName, undefined, PROPERTIES_ARG, options),
@@ -271,11 +257,6 @@ export const toMatchNamedSnapshot: MatcherFunctionWithContext<
271257
),
272258
);
273259
}
274-
275-
// Future breaking change: Snapshot hint must be a string
276-
// if (arguments.length === 3 && typeof hint !== 'string') {}
277-
278-
properties = propertiesOrSnapshotName;
279260
}
280261

281262
return _toMatchSnapshot({
@@ -548,7 +529,7 @@ export const toThrowErrorMatchingInlineSnapshot: MatcherFunctionWithContext<
548529

549530
export const toThrowErrorMatchingNamedSnapshot: MatcherFunctionWithContext<
550531
Context,
551-
[snapshotName?: string, fromPromise?: boolean]
532+
[snapshotName: string, fromPromise?: boolean]
552533
> = function (received, snapshotName, fromPromise) {
553534
const matcherName = 'toThrowErrorMatchingNamedSnapshot';
554535

packages/jest-snapshot/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ export interface SnapshotMatchers<R extends void | Promise<void>, T> {
5858
* This ensures that a value matches the specific snapshot.
5959
* Instead of use current test name in global state, it will use the specific name to find the snapshot.
6060
*/
61-
toMatchNamedSnapshot(snapshotName?: string): R;
61+
toMatchNamedSnapshot(snapshotName: string): R;
6262
/**
6363
* This ensures that a value matches the specific snapshot with property matchers.
6464
* Instead of use current test name in global state, it will use the specific name to find the snapshot.
6565
*/
6666
toMatchNamedSnapshot<U extends Record<keyof T, unknown>>(
67+
snapshot: string,
6768
propertyMatchers: Partial<U>,
68-
snapshot?: string,
6969
): R;
7070
/**
7171
* This ensures that a value matches the most recent snapshot with property matchers.
@@ -95,7 +95,7 @@ export interface SnapshotMatchers<R extends void | Promise<void>, T> {
9595
* Used to test that a function throws a error matching the specific snapshot.
9696
* Instead of use current test name in global state, it will use the specific name to find the snapshot.
9797
*/
98-
toThrowErrorMatchingNamedSnapshot(snapshotName?: string): R;
98+
toThrowErrorMatchingNamedSnapshot(snapshotName: string): R;
9999
}
100100

101101
export type SnapshotFormat = Omit<PrettyFormatOptions, 'compareKeys'>;

0 commit comments

Comments
 (0)