Requirejs 可以将CommonJS模块与karma/mocha一起使用吗

Requirejs 可以将CommonJS模块与karma/mocha一起使用吗,requirejs,mocha.js,karma-runner,commonjs,Requirejs,Mocha.js,Karma Runner,Commonjs,这在CommonJS中可能吗 基本上,我试图从 并利用业力进行测试 我试图使用因果报应的RequireJS,基于 我的package.json安装正确,“npm install”得到了我需要的一切, 但是当我执行'karma start test/karma.conf.js'时,不会运行任何测试 DEBUG [karma]: All browsers are ready, executing DEBUG [web-server]: serving: /home/npoklitar/project

这在CommonJS中可能吗

基本上,我试图从

并利用业力进行测试

我试图使用因果报应的RequireJS,基于

我的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
测试/ROINTERSPEC.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();
              });
          }); 
        });
     });
  });
test/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'
        ]
    });
}
test/test-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
});

Karma现在有了一个通用的JS插件:

在尝试了一系列不同的插件之后,我最终使用了一个实际运行非常好的插件——特别是在调试模式下运行它时

我在这里为Karma创建了一个插件:

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

  • 没有创建包,因此可以非常快速地查看文件更改
  • 行号和文件名保留在堆栈跟踪中,无需源映射
  • 与业力覆盖一起工作
  • 允许您将第二个参数传递给
    require
    以在模拟中传递
  • 我希望Browserifys的所有核心功能都能正常工作。例如转换,关于
    package.json
    中的
    browser
    字段,需要内置模块使用与Browserify相同的垫片,等等

您是否遇到了具体问题?我已经将requirejs广泛用于摩卡,没有问题。不幸的是,我没有使用因果报应。有没有可能你碰到了?