4

これはCommonJSで可能ですか?

基本的に私はhttp://thewayofcode.wordpress.com/2013/04/21/how-to-build-and-test-rest-api-with-nodejs-express-mocha/から API テストを受けようとしています。

Karma を使用してテストを実行します。

http://karma-runner.github.io/0.10/plus/requirejs.htmlに基づいて、カルマで 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

テスト/rounderSpec.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'
        ]
    });
}

テスト/テスト-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

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 に答える