lundi 28 septembre 2015

Test individual modules with Karma

There are few independant modules in my web app, and I want to test them individually. Is is possible to do with Karma?

Details

There are parts of gulpfile.js and karma.conf.js:

//gulpfile.js
gulp.task('test', function (done) {
  new karmaServer({
    configFile: __dirname + '/source/__test__/karma.conf.js',
    singleRun: true
  }, done).start();
});

//karma.conf.js
module.exports = function(config) {
  config.set({
    browsers: ['Chrome', 'Firefox'],
    frameworks: ['mocha', 'chai'],
    basePath: '.',
    files: [
      '../js/**/*.js',
      './**/*.spec.js'
    ]
  });
};

Provided config build karma server which serves all *.js files from ../js and *.spec.js files from ./ directory. This is almost okay except the fact that all modules are loaded simulatneously in one browser instance like they work in app. What I want is something that will allow me to create test for one module only and load only files which required for given module. Smth like that:

//hello.js
function greet(name) {
  return 'Hello, ' + name + '!';
}

//hello.specs.js
load('../hello.js'); // I'm going to test it only. Let browser load it 
describe('greeter', function () {
  it('should say Hello to the World', function () {
    expect(greet('World')).to.equal('Hello, World!');
  });
});

I know that it is possible to create one gulp task per each module, but I have a lot of small .js files and looks like it will be easier to test them individually. Do I using Karma in wrong way? Or maybe I missed something global?

Aucun commentaire:

Enregistrer un commentaire