Skip to content

Commit

Permalink
add validation on snapshot name
Browse files Browse the repository at this point in the history
  • Loading branch information
bakamitai456 committed Jun 15, 2023
1 parent 7e7527a commit cbbec0a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
17 changes: 17 additions & 0 deletions e2e/__tests__/toMatchNamedSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ test('mark snapshots as obsolete in skipped tests if snapshot name does not matc
}
});

test('throws the error if snapshot name is not string', () => {
const filename = 'no-obsolete-if-skipped.test.js';
const template = makeTemplate(`
test('will be error', () => {
expect({a: 6}).toMatchNamedSnapshot(true);
});
`);

{
writeFiles(TESTS_DIR, {[filename]: template(['test.skip'])});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
console.log(stderr);
expect(stderr).toMatch('Expected snapshotName must be a string');
expect(exitCode).toBe(1);
}
});

test('accepts custom snapshot name', () => {
const filename = 'accept-custom-snapshot-name.test.js';
const template = makeTemplate(`test('accepts custom snapshot name', () => {
Expand Down
16 changes: 16 additions & 0 deletions e2e/__tests__/toThrowErrorMatchingNamedSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ test("throws the error if tested function didn't throw error", () => {
}
});

test('throws the error if snapshot name is not string', () => {
const filename = 'throws-if-tested-function-did-not-throw.test.js';
const template =
makeTemplate(`test('throws the error if snapshot name is not string', () => {
expect(() => { throw new Error('apple'); }).toThrowErrorMatchingNamedSnapshot(true);
});
`);

{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Expected snapshotName must be a string');
expect(exitCode).toBe(1);
}
});

test('accepts custom snapshot name', () => {
const filename = 'accept-custom-snapshot-name.test.js';
const template = makeTemplate(`test('accepts custom snapshot name', () => {
Expand Down
50 changes: 43 additions & 7 deletions packages/jest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,31 @@ export const toMatchSnapshot: MatcherFunctionWithContext<
export const toMatchNamedSnapshot: MatcherFunctionWithContext<
Context,
[snapshotName: string, properties?: object]
> = function (received, snapshotName, properties?) {
> = function (received: unknown, snapshotName: unknown, properties?: unknown) {
const matcherName = 'toMatchNamedSnapshot';

if (typeof snapshotName !== 'string') {
const options: MatcherHintOptions = {
isNot: this.isNot,
promise: this.promise,
};
const printedWithType = printWithType(
'Expected snapshotName',
snapshotName,
printExpected,
);

throw new Error(
matcherErrorMessage(
matcherHint(matcherName, undefined, PROPERTIES_ARG, options),
`Expected ${EXPECTED_COLOR('snapshotName')} must be a string`,
printedWithType,
),
);
}

if (properties !== undefined) {
if (
Array.isArray(properties) ||
typeof properties !== 'object' ||
properties === null
) {
if (typeof properties !== 'object' || properties === null) {
const options: MatcherHintOptions = {
isNot: this.isNot,
promise: this.promise,
Expand Down Expand Up @@ -530,9 +546,29 @@ export const toThrowErrorMatchingInlineSnapshot: MatcherFunctionWithContext<
export const toThrowErrorMatchingNamedSnapshot: MatcherFunctionWithContext<
Context,
[snapshotName: string, fromPromise?: boolean]
> = function (received, snapshotName, fromPromise) {
> = function (received: unknown, snapshotName: unknown, fromPromise: unknown) {
const matcherName = 'toThrowErrorMatchingNamedSnapshot';

if (typeof snapshotName !== 'string') {
const options: MatcherHintOptions = {
isNot: this.isNot,
promise: this.promise,
};
const printedWithType = printWithType(
'Expected snapshotName',
snapshotName,
printExpected,
);

throw new Error(
matcherErrorMessage(
matcherHint(matcherName, undefined, PROPERTIES_ARG, options),
`Expected ${EXPECTED_COLOR('snapshotName')} must be a string`,
printedWithType,
),
);
}

return _toThrowErrorMatchingSnapshot(
{
context: this,
Expand Down

0 comments on commit cbbec0a

Please sign in to comment.