简体   繁体   中英

How to dynamically set files to be used in karma test

I have node file that is running a karma test in a node app using the karma public api (I'll save writing out the code because it comes straight from http://karma-runner.github.io/0.13/dev/public-api.html ).

All is fine so far, the test runs. Now I need to start serving different files to the karma run. For example, I might have exampleSpec.js, example1.js, example2.js, and example3.js. I need to serve exampleSpec and then example1-3 in sequence.

However, I don't see any documentation on this, and can't seem to get anywhere on.

So, The answer ended up being pretty simple. The first argument to the server constructor is a config object, that can replace or augment the karma.conf.js, so it is posible to send in altered files arrays. Code below for posterity:

"use strict";

var Server = require('karma').Server;


var filePath = process.cwd();
filePath += "/karma.conf.js";

console.log(filePath);

//files needs to be an array of string file matchers
function runTests(files, port) {
    var config = {
        configFile: filePath,
        files: files,
        port: port
    };
    var server = new Server(config, function(exitCode) {
        console.log('Karma has server exited with ' + exitCode);
        process.exit(exitCode)
    });

    server.on('run_complete', function (browser, result) {
        console.log('A browser run was completed');
        console.log(result);
    });

    server.start();
}

runTests(['test/**/*Spec.js', 'tmp/example.js'], 9876);
runTests(['test/**/*Spec.js', 'tmp/example2.js'], 9877);
runTests(['test/**/*Spec.js', 'tmp/example3.js'], 9878);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM