NodeJS – 如何在没有module.exports的情况下testingindex.js

我正在testing我的NodeJs项目使用摩卡和我有一个文件, index.js是主要文件没有module.exports像CLI运行

index.js

 // CLI tools var bluebird = require('bluebird'); var gigatool = require('./lib/gigatool'); var debug = require('debug')('index'); var size = 20; var page = process.env.BATCH; var startDate = process.env.START; var dataDir = process.env.DATADIR; debug(page, startDate, dataDir); // requires parameters if (!process.env.BATCH) { throw new Error('BATCH environment variable is needed'); } tool = gigatool(size, page, dataDir); bluebird.all([tool.clean(startDate), tool.continuous()]) .finally(function(){ process.exit(0); }); 

test.js

 'use strict'; var chai = require('chai'); var fs = require('fs'); var noop = require('lodash.noop'); var rimraf = require('rimraf'); var async = require('async'); var rimraf = require('rimraf'); var expect = chai.expect; describe.only('Integration', function() { var dataDir = './countries'; var path = dataDir + '/Albania'; describe('clean run', function() { this.timeout(10000); before(function() { process.env.BATCH = 1; process.env.DEBUG = '*'; require('../../index'); }); after(function(done) { // rimraf(dataDir, done); }); }); }); 

如果我运行require('./index') ,它会运行模块,然后继续前进,我如何等待它结束之前我运行testing用例?

注意:这是打电话给一些apis

       

网上收集的解决方案 "NodeJS – 如何在没有module.exports的情况下testingindex.js"

你需要一次testing你的整个应用程序,除非你的代码是一个单元(“unix方式”),否则这个testing仍然是testing,但几乎不是“单元”testing。 为此,您的代码应该从以下开始:

 var Promise= require("bluebird"); var exec= Promise.promisify(require("child_process").exec); var run = function(args){ return exec("node", ["../../index.js"].concat(args)).get("stdout"); }; 

这将使你的testingtesting文件上的实际input:

 describe('your code', function() { it('should work with params a,b', function(){ return run(['a','b']).then(function(out){ // note the Mocha promise syntax assert.equal(out, 'your expected stdout'); }); }); }); 

不幸的是,没有办法单独testingCLI节点脚本的各个方面。 相反,过去我所做的是根据脚本是通过require还是从命令行调用来require条件执行:

 // index.js var foo = require('foo'); var bar = require('bar'); // ... // determine if this script is being required as a module or is CLI var IS_EXECUTING = (require.main === module); var methods = { init: function(args) { methods.auditArgs(args); methods.doSomeStuff(arg1, arg2); methods.doOtherStuff(); }, auditArgs: function(args) {/* ... */}, doSomeStuff: function(arg1, arg2) {/* ... */}, // ... }; // At the bottom we either begin execution or return a function which can // be called in a test harness when ready... if (IS_EXECUTING) { methods.init(process.argv); } else { module.exports = function (mockMethods) { // you could have some code here to mock out provided methods // for example: methods.auditArgs = mockMethods.auditArgs || methods.auditArgs; // then return the "API" for this script... return methods; }; } 

在你的testing工具中,你只需要这个文件,准备就绪的时候就像使用其他模块一样使用它。 但是从命令行调用时,代码将正常执行:

 // in test.js 'use strict'; var chai = require('chai'); // ... var appFactory = require('index'); var expect = chai.expect; describe('initialization', function() { var app; beforeEach(function() { app = appFactory({ auditArgs = chai.spy(function() { }); // other mock method implementations, spies, etc }); }); it('should call necessary methods on init', function() { expect(app.auditArgs).to.have.been.called(1); // ... }); });