Skip to content

Commit b1ef690

Browse files
author
Warit Kosolwattanasombat
committed
add validation on snapshot name
1 parent abda18b commit b1ef690

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
@@ -234,15 +234,31 @@ export const toMatchSnapshot: MatcherFunctionWithContext<
234234
export const toMatchNamedSnapshot: MatcherFunctionWithContext<
235235
Context,
236236
[snapshotName: string, properties?: object]
237-
> = function (received, snapshotName, properties?) {
237+
> = function (received: unknown, snapshotName: unknown, properties?: unknown) {
238238
const matcherName = 'toMatchNamedSnapshot';
239239

240+
if (typeof snapshotName !== 'string') {
241+
const options: MatcherHintOptions = {
242+
isNot: this.isNot,
243+
promise: this.promise,
244+
};
245+
const printedWithType = printWithType(
246+
'Expected snapshotName',
247+
snapshotName,
248+
printExpected,
249+
);
250+
251+
throw new Error(
252+
matcherErrorMessage(
253+
matcherHint(matcherName, undefined, PROPERTIES_ARG, options),
254+
`Expected ${EXPECTED_COLOR('snapshotName')} must be a string`,
255+
printedWithType,
256+
),
257+
);
258+
}
259+
240260
if (properties !== undefined) {
241-
if (
242-
Array.isArray(properties) ||
243-
typeof properties !== 'object' ||
244-
properties === null
245-
) {
261+
if (typeof properties !== 'object' || properties === null) {
246262
const options: MatcherHintOptions = {
247263
isNot: this.isNot,
248264
promise: this.promise,
@@ -535,9 +551,29 @@ export const toThrowErrorMatchingInlineSnapshot: MatcherFunctionWithContext<
535551
export const toThrowErrorMatchingNamedSnapshot: MatcherFunctionWithContext<
536552
Context,
537553
[snapshotName: string, fromPromise?: boolean]
538-
> = function (received, snapshotName, fromPromise) {
554+
> = function (received: unknown, snapshotName: unknown, fromPromise: unknown) {
539555
const matcherName = 'toThrowErrorMatchingNamedSnapshot';
540556

557+
if (typeof snapshotName !== 'string') {
558+
const options: MatcherHintOptions = {
559+
isNot: this.isNot,
560+
promise: this.promise,
561+
};
562+
const printedWithType = printWithType(
563+
'Expected snapshotName',
564+
snapshotName,
565+
printExpected,
566+
);
567+
568+
throw new Error(
569+
matcherErrorMessage(
570+
matcherHint(matcherName, undefined, PROPERTIES_ARG, options),
571+
`Expected ${EXPECTED_COLOR('snapshotName')} must be a string`,
572+
printedWithType,
573+
),
574+
);
575+
}
576+
541577
return _toThrowErrorMatchingSnapshot(
542578
{
543579
context: this,

0 commit comments

Comments
 (0)