Mocha test failing to run on nodejs server [duplic

2019-07-23 15:07发布

问题:

This question already has an answer here:

  • npm error ELIFECYCLE while running the test 4 answers

Objective

Find out why my tests crash when I fail the assert.

Background

I have a very simple NodeJs application, and I am using Mocha for BDD with no assertion framework (just the basic assert from NodeJs).

I run my Mocha test using npm test and I have the following package.json file:

{
  "name": "server",
  "version": "1.0.0",
  "description": "Mah Project!",
  "main": "index.js",
  "scripts": {
    "test": "mocha test.js",
    "test-kitten": "mocha -R nyan test.js",
    "watch": "gulp watch",
    "start": "node server.js"
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "keywords": [
    "awesome"
  ],
  "author": "Rick and Morty",
  "license": "ISC",
  "homepage": "",
  "dependencies": {
    "express": "^4.14.0",
    "mongodb": "^2.2.6",
    "underscore": "^1.8.3"
  },
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-mocha": "^3.0.1",
    "mocha": "^3.0.2",
    "superagent": "^2.2.0"
  }
}

Code

To try Mocha, I created a very simple test:

"use strict";

let assert = require("assert");

describe("server", function(){
    it("prints out 'Hello, world'", function(){
        assert.equal('A', 'B');
    });
});

Problem

When I have assert.equal('A', 'A');, the test passes and everything is fine.

However, when I have assert.equal('A', 'B');, the test fails (as is supposed to) but the application crashes as well!

According to the tutorial I am following, this should not crash:

  • https://youtu.be/MrZ-XwDGPto?t=146

It also creates a debug file:

0 info it worked if it ends with ok
1 verbose cli [ '/home/ubuntu/.nvm/versions/node/v4.4.5/bin/node',
1 verbose cli   '/home/ubuntu/.nvm/versions/node/v4.4.5/bin/npm',
1 verbose cli   'run',
1 verbose cli   'test' ]
2 info using npm@2.15.5
3 info using node@v4.4.5
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info pretest server@1.0.0
6 info test server@1.0.0
7 verbose unsafe-perm in lifecycle true
8 info server@1.0.0 Failed to exec test script
9 verbose stack Error: server@1.0.0 test: `mocha test.js`
9 verbose stack Exit status 1
9 verbose stack     at EventEmitter.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules/npm/lib/utils/lifecycle.js:217:16)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at EventEmitter.emit (events.js:172:7)
9 verbose stack     at ChildProcess.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules/npm/lib/utils/spawn.js:24:14)
9 verbose stack     at emitTwo (events.js:87:13)
9 verbose stack     at ChildProcess.emit (events.js:172:7)
9 verbose stack     at maybeClose (internal/child_process.js:827:16)
9 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
10 verbose pkgid server@1.0.0
11 verbose cwd /home/ubuntu/workspace/server
12 error Linux 4.2.0-c9
13 error argv "/home/ubuntu/.nvm/versions/node/v4.4.5/bin/node" "/home/ubuntu/.nvm/versions/node/v4.4.5/bin/npm" "run" "test"
14 error node v4.4.5
15 error npm  v2.15.5
16 error code ELIFECYCLE
17 error server@1.0.0 test: `mocha test.js`
17 error Exit status 1
18 error Failed at the server@1.0.0 test script 'mocha test.js'.
18 error This is most likely a problem with the server package,
18 error not with npm itself.
18 error Tell the author that this fails on your system:
18 error     mocha test.js
18 error You can get information on how to open an issue for this project with:
18 error     npm bugs server
18 error Or if that isn't available, you can get their info via:
18 error
18 error     npm owner ls server
18 error There is likely additional logging output above.
19 verbose exit [ 1, true ]

Correction Tentative

I tried correcting this by doing

"dependencies": {
    "express": "^4.14.0",
    "mongodb": "^2.2.6",
    "underscore": "^1.8.3",
    "gulp": "^3.9.1",
    "gulp-mocha": "^3.0.1",
    "mocha": "^3.0.2",
    "superagent": "^2.2.0"
}

and completely removing the devDependencies object from my package.json, but this is not a real solution, only a meager workaround for the problem. If I have packages and tools that I use just for a dev environment, I should be able to use and install them separately !

The problem still persists even after the previous tentative.

Question

  • Why is my code crashing when the assertion fails?

回答1:

TL;DR

This is somehow an expected behavior from npm run.

Explanation

After a long time of searching, I found a threat that lead me in the right way:

  • npm error ELIFECYCLE while running the test

What happens here, is that mocha returns the number of failed tests. So if all tests pass, mocha returns 0 and npm continues without throwing an exception.

If however, X tests fail, mocha will return X. Because the returned result from running the script is different from 0, npm will throw an exception with code X and will fail execution.

Venting

Personally, this behavior is crap. I don't know who to blame here, if mocha for returning an arbitrary number when an arbitrary number of tests fail, is npm for refusing anything other than 0, or me, for apparently not knowing how to run tests, or at least keep up with the technologies to do so ....

Now I need to go on and find a decent way to run tests using mocha and npm. Hoping this helps someone.