Skip to content

Commit

Permalink
jest-snapshot: Remove report method and throw matcher errors (#9049)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pedrottimark authored Oct 21, 2019
1 parent bd48ccc commit 800ba64
Show file tree
Hide file tree
Showing 13 changed files with 1,823 additions and 1,011 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- `[jest-snapshot]` Remove only the added newlines in multiline snapshots ([#8859](https://github.com/facebook/jest/pull/8859))
- `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880))
- `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898))
- `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049))
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))

### Chore & Maintenance
Expand Down
10 changes: 5 additions & 5 deletions e2e/__tests__/toMatchSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ test('handles invalid property matchers', () => {
`,
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Property matchers must be an object.');
expect(stderr).toMatch('Expected properties must be an object');
expect(exitCode).toBe(1);
}
{
Expand All @@ -207,9 +207,9 @@ test('handles invalid property matchers', () => {
`,
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Property matchers must be an object.');
expect(stderr).toMatch('Expected properties must be an object');
expect(stderr).toMatch(
'To provide a snapshot test name without property matchers, use: toMatchSnapshot("name")',
`To provide a hint without properties: toMatchSnapshot('hint')`,
);
expect(exitCode).toBe(1);
}
Expand All @@ -221,9 +221,9 @@ test('handles invalid property matchers', () => {
`,
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Property matchers must be an object.');
expect(stderr).toMatch('Expected properties must be an object');
expect(stderr).toMatch(
'To provide a snapshot test name without property matchers, use: toMatchSnapshot("name")',
`To provide a hint without properties: toMatchSnapshot('hint')`,
);
expect(exitCode).toBe(1);
}
Expand Down
6 changes: 4 additions & 2 deletions e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ test('cannot be used with .not', () => {
const filename = 'cannot-be-used-with-not.test.js';
const template = makeTemplate(`
test('cannot be used with .not', () => {
expect('').not.toThrowErrorMatchingInlineSnapshot();
expect(() => { throw new Error('apple'); })
.not
.toThrowErrorMatchingInlineSnapshot();
});
`);

{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('.not cannot be used with snapshot matchers');
expect(stderr).toMatch('Snapshot matchers cannot be used with not');
expect(exitCode).toBe(1);
}
});
Expand Down
6 changes: 4 additions & 2 deletions e2e/__tests__/toThrowErrorMatchingSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ test('accepts custom snapshot name', () => {
test('cannot be used with .not', () => {
const filename = 'cannot-be-used-with-not.test.js';
const template = makeTemplate(`test('cannot be used with .not', () => {
expect('').not.toThrowErrorMatchingSnapshot();
expect(() => { throw new Error('apple'); })
.not
.toThrowErrorMatchingSnapshot();
});
`);

{
writeFiles(TESTS_DIR, {[filename]: template()});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('.not cannot be used with snapshot matchers');
expect(stderr).toMatch('Snapshot matchers cannot be used with not');
expect(exitCode).toBe(1);
}
});
Expand Down
2 changes: 1 addition & 1 deletion e2e/snapshot/__tests__/snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('snapshot', () => {

it('cannot be used with .not', () => {
expect(() => expect('').not.toMatchSnapshot()).toThrow(
'.not cannot be used with snapshot matchers'
'Snapshot matchers cannot be used with not'
);
});

Expand Down
7 changes: 5 additions & 2 deletions packages/jest-matcher-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,11 @@ export const getLabelPrinter = (...strings: Array<string>): PrintLabel => {
export const matcherErrorMessage = (
hint: string, // assertion returned from call to matcherHint
generic: string, // condition which correct value must fulfill
specific: string, // incorrect value returned from call to printWithType
) => `${hint}\n\n${chalk.bold('Matcher error')}: ${generic}\n\n${specific}`;
specific?: string, // incorrect value returned from call to printWithType
) =>
`${hint}\n\n${chalk.bold('Matcher error')}: ${generic}${
typeof specific === 'string' ? '\n\n' + specific : ''
}`;

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

This file was deleted.

Loading

0 comments on commit 800ba64

Please sign in to comment.