如何使用mocha和chai配置和使用karma来使用requirejs进行nodejs单元测试?

时间:2016-05-08 10:40:43

标签: javascript node.js unit-testing requirejs karma-runner

我花了很多天和时间来使用mocha和chai使用requirejs进行nodejs单元测试。对于nodejs单元测试karma配置不是必需的,但我希望浏览器显示。您的任何回复表示赞赏。看来我必须在test-main.how中指定每个依赖的JS库。它可以在单元测试开始之前从node_modules加载所有js

 testservice
    |   |
    |    ---- node_modules
    |   
    |   |  
    |    -----service 
    |     |-----demo.js
    |
    |    |--util
    |         |
    |         --httputil.js
    |   
    |    |
    |    ---- test
    |           |  
    |        ---- directtest    
    |               |
    |               ---- demo_Test.js 
    |                
    |
    |    |
    |    ---- test-main     
    |           |
    |            ---- test-main.js
    |           -----mocha.opts
    |
    |    |
    |     ---- karma.conf.js 
    |    
    |    | 
    |    ---- AppRoute.js
    |    -----server.js    
    |
    |    |   
    |    ------package.json

karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'requirejs'],
    files: [

        { pattern: 'node_modules/**/*.js', included: false },  
        { pattern: 'node_modules/**/*/*.js', included: false },
        'test/test-main/test-main.js',
        { pattern: 'test/test-main/*.js', included: false },
        { pattern: 'service/*.js', included: false },
        { pattern: 'util/*.js', included: false },
        { pattern: 'test/directtest/*.js', included: false }

    ],
    exclude: [

    ],

    reporters: ['progress','html'],

    htmlReporter: {
      outputFile: 'test/report/units.html',
      pageTitle: 'test Tests',
      subPageTitle: 'A sample project description'
    },  

    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: false
  });
};

测试main.js

(function() {

  var specFiles = null;
  var baseUrl = '';
  var requirejsCallback = null;
  if (typeof window != 'undefined' && window.__karma__ != undefined) {

    baseUrl = '/base';
    requirejsCallback = window.__karma__.start;
    specFiles = [];
    for (var file in window.__karma__.files) {
      if (window.__karma__.files.hasOwnProperty(file)) {
          if (/.*\/directtest\/*\/.+_Test\.js$/.test(file)) {
          specFiles.push(file);
        }
      }
    }
  }
  requirejs.config({

      baseUrl: baseUrl,

      paths: {
          'nodemodule': './node_modules/*/*',
        'chai': './node_modules/chai/chai',
        'sinon': './node_modules/sinon/pkg/sinon',
        'chaiHttp': './node_modules/chai-http/dist/chai-http',
        'util':'./util/httputil',
        'demotest':'./service/demo'
      },

      deps: specFiles,
      callback: requirejsCallback
  });
})();

demo_Test.js

define(['chai', 'sinon', 'chaiHttp', 'util','demotest'], function (chai, sinon, chaiHttp,util,demotest) {
 //define(function (require, exports, module) { --tried this way but still    getting error
  describe("should work", function () { 
    //using this block of code to get all js from node_module but seems not doing as of my intention.  
    var Other;
      var testService= require("../../service/demo");
    var expect = require('chai').expect;   

     beforeEach(function(done){
        requirejs(['./node_modules/*/*'], function(_File){
            Other = _File;
            done(); 
        });
    });

    it('should work', function () {
        expect(true).to.be.true;
    });

     it('test', function () {
          expect(testService.callSomeFunc({ "xxx": "1" }, {})).to.be.equal("x1");
      });

    });

});
demo.js


       var httpUtil = require('../util/httputil_test'); // is calling diff
                                                             JS(httputil.js)
         exports.function = abc(){ ....}// i want to write testcase for abc()
    httputil.js


        var Client = require('node-rest-client').Client; 
              var restClient = new Client();
        // node-rest-client third party lib if i specify this module again complains for other negotiator library not loaded so and so. i want all depency to be automatically resolved and get from node_modules itself. 

    i tried also change in karma.conf.js with this option
      { pattern: 'node_modules/**/*.js', included: true  },  
      { pattern: 'node_modules/**/*/*.js', included: true }
 but no luck

ERROR:

08 05 2016 05:55:56.170:DEBUG [web-server]: serving (cached): C:/Work/New folder/working/testservice/node_modules/chai/chai.js
08 05 2016 05:55:56.189:DEBUG [web-server]: serving (cached): C:/Work/New folder/working/testservice/node_modules/sinon/pkg/sinon.js
08 05 2016 05:55:56.224:DEBUG [web-server]: serving (cached): C:/Work/New folder/working/testservice/node_modules/chai-http/dist/chai-http.js
08 05 2016 05:55:56.265:DEBUG [web-server]: serving (cached): C:/Work/New folder/working/testservice/util/HttpUtil.js
08 05 2016 05:55:56.316:DEBUG [web-server]: serving (cached): C:/Work/New folder/working/testservice/service/demo.js
Chrome 50.0.2661 (Windows 10 0.0.0) ERROR
  Uncaught Error: Module name "node-rest-client" has not been loaded yet for context: _. Use require([])
  http://requirejs.org/docs/errors.html#notloaded
  at C:/Work/New folder/working/testservice/node_modules/requirejs/require.js:140

0 个答案:

没有答案