mercredi 23 septembre 2015

nodejs testing with rewire and mocha SyntaxError: Unexpected token

I am trying to test a function exported from a file on nodejs. I also use q to handle promises. The function returns a promise that is resolve /reject on a callback internally. Inside this callback it calls another function from another place that returns another promise. When the second promise is done I resolve or reject the returned promise. On the test I would like to mock the second function so I use rewire to pass a stub from sinon that returns a promise which i manually resolve on the test. The thing is that when i try to mock this function then i get output: SyntaxtError: Unexpected token .

I am on a windows 7.

Is there anything that i am missing from the rewire module?

Error:

      1) Calls the createConfFiles just once


  1 passing (113ms)
  1 failing

  1) Setup scripts management configuration on current dir base on user inputs d
sdsdsd Calls the createConfFiles just once:
     SyntaxError: Unexpected token .
      at Function.__set__ (D:\p4\rcalvo_DEV-OB\Enterprise\static-content\src\mai
n\webapp\dirigent\bin\init\scripts.js:102:19)
      at Context.<anonymous> (D:\p4\rcalvo_DEV-OB\Enterprise\static-content\src\
main\webapp\dirigent\bin\init\scriptst.test.js:47:21)
      at callFnAsync (D:\p4\rcalvo_DEV-OB\Enterprise\static-content\src\main\web
app\dirigent\node_modules\mocha\lib\runnable.js:306:8)
      at Test.Runnable.run (D:\p4\rcalvo_DEV-OB\Enterprise\static-content\src\ma
in\webapp\dirigent\node_modules\mocha\lib\runnable.js:261:7)
      at Runner.runTest (D:\p4\rcalvo_DEV-OB\Enterprise\static-content\src\main\
webapp\dirigent\node_modules\mocha\lib\runner.js:421:10)
      at D:\p4\rcalvo_DEV-OB\Enterprise\static-content\src\main\webapp\dirigent\
node_modules\mocha\lib\runner.js:528:12
      at next (D:\p4\rcalvo_DEV-OB\Enterprise\static-content\src\main\webapp\dir
igent\node_modules\mocha\lib\runner.js:341:14)
      at D:\p4\rcalvo_DEV-OB\Enterprise\static-content\src\main\webapp\dirigent\
node_modules\mocha\lib\runner.js:351:7
      at next (D:\p4\rcalvo_DEV-OB\Enterprise\static-content\src\main\webapp\dir
igent\node_modules\mocha\lib\runner.js:283:14)
      at Immediate._onImmediate (D:\p4\rcalvo_DEV-OB\Enterprise\static-content\s
rc\main\webapp\dirigent\node_modules\mocha\lib\runner.js:319:5)



npm ERR! Test failed.  See above for more details.

Test file:

var expect = require('chai').expect;
var sinon = require('sinon');
var rewire = require('rewire');
var Q = require('q');
var inquirers = require('./mocks.js').inquirers;
var Scripts;

describe('Setup scripts management configuration on current dir base on user inputs', function () {

    var createConfigFiles;
    var defer;

    describe('Call once the createConfFiles function', function () {
        before(function () {
            Scripts = rewire('./scripts.js');
        });
        it('Calls the createConfFiles just once', function (done) {
            defer = Q.defer();
            createConfigFiles = sinon.stub().returns(defer.promise);
            Scripts.__set__({
                'inquirer': inquirers.buildScripts,
                './createConfigFiles.js': createConfigFiles
            });
            Scripts().then(function (result) {
                expect(result).to.equal("Yes");
                expect(createConfigFiles.calledOnce).to.be.true;
                done();
            }).done();

            defer.resolve(true);
        });
    });

});

Tested function:

function init() {
    process.stdout.write('\nScripts\n');
    inquirer.prompt(question, function (answer) {
        if (answer.scripts === choices[0]) {
           Q.when(createScriptsConfFile(ioOptions))
                .then(function (result) {
                    defer.resolve(choices[0]);
               }, function (error) {
                   defer.reject(error);
               }).done();
        } else {
            defer.reject(answer.scripts);
        }
    });

    return defer.promise;

};
// exported as recomended here http://ift.tt/1jaHsuV
module.exports = init;

Aucun commentaire:

Enregistrer un commentaire