3

I'm using karma to run tests on an angularjs application.

There are a couple JavaScript functions that I would like to run at start-up, but they need to be dynamically created based on some system data. When running the app, this is handled with node.

Is there any way to create a script as a var and pass it to the files: [] rather than just using a pattern to load an existing file?

I can make this work by creating the file, saving it to disk then loading it normally, but that's messy.

Diver
  • 1,568
  • 2
  • 17
  • 32
  • Have a look at this question: http://stackoverflow.com/questions/24276239/detecting-environment-when-running-karma – MarcoL Jun 19 '14 at 21:29
  • Thanks, but I'm trying to execute a JavaScript function in the browser rather than change the karma.conf.js options. Kind of like how the html2js pre-processor sets the value of window.__html__[] with a script it creates and runs. – Diver Jun 19 '14 at 21:41
  • I see. Probably then karma init hooks can be much useful: http://stackoverflow.com/questions/22767246/does-karma-have-an-initialization-hook/22767451#22767451 – MarcoL Jun 19 '14 at 21:50
  • That looks closer, but its the '// do my stuff in here' that I'm creating on the fly. Short of using fs.writeFile to create the helper.js file I cant see how to make this work. I may just have to bite the bullet and maintain some 'config' scripts just for the tests, but I'm trying to automate as much as I can. – Diver Jun 19 '14 at 22:10
  • It sounds like you need to write your own pre-processor for karma then. It's not that hard, have a look to some on nom: https://www.npmjs.org/search?q=karma%20preprocessor – MarcoL Jun 19 '14 at 22:14
  • Yea, that's where I thought I was headed. Put that down as an answer and I'll accept it. – Diver Jun 19 '14 at 22:17

1 Answers1

3

You can create your own karma preprocessor script. For a starting point use the following as example:

var fs = require('fs'),
    path = require('path');

var createCustomPreprocessor = function (config, helper, logger) {

var log = logger.create('custom'),
// get here the configuration set in the karma.conf.js file
    customConfig = config.customConfig || {};

// read more config here in case needed
...

// a preprocessor has to return a function that when completed will call
// the done callback with the preprocessed content
return function (content, file, done) {
    log.debug('custom: processing "%s"\n', file.originalPath);

    // your crazy code here

    fs.writeFile(path.join(outputDirectory, name), ... , function (err) {
        if (err) {
            log.error(err);
        }
        done(content);
    });
  }
};

createCustomPreprocessor.$inject = ['config', 'helper', 'logger'];

module.exports = {
  'preprocessor:custom': ['factory', createCustomPreprocessor]
};

Add a package.json with the dependencies and serve it as a module. ;)

For more examples have a look to more modules here: https://www.npmjs.org/search?q=karma%20preprocessor

MarcoL
  • 9,829
  • 3
  • 37
  • 50