节点CLIunit testing

我有一个节点模块,我正在努力,我想写它的unit testing,但是我很困惑如何通过testing套件将参数(CLI所需)传递给节点。

让我们假设(为了简洁)模块名称是J,所以我会这样称呼它…

$ j --file test.js --file test2.js 

当我正在编写testing套件时,如何重新创build这些文件?

您可以使用节点的subprocess模块来运行其他命令行进程。 这个链接可以给你更多的语法信息; 我build议也检查承诺的subprocess版本 。

 var spawn = require('child-process').spawn; spawn('j', ['--file', 'test.js', '--file', 'test2.js']) .progress(function(childProcess){ // any logic you want to do here while process is running }) .then(function(result){ // command was executed // write tests here }) .fail(function(err){ // maybe 1 last test to make sure there was no test }); 

就unit testing套件而言,我期望你的任何东西都可以工作(mocha / chai等)