3

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?

Alex Povar
  • 4,890
  • 3
  • 29
  • 44
  • I have the same issue. I load all my module files with ``files : [ my/base/appfolder/**/*.js, my/base/testfolder/**/*-test.js]``, but when karma runs (all of them), the test files conflict with each other, for example when declaring the same global variable for mocking. How to tell karma to run another browser, or reinit DOM, for each testjs file (or each parent ``describe(...)``). – Saad Benbouzid Nov 17 '15 at 09:05

0 Answers0