NodeJS - How to test index.js without module.exports

I am testing a NodeJs project using Mocha, and I have a file index.jsthat is the main file without modules.exports, which runs as a 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);
    });
  });
});

if I run require('./index'), it will start the module and then continue moving forward, how can I wait for it to finish before I run the test cases?

Note. It calls some apis

+4
source share
2 answers

, , "" , ( "unix way" ). :

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

:

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

, unit test CLI Node script, . , , , script 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;
    };
}

, , , . :

// 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);
        // ...
    });
});
+2

Source: https://habr.com/ru/post/1568863/


All Articles