Hi,
I'm using mocha 2.4.5 and I get the following error only when i use the npm run-script command line:
My package.json scripts:
{
"scripts": {
"test": "mocha -d -c --compilers js:babel-core/register --recursive test",
"test:watch": "npm test -- --watch"
}
}
My test.js file:
import {assert} from 'chai';
describe('addition', () => {
it('should return the correct result', () => {
assert.equal(1 + 1, 1);
assert.equal(1 + 1, 2);
});
});
The result:
pc-if24:app fgirardey$ npm run-script test
> [email protected] test /Users/fgirardey/my-project/app
> mocha -d -c --compilers js:babel-core/register --recursive test
Debugger listening on port 5858
addition
1) should return the correct result
0 passing (93ms)
1 failing
1) addition should return the correct result:
AssertionError: expected 2 to equal 1
at Context.<anonymous> (test.js:5:10)
npm ERR! Darwin 15.4.0
npm ERR! argv "/Users/fgirardey/.nvm/versions/node/v4.4.1/bin/node" "/Users/fgirardey/.nvm/versions/node/v4.4.1/bin/npm" "run-script" "test"
npm ERR! node v4.4.1
npm ERR! npm v2.14.20
npm ERR! code ELIFECYCLE
npm ERR! [email protected] test: `mocha -d -c --compilers js:babel-core/register --recursive test`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script 'mocha -d -c --compilers js:babel-core/register --recursive test'.
npm ERR! This is most likely a problem with the my-project package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! mocha -d -c --compilers js:babel-core/register --recursive test
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs my-project
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls my-project
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/fgirardey/my-project/app/npm-debug.log
Is it normal to have all these errors?
@fgirardey Are you pointing out the following part of the output?
npm ERR! Darwin 15.4.0
npm ERR! argv "/Users/fgirardey/.nvm/versions/node/v4.4.1/bin/node" "/Users/fgirardey/.nvm/versions/node/v4.4.1/bin/npm" "run-script" "test"
npm ERR! node v4.4.1
npm ERR! npm v2.14.20
npm ERR! code ELIFECYCLE
npm ERR! [email protected] test: `mocha -d -c --compilers js:babel-core/register --recursive test`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script 'mocha -d -c --compilers js:babel-core/register --recursive test'.
npm ERR! This is most likely a problem with the my-project package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! mocha -d -c --compilers js:babel-core/register --recursive test
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs my-project
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls my-project
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/fgirardey/my-project/app/npm-debug.log
If so, that part is added by npm whenever one of the package.json's scripts fails. Let us know otherwise.
I'd just like to point out that npm is much less verbose with the newer version that comes with Node 5.x (that or the default settings I've got are affecting it?), and that you can theoretically suppress the return value of mocha (though the exact way to do so may depend on the OS, I'm not sure if there's a portable solution).
and that you can theoretically suppress the return value of mocha
Like if mocha exits with 1, npm ignores the failure? Can you elaborate on that?
Yup; like add "|| echo" to the end of the test command. That has an annoying side-effect in Windows, however (echo without any text prints whether CMD is currently set to print the prompt/prefix and each command it executes). Off the top of my head I'm not sure of a command that works in both Windows CMD and Unix-like shells that will not print anything (or nothing but an empty line anyway) but will cause the previous return value to be ignored.
A downside that just occurred to me is that would also suppress any other error return code Mocha uses, if it uses any others for bigger problems than failed tests. Alternatively, maybe a commandline flag could be added to mocha to make it not return 1 in the event that tests fail, so that it can be used in things like npm without test failure looking like a program failure, but by default continue to report test failure in the return code?
I've found a portable suppression workaround: || exit 0 on the end of the test command works in both Windows and Linux. The caveat remains that this prevents npm from reporting errors from _any_ failure return code from mocha, not just the ones from test failures, and might ignore if mocha crashes or anything like that too (short of rebuilding Mocha with an intentional crash bug as a test [ha, meta], I'm not sure how we'd find out whether that's a potential issue...).
Most helpful comment
I've found a portable suppression workaround:
|| exit 0on the end of thetestcommand works in both Windows and Linux. The caveat remains that this prevents npm from reporting errors from _any_ failure return code from mocha, not just the ones from test failures, and might ignore if mocha crashes or anything like that too (short of rebuilding Mocha with an intentional crash bug as a test [ha, meta], I'm not sure how we'd find out whether that's a potential issue...).