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 that you not install Mocha globally.
Description
Due to how mocha generates the diffs of action vs expected, the results can be very confusing and misleading. There's a big difference between an array that contains undefined and the value undefined
— that is, [undefined]
vs undefined
.
I originally thought this was an issue in Chai, but it turns out to be in Mocha:
- expect().to.deep.equal fails to handle arrays vs undefined chaijs/chai#1356
- https://github.com/mochajs/mocha/blob/v8.0.1/lib/utils.js#L307
Steps to Reproduce
Given an assertion of:
expect({
scopes: []
}).to.deep.equal({
scopes: undefined
})
Expected behavior: [What you expect to happen]
Output should be:
- "scopes": []
+ "scopes": undefined
Actual behavior: [What actually happens]
- "scopes": []
+ "scopes": [undefined]
Reproduces how often: [What percentage of the time does it reproduce?]
Every time.
Versions
- The output of
mocha --version
andnode node_modules/.bin/mocha --version
: 6.2.0, but the problem exists in v8.0.1 too - The output of
node --version
: 12.8.x - Your operating system
- name and version: n/a
- architecture (32 or 64-bit): n/a
- Your shell (e.g., bash, zsh, PowerShell, cmd): n/a
- Your browser and version (if running browser tests): n/a
- Any third-party Mocha-related modules (and their versions): n/a
- Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): n/a
Additional Information
This is due to Mocha's internal stringify function using square brackets to denote different types: https://github.com/mochajs/mocha/blob/v8.0.1/lib/utils.js#L303-L307
It looks okay when you're just doing a diff between undefined
and something else, where the output will be:
- "something"
+ [undefined]
But because the stringify function is used recursively inside objects, it leads to confusing output.