Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
bd8de10
Add toMatchNamedSnapshot
bakamitai456 Mar 31, 2023
c5fe519
add to match named snapshot to expect
bakamitai456 Mar 31, 2023
35bbdeb
add test
bakamitai456 Mar 31, 2023
6841acf
add e2e testing
bakamitai456 Apr 3, 2023
7dfe361
add to throw error matching named snapshot with e2e
bakamitai456 Apr 4, 2023
b684d88
add unit test named snapshot
bakamitai456 Apr 4, 2023
9445a2f
add e2e toMatchNamedSnapshot with retries
bakamitai456 Apr 4, 2023
6fb5b85
add snapshot file
bakamitai456 Apr 5, 2023
f28f669
add types test
bakamitai456 Apr 5, 2023
14cf63d
add e2e concurrent testing
bakamitai456 Apr 5, 2023
ab462ea
add document
bakamitai456 Apr 7, 2023
c76bf56
fix lint
bakamitai456 Apr 9, 2023
c6ca50e
fixes changelog
bakamitai456 Apr 9, 2023
198fe2e
fixes code style
bakamitai456 Apr 10, 2023
79d3d7d
required snapshot name
bakamitai456 Apr 10, 2023
6b1a6ed
fixes properties
bakamitai456 Apr 10, 2023
735e503
edit e2e testing
bakamitai456 Apr 10, 2023
ed4e605
add e2e type test
bakamitai456 Apr 10, 2023
e3dc8fc
add more e2e type test
bakamitai456 Apr 10, 2023
7e7527a
remove unused overload
bakamitai456 Apr 10, 2023
cbbec0a
add validation on snapshot name
bakamitai456 Apr 10, 2023
77ae436
support array in properties matcher
bakamitai456 Apr 10, 2023
b7b81a1
remove unused function
bakamitai456 Apr 10, 2023
6327019
Apply suggestions from code review
bakamitai456 Apr 10, 2023
58acde3
remove fromPromise type
bakamitai456 Apr 11, 2023
23d8053
update tip on snapshot testing document
bakamitai456 Apr 16, 2023
78ddf16
Fixes alphabetic order
bakamitai456 Apr 16, 2023
435bdc8
Fixes docs
bakamitai456 Apr 16, 2023
9fccf7d
Update docs/SnapshotTesting.md
bakamitai456 Apr 16, 2023
3037070
Throw error when duplicate snapshot name
bakamitai456 Jun 14, 2023
5e58009
fix test
bakamitai456 Jun 15, 2023
acb42b0
fix the test
bakamitai456 Jun 16, 2023
f397e96
Merge branch 'main' into add-match-named-snapshot
SimenB Jun 21, 2023
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
Prev Previous commit
Next Next commit
add validation on snapshot name
  • Loading branch information
bakamitai456 committed Jun 15, 2023
commit cbbec0a6b9791da3e6bc6678828182cca8165d19
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