Skip to content

Commit cbbec0a

Browse files
committed
add validation on snapshot name
1 parent 7e7527a commit cbbec0a

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

e2e/__tests__/toMatchNamedSnapshot.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ test('mark snapshots as obsolete in skipped tests if snapshot name does not matc
170170
}
171171
});
172172

173+
test('throws the error if snapshot name is not string', () => {
174+
const filename = 'no-obsolete-if-skipped.test.js';
175+
const template = makeTemplate(`
176+
test('will be error', () => {
177+
expect({a: 6}).toMatchNamedSnapshot(true);
178+
});
179+
`);
180+
181+
{
182+
writeFiles(TESTS_DIR, {[filename]: template(['test.skip'])});
183+
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
184+
console.log(stderr);
185+
expect(stderr).toMatch('Expected snapshotName must be a string');
186+
expect(exitCode).toBe(1);
187+
}
188+
});
189+
173190
test('accepts custom snapshot name', () => {
174191
const filename = 'accept-custom-snapshot-name.test.js';
175192
const template = makeTemplate(`test('accepts custom snapshot name', () => {

e2e/__tests__/toThrowErrorMatchingNamedSnapshot.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ test("throws the error if tested function didn't throw error", () => {
5252
}
5353
});
5454

55+
test('throws the error if snapshot name is not string', () => {
56+
const filename = 'throws-if-tested-function-did-not-throw.test.js';
57+
const template =
58+
makeTemplate(`test('throws the error if snapshot name is not string', () => {
59+
expect(() => { throw new Error('apple'); }).toThrowErrorMatchingNamedSnapshot(true);
60+
});
61+
`);
62+
63+
{
64+
writeFiles(TESTS_DIR, {[filename]: template()});
65+
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
66+
expect(stderr).toMatch('Expected snapshotName must be a string');
67+
expect(exitCode).toBe(1);
68+
}
69+
});
70+
5571
test('accepts custom snapshot name', () => {
5672
const filename = 'accept-custom-snapshot-name.test.js';
5773
const template = makeTemplate(`test('accepts custom snapshot name', () => {

packages/jest-snapshot/src/index.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,31 @@ export const toMatchSnapshot: MatcherFunctionWithContext<
230230
export const toMatchNamedSnapshot: MatcherFunctionWithContext<
231231
Context,
232232
[snapshotName: string, properties?: object]
233-
> = function (received, snapshotName, properties?) {
233+
> = function (received: unknown, snapshotName: unknown, properties?: unknown) {
234234
const matcherName = 'toMatchNamedSnapshot';
235235

236+
if (typeof snapshotName !== 'string') {
237+
const options: MatcherHintOptions = {
238+
isNot: this.isNot,
239+
promise: this.promise,
240+
};
241+
const printedWithType = printWithType(
242+
'Expected snapshotName',
243+
snapshotName,
244+
printExpected,
245+
);
246+
247+
throw new Error(
248+
matcherErrorMessage(
249+
matcherHint(matcherName, undefined, PROPERTIES_ARG, options),
250+
`Expected ${EXPECTED_COLOR('snapshotName')} must be a string`,
251+
printedWithType,
252+
),
253+
);
254+
}
255+
236256
if (properties !== undefined) {
237-
if (
238-
Array.isArray(properties) ||
239-
typeof properties !== 'object' ||
240-
properties === null
241-
) {
257+
if (typeof properties !== 'object' || properties === null) {
242258
const options: MatcherHintOptions = {
243259
isNot: this.isNot,
244260
promise: this.promise,
@@ -530,9 +546,29 @@ export const toThrowErrorMatchingInlineSnapshot: MatcherFunctionWithContext<
530546
export const toThrowErrorMatchingNamedSnapshot: MatcherFunctionWithContext<
531547
Context,
532548
[snapshotName: string, fromPromise?: boolean]
533-
> = function (received, snapshotName, fromPromise) {
549+
> = function (received: unknown, snapshotName: unknown, fromPromise: unknown) {
534550
const matcherName = 'toThrowErrorMatchingNamedSnapshot';
535551

552+
if (typeof snapshotName !== 'string') {
553+
const options: MatcherHintOptions = {
554+
isNot: this.isNot,
555+
promise: this.promise,
556+
};
557+
const printedWithType = printWithType(
558+
'Expected snapshotName',
559+
snapshotName,
560+
printExpected,
561+
);
562+
563+
throw new Error(
564+
matcherErrorMessage(
565+
matcherHint(matcherName, undefined, PROPERTIES_ARG, options),
566+
`Expected ${EXPECTED_COLOR('snapshotName')} must be a string`,
567+
printedWithType,
568+
),
569+
);
570+
}
571+
536572
return _toThrowErrorMatchingSnapshot(
537573
{
538574
context: this,

0 commit comments

Comments
 (0)