Skip to content

Commit 800ba64

Browse files
authored
jest-snapshot: Remove report method and throw matcher errors (#9049)
* jest-snapshot: Remove report method and throw matcher errors * Improve condition for snapshot state not initialized * Update CHANGELOG.md * Snapshot matchers cannot be used with not * Add BREAKING to CHANGELOG.md * Distinguish color of expected arguments with isUpdatable * Fix received or snapshot in some toThrowError tests
1 parent bd48ccc commit 800ba64

File tree

13 files changed

+1823
-1011
lines changed

13 files changed

+1823
-1011
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
- `[jest-snapshot]` Remove only the added newlines in multiline snapshots ([#8859](https://github.com/facebook/jest/pull/8859))
4040
- `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880))
4141
- `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898))
42+
- `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049))
4243
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))
4344

4445
### Chore & Maintenance

e2e/__tests__/toMatchSnapshot.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ test('handles invalid property matchers', () => {
196196
`,
197197
});
198198
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
199-
expect(stderr).toMatch('Property matchers must be an object.');
199+
expect(stderr).toMatch('Expected properties must be an object');
200200
expect(exitCode).toBe(1);
201201
}
202202
{
@@ -207,9 +207,9 @@ test('handles invalid property matchers', () => {
207207
`,
208208
});
209209
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
210-
expect(stderr).toMatch('Property matchers must be an object.');
210+
expect(stderr).toMatch('Expected properties must be an object');
211211
expect(stderr).toMatch(
212-
'To provide a snapshot test name without property matchers, use: toMatchSnapshot("name")',
212+
`To provide a hint without properties: toMatchSnapshot('hint')`,
213213
);
214214
expect(exitCode).toBe(1);
215215
}
@@ -221,9 +221,9 @@ test('handles invalid property matchers', () => {
221221
`,
222222
});
223223
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
224-
expect(stderr).toMatch('Property matchers must be an object.');
224+
expect(stderr).toMatch('Expected properties must be an object');
225225
expect(stderr).toMatch(
226-
'To provide a snapshot test name without property matchers, use: toMatchSnapshot("name")',
226+
`To provide a hint without properties: toMatchSnapshot('hint')`,
227227
);
228228
expect(exitCode).toBe(1);
229229
}

e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ test('cannot be used with .not', () => {
7474
const filename = 'cannot-be-used-with-not.test.js';
7575
const template = makeTemplate(`
7676
test('cannot be used with .not', () => {
77-
expect('').not.toThrowErrorMatchingInlineSnapshot();
77+
expect(() => { throw new Error('apple'); })
78+
.not
79+
.toThrowErrorMatchingInlineSnapshot();
7880
});
7981
`);
8082

8183
{
8284
writeFiles(TESTS_DIR, {[filename]: template()});
8385
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
84-
expect(stderr).toMatch('.not cannot be used with snapshot matchers');
86+
expect(stderr).toMatch('Snapshot matchers cannot be used with not');
8587
expect(exitCode).toBe(1);
8688
}
8789
});

e2e/__tests__/toThrowErrorMatchingSnapshot.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ test('accepts custom snapshot name', () => {
6767
test('cannot be used with .not', () => {
6868
const filename = 'cannot-be-used-with-not.test.js';
6969
const template = makeTemplate(`test('cannot be used with .not', () => {
70-
expect('').not.toThrowErrorMatchingSnapshot();
70+
expect(() => { throw new Error('apple'); })
71+
.not
72+
.toThrowErrorMatchingSnapshot();
7173
});
7274
`);
7375

7476
{
7577
writeFiles(TESTS_DIR, {[filename]: template()});
7678
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
77-
expect(stderr).toMatch('.not cannot be used with snapshot matchers');
79+
expect(stderr).toMatch('Snapshot matchers cannot be used with not');
7880
expect(exitCode).toBe(1);
7981
}
8082
});

e2e/snapshot/__tests__/snapshot.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('snapshot', () => {
3030

3131
it('cannot be used with .not', () => {
3232
expect(() => expect('').not.toMatchSnapshot()).toThrow(
33-
'.not cannot be used with snapshot matchers'
33+
'Snapshot matchers cannot be used with not'
3434
);
3535
});
3636

packages/jest-matcher-utils/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,11 @@ export const getLabelPrinter = (...strings: Array<string>): PrintLabel => {
415415
export const matcherErrorMessage = (
416416
hint: string, // assertion returned from call to matcherHint
417417
generic: string, // condition which correct value must fulfill
418-
specific: string, // incorrect value returned from call to printWithType
419-
) => `${hint}\n\n${chalk.bold('Matcher error')}: ${generic}\n\n${specific}`;
418+
specific?: string, // incorrect value returned from call to printWithType
419+
) =>
420+
`${hint}\n\n${chalk.bold('Matcher error')}: ${generic}${
421+
typeof specific === 'string' ? '\n\n' + specific : ''
422+
}`;
420423

421424
// Display assertion for the report when a test fails.
422425
// New format: rejects/resolves, not, and matcher name have black color

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

Lines changed: 0 additions & 295 deletions
This file was deleted.

0 commit comments

Comments
 (0)