Mocha测试无法在Node.js服务器上运行 [英] Mocha test failing to run on nodejs server

查看:110
本文介绍了Mocha测试无法在Node.js服务器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找出导致断言失败时为什么我的测试崩溃的原因.

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

我有一个非常简单的NodeJs应用程序,并且我将 Mocha 用于 BDD ,没有声明框架(仅来自NodeJ的基本声明).

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

我使用npm test运行Mocha测试,并且具有以下package.json文件:

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"
  }
}

代码

要尝试Mocha,我创建了一个非常简单的测试:

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');
    });
});

问题

当我有assert.equal('A', 'A');时,测试通过,一切都很好.

Problem

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

但是,当我有assert.equal('A', 'B');时,测试失败(应该是),但是应用程序也崩溃了!

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:

它还会创建一个调试文件:

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"
}

并从我的package.json中完全删除devDependencies对象,但这不是一个真正的解决方案,只是该问题的一个微不足道的解决方法.如果我有仅用于开发环境的软件包和工具,则应该能够单独使用和安装它们!

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.

  • 为什么断言失败时我的代码会崩溃?

推荐答案

TL; DR

这是npm run的一种预期行为.

TL;DR

This is somehow an expected behavior from npm run.

经过长时间的搜索,我发现了一种以正确的方式引导我的威胁:

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

这里发生的是,摩卡返回失败测试的次数.因此,如果所有测试通过,mocha将返回0,npm继续运行而不会引发异常.

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.

但是,如果X测试失败,则mocha将返回X.由于运行脚本返回的结果不同于0,因此npm将引发代码X异常,并且执行失败.

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.

就个人而言,这种行为是胡扯.我不知道该怪谁,如果mocha在任意数量的测试失败时返回任意数字,是npm拒绝0以外的任何东西,或者是我,显然是不知道如何运行测试,或者至少保持掌握相关技术....

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 ....

现在,我需要继续寻找一种不错的方式来使用mocha和npm运行测试.希望这对某人有帮助.

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

这篇关于Mocha测试无法在Node.js服务器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆