Skip to content

Commit b472ca4

Browse files
committed
Add showStack option
1 parent 6803996 commit b472ca4

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

README.md

+41-5
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ Custom message [example](/example) with typescript
134134
- `expect(actual, message, options?)`
135135
- `actual`: The value you would normally pass into an `expect` to assert against with a given matcher.
136136
- `message`: String, the custom message you want to be printed should the `expect` fail.
137-
- `options`: An optional object that controls if the custom message prefix and/or if the original matcher message are shown.
138-
- type: `{ showPrefix: boolean, showMatcherMessage: boolean }`
139-
- Note: `options` is optional and so are both `showPrefix` and `showMatcherMessage` which will be defaulted to `true`.
137+
- `options`: An optional object that controls what is shown as part of the custom message.
138+
- `showPrefix: boolean`: If `false` will not show the `Custom message:` prefix. Default: `true`
139+
- `showMatcherMessage: boolean`: If `false` will not show the matchers original error message. Default: `true`
140+
- `showStack: boolean`: If `false` will not show the matchers stack trace. Default: `true`
140141

141142
```js
142143
test('returns 2 when adding 1 and 1', () => {
@@ -153,10 +154,15 @@ test('returns 2 when adding 1 and 1', () => {
153154
154155
Expected: 3
155156
Received: 2
157+
158+
1 | test('returns 2 when adding 1 and 1', () => {
159+
> 2 | expect(1 + 1, 'Woah this should be 2!').toBe(3);
160+
| ^
161+
3 | });
156162
*/
157163
```
158164

159-
### ShowPrefix false
165+
### showPrefix: `false`
160166

161167
```js
162168
test('returns 2 when adding 1 and 1', () => {
@@ -172,10 +178,15 @@ test('returns 2 when adding 1 and 1', () => {
172178
173179
Expected: 3
174180
Received: 2
181+
182+
1 | test('returns 2 when adding 1 and 1', () => {
183+
> 2 | expect(1 + 1, 'Woah this should be 2!', { showPrefix: false }).toBe(3);
184+
| ^
185+
3 | });
175186
*/
176187
```
177188

178-
### ShowMatcherMessage false
189+
### showMatcherMessage: `false`
179190

180191
```js
181192
test('returns 2 when adding 1 and 1', () => {
@@ -187,6 +198,31 @@ test('returns 2 when adding 1 and 1', () => {
187198
188199
Custom message:
189200
Woah this should be 2!
201+
202+
1 | test('returns 2 when adding 1 and 1', () => {
203+
> 2 | expect(1 + 1, 'Woah this should be 2!', { showMatcherMessage: false }).toBe(3);
204+
| ^
205+
3 | });
206+
*/
207+
```
208+
209+
### showStack: `false`
210+
211+
```js
212+
test('returns 2 when adding 1 and 1', () => {
213+
expect(1 + 1, 'Woah this should be 2!', { showStack: false }).toBe(3);
214+
});
215+
// ↓ ↓ ↓ ↓ ↓ ↓
216+
/*
217+
● returns 2 when adding 1 and 1
218+
219+
Custom message:
220+
Woah this should be 2!
221+
222+
expect(received).toBe(expected) // Object.is equality
223+
224+
Expected: 3
225+
Received: 2
190226
*/
191227
```
192228

src/withMessage.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ const wrapMatcher = (matcher, customMessage, config) => {
2929
const messagePrefix = config.showPrefix ? 'Custom message:\n ' : '';
3030

3131
const message = () => messagePrefix + customMessage + (config.showMatcherMessage ? '\n\n' + matcherMessage : '');
32+
const e = new JestAssertionError({ ...matcherResult, message }, newMatcher);
3233

33-
throw new JestAssertionError({ ...matcherResult, message }, newMatcher);
34+
if (!config.showStack) {
35+
e.stack = null;
36+
}
37+
38+
throw e;
3439
}
3540
};
3641
return newMatcher;
@@ -56,7 +61,8 @@ export default (expect) => {
5661
(actual, customMessage, options = {}) => {
5762
const config = {
5863
showMatcherMessage: typeof options.showMatcherMessage === 'boolean' ? options.showMatcherMessage : true,
59-
showPrefix: typeof options.showPrefix === 'boolean' ? options.showPrefix : true
64+
showPrefix: typeof options.showPrefix === 'boolean' ? options.showPrefix : true,
65+
showStack: typeof options.showStack === 'boolean' ? options.showStack : true
6066
};
6167
let matchers = expect(actual); // partially apply expect to get all matchers and chain them
6268
if (customMessage) {

src/withMessage.test.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe('withMessage()', () => {
158158
});
159159

160160
test('does not throw error with matcher message when `config.showMatcherMessage` is false', () => {
161-
expect.assertions(2);
161+
expect.assertions(3);
162162
const originalError = new Error('Boo');
163163
const matcherMessage = 'expected ACTUAL to not be ACTUAL';
164164
originalError.matcherResult = {
@@ -176,13 +176,14 @@ describe('withMessage()', () => {
176176
try {
177177
withMessage(expectMock)(ACTUAL, customMessage, { showMatcherMessage: false }).toBe(1);
178178
} catch (e) {
179+
expect(e.stack).not.toBe(null);
179180
expect(e.message).toInclude(customMessage);
180181
expect(e.message).not.toInclude(matcherMessage);
181182
}
182183
});
183184

184185
test('does not throw error with custom message prefix when `config.showPrefix` is false', () => {
185-
expect.assertions(3);
186+
expect.assertions(4);
186187
const originalError = new Error('Boo');
187188
const matcherMessage = 'expected ACTUAL to not be ACTUAL';
188189
originalError.matcherResult = {
@@ -200,14 +201,15 @@ describe('withMessage()', () => {
200201
try {
201202
withMessage(expectMock)(ACTUAL, customMessage, { showPrefix: false }).toBe(1);
202203
} catch (e) {
204+
expect(e.stack).not.toBe(null);
203205
expect(e.message).not.toInclude('Custom message:\n');
204206
expect(e.message).toInclude(customMessage);
205207
expect(e.message).toInclude(matcherMessage);
206208
}
207209
});
208210

209211
test('throws error containing only custom message prefix when `config.showPrefix` and `config.showMatcherMessage` are false', () => {
210-
expect.assertions(3);
212+
expect.assertions(4);
211213
const originalError = new Error('Boo');
212214
const matcherMessage = 'expected ACTUAL to not be ACTUAL';
213215
originalError.matcherResult = {
@@ -225,9 +227,36 @@ describe('withMessage()', () => {
225227
try {
226228
withMessage(expectMock)(ACTUAL, customMessage, { showPrefix: false, showMatcherMessage: false }).toBe(1);
227229
} catch (e) {
230+
expect(e.stack).not.toBe(null);
228231
expect(e.message).not.toInclude('Custom message:\n');
229232
expect(e.message).not.toInclude(matcherMessage);
230233
expect(e.message).toInclude(customMessage);
231234
}
232235
});
236+
237+
test('throws error with empty stack when `config.showStack` is false', () => {
238+
expect.assertions(4);
239+
const originalError = new Error('Boo');
240+
const matcherMessage = 'expected ACTUAL to not be ACTUAL';
241+
originalError.matcherResult = {
242+
actual: ACTUAL,
243+
expected: 1,
244+
message: matcherMessage,
245+
pass: false
246+
};
247+
const toBeMock = jest.fn(() => {
248+
throw originalError;
249+
});
250+
const expectMock = jest.fn(() => ({ toBe: toBeMock }));
251+
252+
const customMessage = 'should fail';
253+
try {
254+
withMessage(expectMock)(ACTUAL, customMessage, { showStack: false }).toBe(1);
255+
} catch (e) {
256+
expect(e.stack).toBe(null);
257+
expect(e.message).toInclude('Custom message:\n');
258+
expect(e.message).toInclude(matcherMessage);
259+
expect(e.message).toInclude(customMessage);
260+
}
261+
});
233262
});

types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ declare namespace jest {
33
<T = any>(
44
actual: T,
55
message?: string,
6-
options?: { showMatcherMessage?: boolean; showPrefix?: boolean }
6+
options?: { showMatcherMessage?: boolean; showPrefix?: boolean; showStack?: boolean }
77
): Matchers<T>;
88
}
99
}

0 commit comments

Comments
 (0)