Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Karma server without starting any Jasmine tests

I want to pass the --grep=PATTERN flag to karma run, so that only a specific tests gets executed. But if I start my karma server with karma start karma.conf.js, all Jasmine unit tests get executed immediately. As I have several hundred tests, this takes too long.

Question: How can I start the karma server and do not run any tests, but rather run only tests when I invoke karma run?

like image 220
Dynalon Avatar asked Feb 04 '16 09:02

Dynalon


1 Answers

It sounds like what you would prefer is to run a single test at a time via karma/jasmine using the terminal. If this is correct, this is what I do to run single file tests.

Note: I use gulp to spool up Karma, but this should still work for just using the global karma.

Here is my gulp method as well if you chose to use that.

gulp.task('karma', function (done) {
  karma.start({
    configFile: __dirname + '/karma.conf.js'
  }, done);
});

In my karma.config.js

module.exports = function(config) {

  config.set({

  basePath: '',

  frameworks: ['jasmine'],

  // Note the **setFilesUpForTesting** function below. Just change this.
  files: setFilesUpForTesting(),

  ... the rest of file
} // end of module!!!!!

--> Now below the module, in the same karma.config.js file add the following

// ------------------------------------------------------------------ >
// Below code is for running either single or all test files.
// To run a single test, just pass in the test file name like so...

// If the test file name was rs-test-me-now_test.js
// To spool up Karma you would enter

// gulp karma --single-test=rs-test-me-now

// To just run all tests
// gulp karma

var fixedPath = [
  'public/js/jquery.js',
  'public/js/jquery-ui.min.js',
  'public/js/foundation.min.js',
  'public/js/angular.min.js',
  'public/js/angular-mocks.js',
  'public/js/angular-route.min.js',
  'public/js/angular-sanitize.min.js',
  'public/js/angular-cookies.js'
  // all the files you want to include when tests run. I removed most of mine, but left some for reference.
];

var baseTestPath = 'public/test/'; // This is the path from root to your test/spec folder that contains all your tests.

function setFilesUpForTesting(){
  fixedPath.push( testPath());
  return fixedPath;
}

function testPath(){
  return singleTestPath() || fullTestPath();
}

function fullTestPath(){
  // If your root folder was butter, and all your tests were labeled
  // like my_file_spec.js, and all your specs were in a folder under root
  // called bread, then the path below would be
  // 'butter/bread/**/*_spec.js' Got it?
  return  'public/test/**/*_test.js'; // change this to suit your path.
}

function singleTestPath(){
  var passedArgument = process.argv.filter(function(item){ if(/--single-test=/.test(item)){return item; }});
  if( isEmpty( passedArgument )){ return false; }
  return baseTestPath + '**/*' + fileName( passedArgument ) + '_test.js';
  // change above to fit your path.
}

function fileName( argument ){
  if( !argument ){ return; }
  return argument[0].replace('--single-test=', ''); // Change single-test if you want to.
}

function isEmpty( array ){
  return array.length === 0;
}

When I want to run tests, I then just run the following

For single file tests gulp karma --single-test=test-me-now

// when test-me-now_test.js is the file name.
// and root/pulblic/test/   has all my test files and test folders.
// or root/bread/butter/ and file is called test-me-now_spec.js as in the other example I gave above.

Hope this helps someone. Ask questions if you can't get it up and running.

UPDATE

I just used karma from the command line and this works as stated above when using gulp as well.

If karma is accessible from the command line you can now do this if you implement the karma.config.js file above...

Note: I have to use karma start not run,

karma start (will run all tests)
// or for a single test file
karma start --single-test=my-test-file-name // but without the _test.js or _spec.js whatever you call your files. I had reasons for doing this, but you can change the functions above to accept the complete file name if needed.

Note:

You can also change the wording after "--" in --single-test= just make sure you update the karma.config.js info I shared.

like image 187
SoEzPz Avatar answered Sep 21 '22 11:09

SoEzPz