Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adjusted assert.throws error format when thrown #13322

Merged
merged 3 commits into from Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@

### Fixes

- `[jest-circus, jest-jasmine2]` Fix error messages for Node's `assert.throes` ([#13322](https://github.com/facebook/jest/pull/13322))
- `[jest-haste-map]` Remove `__proto__` usage ([#13256](https://github.com/facebook/jest/pull/13256))
- `[jest-mock]` Improve `spyOn` typings to handle optional properties ([#13247](https://github.com/facebook/jest/pull/13247))
- `[jest-snapshot]` Throw useful error when an array is passed as property matchers ([#13263](https://github.com/facebook/jest/pull/13263))
Expand Down
91 changes: 67 additions & 24 deletions e2e/__tests__/__snapshots__/failures.test.ts.snap
Expand Up @@ -422,6 +422,8 @@ exports[`works with node assert 1`] = `
✕ assert.ifError
✕ assert.doesNotThrow
✕ assert.throws
✕ assert.throws with different error messages
✕ assert.throws with different error types
✕ async
✕ assert.fail
✕ assert.fail with a message
Expand Down Expand Up @@ -795,10 +797,51 @@ exports[`works with node assert 1`] = `
| ^
77 | });
78 |
79 | test('async', async () => {
79 | test('assert.throws with different error messages', () => {

at Object.throws (__tests__/assertionError.test.js:76:10)

● assert.throws with different error messages

assert.throws(function)

Expected values to be strictly deep-equal:
+ actual - expected

Comparison {
+ message: 'message 1'
- message: 'message 2'
}

78 |
79 | test('assert.throws with different error messages', () => {
> 80 | assert.throws(
| ^
81 | () => {
82 | throw new Error('message 1');
83 | },

at Object.throws (__tests__/assertionError.test.js:80:10)

● assert.throws with different error types

assert.throws(function)

The "TypeError" validation function is expected to return "true". Received TypeError: SyntaxError: message 1

89 |
90 | test('assert.throws with different error types', () => {
> 91 | assert.throws(() => {
| ^
92 | throw new SyntaxError('message 1');
93 | }, TypeError);
94 | });

at Object.throws (__tests__/assertionError.test.js:91:10)
Caught error:
SyntaxError: message 1
at Object.throws (__tests__/assertionError.test.js:91:10)

● async

assert.equal(received, expected)
Expand All @@ -820,29 +863,29 @@ exports[`works with node assert 1`] = `
hello
+ goodbye

78 |
79 | test('async', async () => {
> 80 | assert.equal('hello\\ngoodbye', 'hello', 'hmmm');
| ^
81 | });
82 |
83 | test('assert.fail', () => {
95 |
96 | test('async', async () => {
> 97 | assert.equal('hello\\ngoodbye', 'hello', 'hmmm');
| ^
98 | });
99 |
100 | test('assert.fail', () => {

at Object.equal (__tests__/assertionError.test.js:80:10)
at Object.equal (__tests__/assertionError.test.js:97:10)

● assert.fail

assert.fail(received, expected)

82 |
83 | test('assert.fail', () => {
> 84 | assert.fail();
| ^
85 | });
86 |
87 | test('assert.fail with a message', () => {
99 |
100 | test('assert.fail', () => {
> 101 | assert.fail();
| ^
102 | });
103 |
104 | test('assert.fail with a message', () => {

at Object.fail (__tests__/assertionError.test.js:84:10)
at Object.fail (__tests__/assertionError.test.js:101:10)

● assert.fail with a message

Expand All @@ -851,14 +894,14 @@ exports[`works with node assert 1`] = `
Message:
error!

86 |
87 | test('assert.fail with a message', () => {
> 88 | assert.fail('error!');
| ^
89 | });
90 |
103 |
104 | test('assert.fail with a message', () => {
> 105 | assert.fail('error!');
| ^
106 | });
107 |

at Object.fail (__tests__/assertionError.test.js:88:10)"
at Object.fail (__tests__/assertionError.test.js:105:10)"
`;

exports[`works with snapshot failures 1`] = `
Expand Down
17 changes: 17 additions & 0 deletions e2e/failures/__tests__/assertionError.test.js
Expand Up @@ -76,6 +76,23 @@ test('assert.throws', () => {
assert.throws(() => {});
});

test('assert.throws with different error messages', () => {
assert.throws(
() => {
throw new Error('message 1');
},
{
message: 'message 2',
},
);
});

test('assert.throws with different error types', () => {
assert.throws(() => {
throw new SyntaxError('message 1');
}, TypeError);
});

test('async', async () => {
assert.equal('hello\ngoodbye', 'hello', 'hmmm');
});
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-circus/src/formatNodeAssertErrors.ts
Expand Up @@ -150,6 +150,14 @@ function assertionErrorMessage(
}

if (operatorName === 'throws') {
if (error.generatedMessage) {
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset(error.message) +
chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +
trimmedStack
);
}
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset('Expected the function to throw an error.\n') +
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-jasmine2/src/assertionErrorMessage.ts
Expand Up @@ -111,6 +111,14 @@ function assertionErrorMessage(
}

if (operatorName === 'throws') {
if (error.generatedMessage) {
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset(error.message) +
chalk.reset(hasCustomMessage ? `\n\nMessage:\n ${message}` : '') +
trimmedStack
);
}
return (
buildHintString(assertThrowingMatcherHint(operatorName)) +
chalk.reset('Expected the function to throw an error.\n') +
Expand Down