相关疑难解决方法(0)

在Node.js中执行bash命令并获取退出代码

我可以在node.js中运行bash命令,如下所示:

var sys = require('sys')
var exec = require('child_process').exec;

function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("ls -la", function(err, stdout, stderr) {
  console.log(stdout);
});
Run Code Online (Sandbox Code Playgroud)

如何获取该命令的退出代码(ls -la在此示例中)?我试过跑步

exec("ls -la", function(err, stdout, stderr) {
  exec("echo $?", function(err, stdout, stderr) {
    console.log(stdout);
  });
});
Run Code Online (Sandbox Code Playgroud)

不管以前命令的退出代码如何,这总是返回0.我错过了什么?

bash exit-code node.js

35
推荐指数
3
解决办法
5万
查看次数

如何在基于Express的API上设置代码覆盖率?

我已经解决了这个问题一段时间了,我不能让现有的解决方案适合我.

我有一个用Express.js编写的Node.js API.我一直在使用Mocha,Chai和Supertest为API编写测试.这些测试主要是集成测试.

一个测试可能看起来像:

it('should fail to register a new user without the proper information', function(done) {
  api.post('/user')
  .send({})
  .expect(400)
  .expect('Content-Type', /json/)
  .end(function(err, res) {
    should.exist(res.body);
    should.exist(res.body.error);
    should.not.exist(err);
    res.body.error.should.contain('Username');
    res.body.error.should.contain('password');
    done();
  });
});
Run Code Online (Sandbox Code Playgroud)

实际测试工作得很好,但现在我需要能够查看这些测试的代码覆盖率.我必须知道我没有充分测试.我尝试过使用Mocha的测试覆盖率:

mocha -R html-cov --coverage > coverage.html
Run Code Online (Sandbox Code Playgroud)

伊斯坦布尔的:

istanbul cover _mocha -- -R spec --timeout 5000
Run Code Online (Sandbox Code Playgroud)

两者都遭遇同样的问题:

https://www.dropbox.com/s/qcgmout6hx91xgs/Screenshot%202014-04-19%2020.42.44.png

你看,这是一个示例路由(用户注册).我的测试肯定会覆盖它,但由于它们不直接调用这种方法,因此覆盖工具假设它从未被调用过.这就是问题 - 代码覆盖工具没有捕获最终执行的代码.

我尝试了另一种解决方案 - 伊斯坦布尔中间件,它实际上似乎更好地捕获了信息(尽管它很笨拙).然而,这里的相同路线如下:

在此输入图像描述

这显然也是不可取的.当然其他应用程序遇到了这个问题,他们如何去做呢?

注意:我已经安装了jscoverage以使所有这些工作.

我看过的来源:
https://brianstoner.com/blog/testing-in-nodejs-with-mocha/
http://boycook.wordpress.com/2013/03/29/automated-javascript-testing-with -mocha-and-js-coverage-for-nodejs/
Mocha的代码覆盖率

code-coverage mocha.js node.js express istanbul

14
推荐指数
2
解决办法
4409
查看次数

标签 统计

node.js ×2

bash ×1

code-coverage ×1

exit-code ×1

express ×1

istanbul ×1

mocha.js ×1