La prueba Mocha no se puede ejecutar en el servidor nodejs [duplicado]
Esta pregunta ya tiene una respuesta aquí:
npm error ELIFECYCLE al ejecutar la prueba 4 respuestasObjetivDescubra por qué mis pruebas fallan cuando no apruebo la afirmación.
FondTengo una aplicación NodeJs muy simple, y estoy usandoMoc para BDD sin marco de afirmación (solo la afirmación básica de NodeJs).
Yo ejecuto mi prueba de Mocha usandonpm test
y tengo el siguiente archivo package.json:
{
"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"
}
}
CódigPara probar Mocha, creé una prueba muy simple:
"use strict";
let assert = require("assert");
describe("server", function(){
it("prints out 'Hello, world'", function(){
assert.equal('A', 'B');
});
});
ProblemCuando tengoassert.equal('A', 'A');
, la prueba pasa y todo está bien.
Sin embargo, cuando tengoassert.equal('A', 'B');
, la prueba falla (como se supone que debe) ¡pero la aplicación también se bloquea!
De acuerdo con el tutorial que estoy siguiendo, esto no debería bloquearse:
https: //youtu.be/MrZ-XwDGPto? t = 146También crea un archivo de depuración:
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 [email protected]
3 info using [email protected]
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info pretest [email protected]
6 info test [email protected]
7 verbose unsafe-perm in lifecycle true
8 info [email protected] Failed to exec test script
9 verbose stack Error: [email protected] 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 [email protected]
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 [email protected] test: `mocha test.js`
17 error Exit status 1
18 error Failed at the [email protected] 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 ]
Corrección TentativaIntenté corregir esto haciendo
"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"
}
y eliminar completamente eldevDependencies
objeto de mi package.json, pero esta no es una solución real, solo una solución alternativa para el problema. Si tengo paquetes y herramientas que uso solo para un entorno de desarrollo, ¡debería poder usarlos e instalarlos por separado!
El problema aún persiste incluso después de la tentativa anterior.
Pregunt ¿Por qué mi código falla cuando falla la aserción?