-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Seperate diff generation code from Base reporter; closes #3011 #3201
Conversation
Thanks! @harrysarson Does this actually close #3011? It looks like this is just part of the solution, right? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where would you suggest the public API lives here?
Not exactly sure of the intent.
cc @segrey |
@boneskull yeah sorry this is not a complete solution to #3011. It was my intention to ask for feedback and about the desired shape of the public API. I think the exported functions |
@segrey I'm not sure this is really going to solve your issue, because Mocha is not the only place diffs come from. Some assertion libraries provide their own diffs, appended to the We may overhaul all of this. See chaijs/chai#1120 for more discussion. I think we should probably sit on #3011 unless @segrey is fine w/ supporting a temporary change. |
@harrysarson Thanks! AFAIU, a reporter can use this API in the following way: var BaseReporter = requireMochaModule('./lib/reporters/base');
var formatDiffs = requireMochaModule('./lib/formatDiffs');
function extractErrorDetails(err) {
var message = err.message, stack = err.stack;
var index = stack.indexOf(message);
if (index >= 0) {
message = stack.slice(0, index + message.length);
stack = stack.slice(message.length);
}
var diff;
if (BaseReporter.inlineDiffs) {
diff = formatDiffs.inlineDiff(err, {useColors: BaseReporter.useColors});
} else {
diff = formatDiffs.unifiedDiff(err, {useColors: BaseReporter.useColors});
}
return {
message : message,
diff: diff,
stack : stack
}
} Since mocha already does something similar in Base reporter (it extracts message, diff and stack from |
Anyway, I'm fine with a quick temporal solution too. Seems it can be smaller, like this one: // lib/reporters/base.js
exports.unifiedDiff = unifiedDiff;
exports.inlineDiff = inlineDiff; Then, IntelliJ reporter could use it in this way: var diff;
if (BaseReporter.inlineDiffs) {
diff = BaseReporter.inlineDiff(err);
} else {
diff = BaseReporter.unifiedDiff(err);
} |
Description of the Change
Moves the code used to generate "nice diffs" out of
lib/reporters/base.js
and into a new file which I have calledlib/formatDiffs.js
. This allows access to "nice diffs" by IDE's.To make this change I also had to move move code which deals with the coloring of mocha output out of
lib/reporters/base.js
as it is needed to generate the diffs. I put this intolib/colorUtils.js
.Alternate Designs
This is a first step towards exposing a public API, I have left function definitions as unchanged as possible but they should be changed if this API is to be exposed.
Benefits
Allows IDE to produce prettier output (#3011)
Possible Drawbacks
Doing
Base.colors = { /* ... */ }
will no longer change the colors used by mocha asBase.colors
is now a reference to the object defined inlib/colorUtils.js
.If this is a problem
Base.colors
could be defined using getters/setters or made readonly.Applicable issues
#3011
This should be semver patch.