使用 karma 和 browerify 提供二进制数据

serve binary data with karma and browerify

我正在使用 karma + jasmine + browserify 来 运行 我的测试。

如果我尝试使用 karma 提供额外数据,我的测试将不再有效,它会告诉我 "Can't find variable: require"。

如果我不尝试提供任何额外数据,它会按预期工作。

那我如何使用 browserify 提供数据?

如果我在下面的配置文件中删除“{pattern: 'app/data/dcm/fruit', included: false, watched: false, served: true}”,"require" 不再是未定义的,当然我无法访问我希望测试加载的数据。

karma.conf:

// Karma configuration
// Generated on Tue Apr 14 2015 09:16:02 GMT+0200 (CEST)
'use strict';

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

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',

    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'browserify'],

    // list of files / patterns to load in the browser
    files: [
      'app/**/*.test.js',
      {pattern: 'app/data/dcm/fruit', included: false, watched: false, served: true}
    ],

    // list of files to exclude
    // exclude: [
    //   'app/doc/**/*.js',
    //   'app/examples/**/*.js'
    // ],

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      'app/**/*.test.js': ['browserify']
    },

    browserify: {
      debug: true,
      transform: [ 'glslify', 'babelify' ]
    },

    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    //    reporters: ['progress'],

    // web server port
    port: 9876,

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

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

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

    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['PhantomJS'],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true
  });

虚拟测试文件:

/* globals describe, it, expect, beforeEach*/
'use strict';

var vjsParsersDicom = require('../parsers/parsers.dicom.js');

var datasets = [];
// fruit dataset
var data = {
  name: 'fruit',
  url: 'base/data/dcm/fruit',
  nbOfFrames: 60
};
datasets.push(data);

// for(var i=0; i<datasets.length; i++){
      var i = 0;
  describe('Parsing ' + datasets[i].name, function() {

    // before each, load the data...
    var dataset;
    beforeEach(function(done) {

      window.console.log(datasets[i].url);
      // fetch the data!
      var oReq = new XMLHttpRequest();
      oReq.open('GET', datasets[i].url, true);
      oReq.responseType = 'arraybuffer';

      oReq.onload = function (oEvent) {
        var arrayBuffer = oReq.response; // Note: not oReq.responseText
        if (arrayBuffer) {
          dataset = vjsParsersDicom.parseArrayBuffer(arrayBuffer);
          done();
        }
      };
      oReq.send(null);
    });


    it('contains expected number of frames', function() {
      expect(true).toBe(true);
    });
  });
// }

错误:

nico@OSXLAP01821:~/work/gitroot/vjs$ ./node_modules/karma/bin/karma start
DEBUG [plugin]: Loading karma-* from /Users/nico/work/gitroot/vjs/node_modules
DEBUG [plugin]: Loading plugin /Users/nico/work/gitroot/vjs/node_modules/karma-browserify.
DEBUG [plugin]: Loading plugin /Users/nico/work/gitroot/vjs/node_modules/karma-chrome-launcher.
DEBUG [plugin]: Loading plugin /Users/nico/work/gitroot/vjs/node_modules/karma-dart.
DEBUG [plugin]: Loading plugin /Users/nico/work/gitroot/vjs/node_modules/karma-firefox-launcher.
DEBUG [plugin]: Loading plugin /Users/nico/work/gitroot/vjs/node_modules/karma-jasmine.
DEBUG [plugin]: Loading plugin /Users/nico/work/gitroot/vjs/node_modules/karma-mocha.
DEBUG [plugin]: Loading plugin /Users/nico/work/gitroot/vjs/node_modules/karma-phantomjs-launcher.
DEBUG [plugin]: Loading plugin /Users/nico/work/gitroot/vjs/node_modules/karma-sauce-launcher.
DEBUG [plugin]: Loading plugin /Users/nico/work/gitroot/vjs/node_modules/karma-spec-reporter.
DEBUG [framework.browserify]: created browserify bundle: /var/folders/g9/pdmxn9d17d50xghb4_yx84hh0005jg/T/e2e0ee5672a57d5a9e676449ef8d0462.browserify
DEBUG [framework.browserify]: add bundle to config.files at position 0
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
DEBUG [temp-dir]: Creating temp dir at /var/folders/g9/pdmxn9d17d50xghb4_yx84hh0005jg/T/karma-18408756
DEBUG [launcher]: /Users/nico/work/gitroot/vjs/node_modules/phantomjs/lib/phantom/bin/phantomjs /var/folders/g9/pdmxn9d17d50xghb4_yx84hh0005jg/T/karma-18408756/capture.js
DEBUG [framework.browserify]: building bundle
DEBUG [framework.browserify]: bundling
INFO [framework.browserify]: 454 bytes written (0.00 seconds)
INFO [framework.browserify]: bundle built
DEBUG [framework.browserify]: updating app/modules/parsers/parsers.dicom.test.js in bundle
DEBUG [web-server]: serving: /Users/nico/work/gitroot/vjs/node_modules/karma/static/client.html
DEBUG [web-server]: serving: /Users/nico/work/gitroot/vjs/node_modules/karma/static/karma.js
DEBUG [web-server]: upgrade /socket.io/1/websocket/9aVjgD27TyfoPeZwwQcQ
DEBUG [karma]: A browser has connected on socket 9aVjgD27TyfoPeZwwQcQ
INFO [PhantomJS 1.9.8 (Mac OS X)]: Connected on socket 9aVjgD27TyfoPeZwwQcQ with id 18408756
DEBUG [launcher]: PhantomJS (id 18408756) captured in 2.577 secs
DEBUG [framework.browserify]: resetting bundle
DEBUG [framework.browserify]: bundling
INFO [framework.browserify]: 386169 bytes written (2.01 seconds)
INFO [framework.browserify]: bundle updated
DEBUG [watcher]: Resolved files:
    /Users/nico/work/gitroot/vjs/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
    /Users/nico/work/gitroot/vjs/node_modules/karma-jasmine/lib/boot.js
    /Users/nico/work/gitroot/vjs/node_modules/karma-jasmine/lib/adapter.js
    /var/folders/g9/pdmxn9d17d50xghb4_yx84hh0005jg/T/e2e0ee5672a57d5a9e676449ef8d0462.browserify
    /Users/nico/work/gitroot/vjs/app/modules/parsers/parsers.dicom.test.js
    /Users/nico/work/gitroot/vjs/app/data/dcm/fruit.dcm
    /Users/nico/work/gitroot/vjs/app/data/dcm/series/36444280.dcm
    /Users/nico/work/gitroot/vjs/app/data/dcm/series/36444294.dcm
    /Users/nico/work/gitroot/vjs/app/data/dcm/series/36444308.dcm
    /Users/nico/work/gitroot/vjs/app/data/dcm/series/36444322.dcm
    /Users/nico/work/gitroot/vjs/app/data/dcm/series/36444336.dcm
DEBUG [web-server]: serving: /Users/nico/work/gitroot/vjs/node_modules/karma/static/context.html
DEBUG [web-server]: serving (cached): /Users/nico/work/gitroot/vjs/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
DEBUG [web-server]: serving (cached): /Users/nico/work/gitroot/vjs/node_modules/karma-jasmine/lib/boot.js
DEBUG [web-server]: serving (cached): /Users/nico/work/gitroot/vjs/node_modules/karma-jasmine/lib/adapter.js
DEBUG [web-server]: serving (cached): /var/folders/g9/pdmxn9d17d50xghb4_yx84hh0005jg/T/e2e0ee5672a57d5a9e676449ef8d0462.browserify
DEBUG [web-server]: serving (cached): /Users/nico/work/gitroot/vjs/app/modules/parsers/parsers.dicom.test.js
PhantomJS 1.9.8 (Mac OS X) ERROR
  ReferenceError: Can't find variable: require
  at /Users/nico/work/gitroot/vjs/app/modules/parsers/parsers.dicom.test.js:1


DEBUG [karma]: Run complete, exiting.
DEBUG [launcher]: Disconnecting all browsers
DEBUG [framework.browserify]: cleaning up

Karma 对我试图服务的内容感到困惑。 我们必须更具体地说明所提供的数据。

此外,Karma 无法提供任意二进制数据,例如 DICOM (.dcm)。首先,我们必须将 the.dcm 个文件重命名为 .dcm.tar.

看来我们只能提供文件,所以要提供所有文件我应该使用:

files: [
      'app/**/*.test.js',
      {pattern: 'app/data/**/*', included: false, watched: false, served: true}
    ]

并且只提供给定扩展名的文件:

files: [
      'app/**/*.test.js',
      {pattern: 'app/data/**/*.dcm.tar', included: false, watched: false, served: true}
    ]