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

have Base generate the strings for error messages immediately #3075

Merged
merged 3 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ exports.cursor = {
}
};

function showDiff (err) {
return err && err.showDiff !== false && sameType(err.actual, err.expected) && err.expected !== undefined;
}

function stringifyDiffObjs (err) {
if (!utils.isString(err.actual) || !utils.isString(err.expected)) {
err.actual = utils.stringify(err.actual);
err.expected = utils.stringify(err.expected);
}
}

/**
* Output the given `failures` as a list.
*
Expand Down Expand Up @@ -183,8 +194,6 @@ exports.list = function (failures) {
}
var stack = err.stack || message;
var index = message ? stack.indexOf(message) : -1;
var actual = err.actual;
var expected = err.expected;
var escape = true;

if (index === -1) {
Expand All @@ -201,13 +210,9 @@ exports.list = function (failures) {
msg = 'Uncaught ' + msg;
}
// explicitly show diff
if (err.showDiff !== false && sameType(actual, expected) && expected !== undefined) {
if (showDiff(err)) {
stringifyDiffObjs(err);
escape = false;
if (!(utils.isString(actual) && utils.isString(expected))) {
err.actual = actual = utils.stringify(actual);
err.expected = expected = utils.stringify(expected);
}

fmt = color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
var match = message.match(/^([^:]+): expected/);
msg = '\n ' + color('error message', match ? match[1] : msg);
Expand Down Expand Up @@ -292,6 +297,9 @@ function Base (runner) {
runner.on('fail', function (test, err) {
stats.failures = stats.failures || 0;
stats.failures++;
if (showDiff(err)) {
stringifyDiffObjs(err);
}
test.err = err;
failures.push(test);
});
Expand Down
22 changes: 22 additions & 0 deletions test/reporters/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ describe('List reporter', function () {

Base.cursor = cachedCursor;
});
it('should immediately construct fail strings', function () {
var actual = { a: 'actual' };
var expected = { a: 'expected' };
var test = {};
var checked = false;
var err;
runner.on = function (event, callback) {
if (!checked && event === 'fail') {
err = new Error('fake failure object with actual/expected');
err.actual = actual;
err.expected = expected;
err.showDiff = true;
callback(test, err);
checked = true;
}
};
List.call({epilogue: function () {}}, runner);

process.stdout.write = stdoutWrite;
expect(typeof err.actual).to.equal('string');
expect(typeof err.expected).to.equal('string');
});
});

describe('on end', function () {
Expand Down