Description
Prerequisites
- Checked that your issue hasn't already been filed by cross-referencing issues with the
faq
label - Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
- 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
- Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with:
node node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend avoiding the use of globally installed Mocha.
Description
The presence of colon (:
) symbol in Error reported by assertion engine along with showDiff
set to true
and fields expected
and actual
present alters the format of error in default reporter.
Steps to Reproduce
Sample test (sample.js
):
class TestError extends Error {
constructor(message) {
super(message + ': expected something else');
this.showDiff = true;
this.actual = null;
this.expected = null;
}
}
it('Should print message without a colon', () => {
throw new TestError('message without a colon');
});
it('Should print message with a colon(:)', () => {
throw new TestError('message with a colon(:)');
});
Run mocha sample.js
Expected behavior: [What you expect to happen]
Error messages follow the same format. Whichever it is, but I like stripped more because it allows to get rid of content if it's too long.
Actual behavior: [What actually happens]
1) Should print message without a colon
2) Should print message with a colon(:)
0 passing (8ms)
2 failing
1) Should print message without a colon:
message without a colon
+ expected - actual
at Context.it (sample.js:11:8)
2) Should print message with a colon(:):
Error: message with a colon(:): expected something else
+ expected - actual
at Context.it (sample.js:15:8)
Note that message that did not have colon was stripped off expected: something else
part and Error class name, while the message with a colon was printed as if it was straight out of toString
method.
Versions
- The output of
mocha --version
andnode node_modules/.bin/mocha --version
:5.2.0
local, no global - The output of
node --version
:v10.12.0
Additional Information
If showDiff
is false
or either actual
or expected
fields are missing, both errors are printed similarly.
Very unintuitive and took a while to figure out. I encountered this while testing http requests and trying to print urls in errors.
Easy to reproduce with chai as well. Assertions could look like this:
expect(1).to.equal(2);
expect(1, 'message without a colon').to.equal(2);
expect(1, 'message with a colon(:)').to.equal(2);