4

CommonJS 有可能吗?

基本上我正在尝试从http://thewayofcode.wordpress.com/2013/04/21/how-to-build-and-test-rest-api-with-nodejs-express-mocha/进行 API 测试

并使用 Karma 运行测试。

我试图使用 RequireJS 和业力,基于http://karma-runner.github.io/0.10/plus/requirejs.html

我的 package.json 设置正确,并且 'npm install' 得到了我需要的一切,但是当我执行 'karma start test/karma.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

测试/routerSpec.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
});
4

3 回答 3

3

我在这里为 Karma 创建了一个插件:https ://www.npmjs.com/package/karma-common-js

它让你像使用 Browserify 一样编写测试,但插件不使用 Browserify。不使用 Browserify 有几个优点:

  • 没有创建捆绑包,因此查看文件更改非常快
  • 行号和文件名保留在堆栈跟踪中,无需源映射
  • 与业力覆盖一起使用
  • 让您传递第二个参数以require传递模拟
  • Browserifys 的所有(我希望)核心功能都能正常工作。例如转换,尊重 中的browser字段package.json,要求内置模块使用与 Browserify 相同的垫片等。
于 2015-03-07T20:22:59.697 回答
0

现在有一个用于 Karma 的 CommonJS 插件:https ://github.com/karma-runner/karma-commonjs

于 2013-12-13T09:57:20.600 回答
0

在尝试了一堆不同的插件之后,我最终使用了实际上运行良好的karma-browserifast插件——特别是如果你在调试模式下运行它。

于 2014-05-28T10:17:45.187 回答