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

feat: support reporting AggregateErrors #5018

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
Empty file modified bin/mocha.js
100644 → 100755
CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Still the permissions change? Is there an automatic modification happening on save or something like that?

Copy link
Author

Choose a reason for hiding this comment

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

It's possible. I'll look into it this week.

Empty file.
3 changes: 2 additions & 1 deletion lib/cli/run-option-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const TYPES = (exports.types = {
'sort',
'watch'
],
number: ['retries', 'jobs'],
number: ['retries', 'jobs', 'jsonStringifyWhitespace'],
CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
string: [
'config',
'fgrep',
Expand Down Expand Up @@ -78,6 +78,7 @@ exports.aliases = {
ignore: ['exclude'],
invert: ['i'],
jobs: ['j'],
jsonStringifyWhitespace: ['jsw'],
CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
'no-colors': ['C'],
'node-option': ['n'],
parallel: ['p'],
Expand Down
30 changes: 28 additions & 2 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ exports.list = function (failures) {
if (test.err && test.err.multiple) {
if (multipleTest !== test) {
multipleTest = test;

CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
multipleErr = [test.err].concat(test.err.multiple);
}
err = multipleErr.shift();
Expand Down Expand Up @@ -301,11 +302,37 @@ exports.list = function (failures) {
}
testTitle += str;
});

CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
Base.consoleLog(fmt, i + 1, testTitle, msg, stack);

// Handles formatting and printing Aggregate errors
if (test.err.errors) {
for (const error of test.err.errors) {
const testTitle = addIndent(`Error ${error.message}`, 0);
const stack = addIndent(error.stack, 3);

Base.consoleLog(fmt, error.message, testTitle, '', stack);
CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
}
}
});
};

/**
* Adds indentation to a given text string.
*
* @param {string} text - The input text to be indented.
* @param {number} [level=1] - The number of levels of indentation. Default is 1.
* @param {string} [indentString=' '] - The string used for one level of indentation. Default is two spaces.
* @returns {string} The indented text.
*/
function addIndent(text, level = 1, indentString = ' ') {
const indent = indentString.repeat(level);
const indentedText = text
.split('\n')
.map(line => `${indent}${line}`)
.join('\n');
return indentedText;
}

/**
* Constructs a new `Base` reporter instance.
*
Expand Down Expand Up @@ -544,7 +571,6 @@ var objToString = Object.prototype.toString;
function sameType(a, b) {
return objToString.call(a) === objToString.call(b);
}

CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
Base.consoleLog = consoleLog;

Base.abstract = true;
3 changes: 2 additions & 1 deletion lib/reporters/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function JSONReporter(runner, options = {}) {

runner.testResults = obj;

var json = JSON.stringify(obj, null, 2);
var json = JSON.stringify(obj, null, options.jsonStringifyWhitespace || 2);
if (output) {
try {
fs.mkdirSync(path.dirname(output), {recursive: true});
Expand Down Expand Up @@ -153,6 +153,7 @@ function cleanCycles(obj) {
*/
function errorJSON(err) {
var res = {};

CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved
Object.getOwnPropertyNames(err).forEach(function (key) {
res[key] = err[key];
}, err);
Expand Down
4 changes: 0 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 26 additions & 13 deletions test/reporters/base.spec.js
Copy link
Member

Choose a reason for hiding this comment

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

@CheadleCheadle there are tests failing, could you please take a look?

Note that I just pulled the latest changes in from main (renamed from master). They didn't fix the test failure.

Original file line number Diff line number Diff line change
Expand Up @@ -507,21 +507,34 @@ describe('Base reporter', function () {
);
});

describe('when reporter output immune to user test changes', function () {
var baseConsoleLog;
it('should list all the errors within an AggregateError', function () {
var err1 = new Error('1');
var err2 = new Error('2');
var aggErr = new AggregateError([err1, err2], '2 errors');
CheadleCheadle marked this conversation as resolved.
Show resolved Hide resolved

beforeEach(function () {
sinon.restore();
sinon.stub(console, 'log');
baseConsoleLog = sinon.stub(Base, 'consoleLog');
});
var test = makeTest(aggErr);
list([test]);

it('should let you stub out console.log without effecting reporters output', function () {
Base.list([]);
baseConsoleLog.restore();
var errOut = stdout.join('\n').trim();

expect(baseConsoleLog, 'was called');
expect(console.log, 'was not called');
});
expect(errOut, 'to contain', '1) Error 1:', '2) Error 2:');
});
});

describe('when reporter output immune to user test changes', function () {
var baseConsoleLog;

beforeEach(function () {
sinon.restore();
sinon.stub(console, 'log');
baseConsoleLog = sinon.stub(Base, 'consoleLog');
});

it('should let you stub out console.log without effecting reporters output', function () {
Base.list([]);
baseConsoleLog.restore();

expect(baseConsoleLog, 'was called');
expect(console.log, 'was not called');
});
});