Description
Bug Report Checklist
- I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
- I have searched for related issues and issues with the
faq
label, but none matched my issue. - I have '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, my usage of Mocha, or Mocha itself.
- I want to provide a PR to resolve this
Expected
For this assertion:
expect(1, 'Test: expected something').to.equal(2)
Expected output:
Test: expected something
+ expected - actual
-1
+2
Actual
Actual output:
Test
+ expected - actual
-1
+2
Minimal, Reproducible Example
import { expect } from 'chai'
describe('reporter', function () {
it.only('should log', function () {
expect(1, 'Test: expected something').to.equal(2)
})
})
Versions
[email protected]
[email protected]
Additional Info
This ticket matches but is closed:
#3531
If you rather re-open that ticket, I'm fine with closing this one as a duplicate.
The regex here appears to have unexpected behavior when the message contains a colon before ": expected".
var match = message.match(/^([^:]+): expected/);
msg = '\n ' + color('error message', match ? match[1] : msg);
No Match
For example:
expect(1, 'Test: something').to.equal(2)
The message is: "Test: something: expected 1 to equal 2". The match is falsy even though it ends with ": expected ..." so the whole message is used:
AssertionError: Test: something: expected 1 to equal 2
+ expected - actual
-1
+2
This looks reasonable until you look at the other use cases. This is actually the output I wanted originally but looking at the code and other cases I don't think this is the expected result.
Unexpected Match
If the custom message includes ": expected", you get something really strange:
expect(1, 'Test: expected something').to.equal(2)
The message is: "'Test: expected something: expected 1 to equal 2" and the capture group is "Test" so you just get:
Test
+ expected - actual
-1
+2
Expected Match
The "normal" case uses just the custom message:
expect(1, 'Test').to.equal(2)
The message
is "Test: expected 1 to equal 2" and the capture group is "Test" so you get just the custom message:
Test
+ expected - actual
-1
+2
Suggestion
Assuming the expected behavior is to extract the custom message and drop the ": expected ..." at the end, like the example with no colon, this regex seems to work better:
var match = message.match(/^(.+): expected/);