Skip to content

Commit b42f062

Browse files
committed
Always print actual or expected assertion value
This is especially useful for `t.notThrows()`, which includes the thrown exception as the `actual` value for the assertion error. Fixes #1150.
1 parent b9ef97a commit b42f062

File tree

2 files changed

+73
-35
lines changed

2 files changed

+73
-35
lines changed

lib/format-assert-error.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,55 @@ module.exports = error => {
3434
.join('\n\n') + '\n';
3535
}
3636

37-
if (error.actual && error.expected) {
38-
if (error.actual.type === error.expected.type) {
39-
const type = error.actual.type;
40-
if (type === 'array' || type === 'object') {
41-
const patch = diff.createPatch('string', error.actual.formatted, error.expected.formatted);
42-
const msg = patch
43-
.split('\n')
44-
.slice(4)
45-
.map(cleanUp)
46-
.filter(Boolean)
47-
.join('\n')
48-
.trimRight();
37+
if (error.actual && error.expected && error.actual.type === error.expected.type) {
38+
const type = error.actual.type;
39+
if (type === 'array' || type === 'object') {
40+
const patch = diff.createPatch('string', error.actual.formatted, error.expected.formatted);
41+
const msg = patch
42+
.split('\n')
43+
.slice(4)
44+
.map(cleanUp)
45+
.filter(Boolean)
46+
.join('\n')
47+
.trimRight();
4948

50-
return `Difference:\n\n${msg}\n`;
51-
}
49+
return `Difference:\n\n${msg}\n`;
50+
}
5251

53-
if (type === 'string') {
54-
const diffMatchPatch = new DiffMatchPatch();
55-
const patch = diffMatchPatch.diff_main(stripAnsi(error.actual.formatted), stripAnsi(error.expected.formatted));
56-
const msg = patch
57-
.map(part => {
58-
if (part[0] === 1) {
59-
return chalk.bgGreen.black(part[1]);
60-
}
52+
if (type === 'string') {
53+
const diffMatchPatch = new DiffMatchPatch();
54+
const patch = diffMatchPatch.diff_main(stripAnsi(error.actual.formatted), stripAnsi(error.expected.formatted));
55+
const msg = patch
56+
.map(part => {
57+
if (part[0] === 1) {
58+
return chalk.bgGreen.black(part[1]);
59+
}
6160

62-
if (part[0] === -1) {
63-
return chalk.bgRed.black(part[1]);
64-
}
61+
if (part[0] === -1) {
62+
return chalk.bgRed.black(part[1]);
63+
}
6564

66-
return chalk.red(part[1]);
67-
})
68-
.join('')
69-
.trimRight();
65+
return chalk.red(part[1]);
66+
})
67+
.join('')
68+
.trimRight();
7069

71-
return `Difference:\n\n${msg}\n`;
72-
}
70+
return `Difference:\n\n${msg}\n`;
7371
}
72+
}
7473

75-
return `Actual:\n\n${indentString(error.actual.formatted, 2).trimRight()}\n\n` +
76-
`Expected:\n\n${indentString(error.expected.formatted, 2).trimRight()}\n`;
74+
let retval = null;
75+
if (error.actual) {
76+
retval = `Actual:\n\n${indentString(error.actual.formatted, 2).trimRight()}\n`;
77+
}
78+
if (error.expected) {
79+
if (retval) {
80+
retval += '\n';
81+
} else {
82+
retval = '';
83+
}
84+
retval += `Expected:\n\n${indentString(error.expected.formatted, 2).trimRight()}\n`;
7785
}
7886

79-
return null;
87+
return retval;
8088
};

test/format-assert-error.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ test('diff strings', t => {
8989
t.end();
9090
});
9191

92-
test('diff different types', t => {
92+
test('print different types', t => {
9393
const err = {
9494
actual: {
9595
type: 'array',
@@ -109,3 +109,33 @@ test('diff different types', t => {
109109
].join('\n'));
110110
t.end();
111111
});
112+
113+
test('print actual even if no expected', t => {
114+
const err = {
115+
actual: {
116+
type: 'array',
117+
formatted: prettyFormat([1, 2, 3])
118+
}
119+
};
120+
121+
t.is(format(err), [
122+
'Actual:\n',
123+
`${indentString(err.actual.formatted, 2)}\n`
124+
].join('\n'));
125+
t.end();
126+
});
127+
128+
test('print expected even if no actual', t => {
129+
const err = {
130+
expected: {
131+
type: 'array',
132+
formatted: prettyFormat([1, 2, 3])
133+
}
134+
};
135+
136+
t.is(format(err), [
137+
'Expected:\n',
138+
`${indentString(err.expected.formatted, 2)}\n`
139+
].join('\n'));
140+
t.end();
141+
});

0 commit comments

Comments
 (0)