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: log full error object if default toString is not enough #814

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/reporters/flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require("lodash");
const BaseReporter = require("./base");
const helpers = require("./utils/helpers");
const icons = require("./utils/icons");
const { stringifyError } = require("../utils/logger");

module.exports = class FlatReporter extends BaseReporter {
constructor(...args) {
Expand Down Expand Up @@ -37,7 +38,7 @@ module.exports = class FlatReporter extends BaseReporter {
const icon = testCase.isFailed ? icons.FAIL : icons.RETRY;

this.informer.log(` ${testCase.browserId}`);
this.informer.log(` ${icon} ${testCase.error}`);
this.informer.log(` ${icon} ${stringifyError(testCase.error)}`);
});
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/reporters/plain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chalk = require("chalk");
const BaseReporter = require("./base");
const icons = require("./utils/icons");
const helpers = require("./utils/helpers");
const { stringifyError } = require("../utils/logger");

module.exports = class PlainReporter extends BaseReporter {
_logTestInfo(test, icon) {
Expand All @@ -14,7 +15,7 @@ module.exports = class PlainReporter extends BaseReporter {
const testInfo = helpers.getTestInfo(test);

this.informer.log(` in file ${testInfo.file}`);
this.informer.log(` ${chalk.red(testInfo.error)}`);
this.informer.log(` ${chalk.red(stringifyError(testInfo.error))}`);
}
}
};
9 changes: 9 additions & 0 deletions src/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ const withTimestampPrefix =
console[logFnName](`[${timestamp}]`, ...args);
};

const stringifyError = error => {
try {
return error.toString() === "[object Object]" ? JSON.stringify(error) : error;
} catch {
return error;
}
};
Comment on lines +12 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Предлагаю подстраховаться и сделать так:

return !error || typeof error === "object"
    ? JSON.stringify(error)
    : error;

typeof дает object как раз только на всякие null и объекты, а такая логика с toString не будет работать в редких кейсах, когда Symbol.toStringTag переопределен, или еще что-то странное прокинут в ошибку. Например, массив/дату/промис/...


module.exports = {
log: withTimestampPrefix("log"),
warn: withTimestampPrefix("warn"),
error: withTimestampPrefix("error"),
stringifyError,
};
Loading