Skip to content

Commit f4940ae

Browse files
wsmdSimenB
authored andcommitted
Improve error message format for Node's assert.fail (#9262)
1 parent 9ac2dcd commit f4940ae

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
- `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136))
7070
- `[pretty-format]` Correctly detect memoized elements ([#9196](https://github.com/facebook/jest/pull/9196))
7171
- `[jest-fake-timers]` Support `util.promisify` on `setTimeout` ([#9180](https://github.com/facebook/jest/pull/9180))
72+
- `[jest-jasmine2, jest-circus]` Improve error message format for Node's assert.fail ([#9262](https://github.com/facebook/jest/pull/9262))
7273

7374
### Chore & Maintenance
7475

e2e/__tests__/__snapshots__/failures.test.ts.snap

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ FAIL __tests__/assertionError.test.js
396396
✕ assert.doesNotThrow
397397
✕ assert.throws
398398
✕ async
399+
✕ assert.fail
400+
✕ assert.fail with a message
399401
400402
● assert
401403
@@ -772,8 +774,39 @@ FAIL __tests__/assertionError.test.js
772774
| ^
773775
81 | });
774776
82 |
777+
83 | test('assert.fail', () => {
775778
776779
at Object.equal (__tests__/assertionError.test.js:80:10)
780+
781+
assert.fail
782+
783+
assert.fail(received, expected)
784+
785+
82 |
786+
83 | test('assert.fail', () => {
787+
> 84 | assert.fail();
788+
| ^
789+
85 | });
790+
86 |
791+
87 | test('assert.fail with a message', () => {
792+
793+
at Object.fail (__tests__/assertionError.test.js:84:10)
794+
795+
assert.fail with a message
796+
797+
assert.fail(received, expected)
798+
799+
Message:
800+
error!
801+
802+
86 |
803+
87 | test('assert.fail with a message', () => {
804+
> 88 | assert.fail('error!');
805+
| ^
806+
89 | });
807+
90 |
808+
809+
at Object.fail (__tests__/assertionError.test.js:88:10)
777810
`;
778811
779812
exports[`works with snapshot failures 1`] = `

e2e/failures/__tests__/assertionError.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@ test('assert.throws', () => {
7979
test('async', async () => {
8080
assert.equal('hello\ngoodbye', 'hello', 'hmmm');
8181
});
82+
83+
test('assert.fail', () => {
84+
assert.fail();
85+
});
86+
87+
test('assert.fail with a message', () => {
88+
assert.fail('error!');
89+
});

packages/jest-circus/src/formatNodeAssertErrors.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ const getOperatorName = (operator: string | undefined, stack: string) => {
7676
if (stack.match('.throws')) {
7777
return 'throws';
7878
}
79+
// this fallback is only needed for versions older than node 10
80+
if (stack.match('.fail')) {
81+
return 'fail';
82+
}
7983
return '';
8084
};
8185

@@ -155,6 +159,14 @@ function assertionErrorMessage(
155159
);
156160
}
157161

162+
if (operatorName === 'fail') {
163+
return (
164+
buildHintString(assertMatcherHint(operator, operatorName, expected)) +
165+
chalk.reset(hasCustomMessage ? 'Message:\n ' + message : '') +
166+
trimmedStack
167+
);
168+
}
169+
158170
return (
159171
buildHintString(assertMatcherHint(operator, operatorName, expected)) +
160172
chalk.reset(`Expected value ${operatorMessage(operator)}`) +

packages/jest-jasmine2/src/assertionErrorMessage.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ const getOperatorName = (operator: string | null, stack: string) => {
4242
if (stack.match('.throws')) {
4343
return 'throws';
4444
}
45+
// this fallback is only needed for versions older than node 10
46+
if (stack.match('.fail')) {
47+
return 'fail';
48+
}
4549
return '';
4650
};
4751

@@ -121,6 +125,14 @@ function assertionErrorMessage(
121125
);
122126
}
123127

128+
if (operatorName === 'fail') {
129+
return (
130+
buildHintString(assertMatcherHint(operator, operatorName, expected)) +
131+
chalk.reset(hasCustomMessage ? 'Message:\n ' + message : '') +
132+
trimmedStack
133+
);
134+
}
135+
124136
return (
125137
buildHintString(assertMatcherHint(operator, operatorName, expected)) +
126138
chalk.reset(`Expected value ${operatorMessage(operator)}`) +

0 commit comments

Comments
 (0)