2013-11-01 50 views
4

CommonJS有可能嗎?是否有可能通過karma/mocha使用CommonJS模塊

基本上我試圖從http://thewayofcode.wordpress.com/2013/04/21/how-to-build-and-test-rest-api-with-nodejs-express-mocha/

採取API測試和使用噶運行測試。

我嘗試使用RequireJS果報,根據關http://karma-runner.github.io/0.10/plus/requirejs.html

我的package.json是正確的設置和「故宮安裝」得到我需要的一切, 但是當我做「業力開始測試/因緣。 conf.js',沒有測試運行

DEBUG [karma]: All browsers are ready, executing 
DEBUG [web-server]: serving: /home/npoklitar/project/node_modules/karma/static/context.html 
DEBUG [web-server]: serving: /home/npoklitar/project/node_modules/karma-requirejs/lib/require.js 
DEBUG [web-server]: serving: /home/npoklitar/project/node_modules/karma-requirejs/lib/adapter.js 
DEBUG [web-server]: serving: /home/npoklitar/project/node_modules/mocha/mocha.js 
DEBUG [web-server]: serving: /home/npoklitar/project/node_modules/karma-mocha/lib/adapter.js 
DEBUG [web-server]: serving: /home/npoklitar/project/test/routerSpec.js 
DEBUG [web-server]: serving: /home/npoklitar/project/test/test-main.js 
ERROR: 'There is no timestamp for /base/supertest.js!' 
Chrome 30.0.1599 (Linux): Executed 0 of 0 SUCCESS (0 secs/0 secs) 
ERROR: 'There is no timestamp for /base/should.js!' 
Chrome 30.0.1599 (Linux): Executed 0 of 0 SUCCESS (0 secs/0 secs) 
ERROR: 'There is no timestamp for /base/assert.js!' 
Chrome 30.0.1599 (Linux): Executed 0 of 0 SUCCESS (0 secs/0 secs) 
Chrome 30.0.1599 (Linux): Executed 0 of 0 ERROR (0.355 secs/0 secs) 
DEBUG [launcher]: Disconnecting all browsers 
DEBUG [launcher]: Killing Chrome 

測試/ rounterSpec.js

require(['supertest','should','assert'], function(supertest,should,assert){ 
    describe('Routing:', function() { 
     var url = 'http://localhost:16000'; 

     describe('API', function() { 
     it('should return the success string and request headers', function(done){ 
      supertest(url) 
      .get('/api') 
      .expect(200) 
      .end(function(err, res) { 
       if (err) { 
        throw err; 
       } 
       var text = res.text; 
       var splitted = text.split('!'); 

       splitted[0].should.include('request successfully proxied to API'); 
       done(); 
       }); 
      }); 
     }); 
    }); 
    }); 

測試/ karma.conf.js

module.exports = function (karma) { 
    karma.set({ 

// base path, that will be used to resolve files and exclude 
     basePath: '../', 

     frameworks: ['mocha','requirejs'], 

// list of files/patterns to load in the browser 
     files: [ 
     //  {pattern: 'node_modules/chai/chai.js', include: true}, 
    //  {pattern: '*.js', include: false}, 
      'test/*.js', 

      'test/test-main.js' 
     ], 


// list of files to exclude 
     exclude: [ 
      'karma.conf.js' 
     ], 


// use dots reporter, as travis terminal does not support escaping sequences 
// possible values: 'dots', 'progress', 'junit', 'teamcity' 
// CLI --reporters progress 
     reporters: ['progress', 'junit', 'coverage'], 

     junitReporter: { 
      // will be resolved to basePath (in the same way as files/exclude patterns) 
      outputFile: 'junit-report/test-results.xml' 
     }, 

     preprocessors: { 
      'src/*.js': 'coverage' 
     }, 

//Code Coverage options. report type available: 
//- html (default) 
//- lcov (lcov and html) 
//- lcovonly 
//- text (standard output) 
//- text-summary (standard output) 
//- cobertura (xml format supported by Jenkins) 
     coverageReporter: { 
      // cf. http://gotwarlost.github.com/istanbul/public/apidocs/ 
      type: 'lcov', 
      dir: 'coverage/' 
     }, 


// web server port 
     port: 9876, 


// cli runner port 
     runnerPort: 9100, 


// enable/disable colors in the output (reporters and logs) 
     colors: true, 


// level of logging 
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG 
     logLevel: LOG_DEBUG, 


// enable/disable watching file and executing tests whenever any file changes 
     autoWatch: true, 


// Start these browsers, currently available: 
// - Chrome 
// - ChromeCanary 
// - Firefox 
// - Opera 
// - Safari (only Mac) 
// - PhantomJS 
// - IE (only Windows) 
// CLI --browsers Chrome,Firefox,Safari 
     browsers: ['Chrome'], 


// If browser does not capture in given timeout [ms], kill it 
     captureTimeout: 6000, 


// Continuous Integration mode 
// if true, it capture browsers, run tests and exit 
     singleRun: true, 


     plugins: [ 
      'karma-mocha', 
      'karma-chrome-launcher', 
      'karma-firefox-launcher', 
      'karma-junit-reporter', 
      'karma-coverage', 
      'karma-requirejs' 
     ] 
    }); 
} 

測試/測試main.js

var tests = []; 
for (var file in window.__karma__.files) { 
    if (/Spec\.js$/.test(file)) { 
     tests.push(file); 
    } 
} 

requirejs.config({ 
    // Karma serves files from '/base' 
    baseUrl: '/base', 
/* 
    paths: { 
     'jquery': '../lib/jquery', 
     'underscore': '../lib/underscore', 
    }, 

    shim: { 
     'underscore': { 
      exports: '_' 
     } 
    }, 
*/ 
    // nodeRequire: require, //doesnt work with or without this commented 

    // ask Require.js to load these files (all our tests) 
    deps: tests, 

    // start test run, once Require.js is done 
    callback: window.__karma__.start 
}); 
+0

是否存在遇到的特定問題? – Louis

+0

我更新了我的帖子 – Niko

+0

我用摩卡咖啡廣泛地使用了requirejs,沒有問題。不幸的是,我沒有使用業力。你是否有可能遇到[這個問題](https://github.com/karma-runner/karma-requirejs/issues/6)? – Louis

回答

0

嘗試很多不同的插件後,我結束了使用karma-browserifast插件實際上工作得很好 - 特別是如果你在調試模式下運行它。

3

我創建了一個插件噶這裏:https://www.npmjs.com/package/karma-common-js

它讓你,如果你正在使用Browserify,但該插件不使用Browserify寫測試的。不使用Browserify有幾個優點:

  • 有沒有創建包,所以這是非常快看文件的更改
  • 行號和文件名中的堆棧跟蹤將被保留,而無需源映射
  • 工程與因果報應 - 覆蓋
  • 讓你通過第二個參數require通過模擬
  • 所有(我希望)Browserifys核心功能的工作。如變換,請尊重package.json中的browser字段,要求內置模塊使用與Browserify相同的墊片等。
相關問題