Is there a way to create and run a dynamic script from karma.conf.js - karma-runner

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.

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

Related

Is it possible to create a file with all my functions and read from from it in my specs?

I have some specs that are using the same functions, I would like to make one single file only for functions and read from this file while executing my scripts, would be that possible? if so.. how to do that?
During google searchers I found the "exports" to add in config file but didn't work (also I don't know how to call it from the config)
For example, I would like to add 2 functions in my config file (or separated file only for functions) and during any point of my execution, call it from the spec file:
function loginAdminUser(userElement, passWordElement, userName, password){
var loginButton = element(by.id('logIn'));
browser.get('https://( ͡° ͜ʖ ͡°).com/');
userElement.sendKeys(userName);
passWordElement.sendKeys(password);
loginButton.click();
}
function accessHistoryViewDetail(){
menuForViews.then(function(selectview) {
selectview[3].click();
browser.sleep(500);
});
}
1 - How can I do that? (using "Suites" would be an option?)
2 - How to call them in my specs?
Thank you and have a good day!
As far as I know you cannot add utility functions that you want to use in your tests in the config file. The options in the config file are generally for setting up the testing environment.
You can however put your functions in a separate file and import that to use the functions. Below is an example of how to do that using js and Node's module exports, you can do something similar with ts using classes.
// utils.js
function loginAdminUser(userElement, passWordElement, userName, password){
var loginButton = element(by.id('logIn'));
browser.get('https://( ͡° ͜ʖ ͡°).com/'); // nice Lenny face :)
userElement.sendKeys(userName);
passWordElement.sendKeys(password);
loginButton.click();
}
function accessHistoryViewDetail() {
menuForViews.then(function(selectview) {
selectview[3].click();
browser.sleep(500);
});
}
module.exports = {
loginAdminUserloginAdminUser: loginAdminUser,
accessHistoryViewDetail: accessHistoryViewDetail
}
Then in your spec file
import * as utils from './utils.js';
...
it('should ...', () => {
...
utils.accessHistoryViewDetail();
...
});
});
I hope that helps.

How do I access environment variables in config SailsJS

I'm trying to access my environment variables inside a config file. Can I use this variable inside a config?
For example
// config/env/development.js
module.exports = {
appUrl: 'http://MY_DEV_PLACE/',
}
//config/passport.js
var appUrl = appUrl || sails.config.appUrl || 'localhost:1337'; //<-- sails is not defined
I also tried in local.js:
// config/local.js
module.exports = {
gAPI: { secret: 'aaa'}
}
//config/passport.js
var appUrl = gAPI || sails.config.gAPI || 'some pass'; //<-- sails is not defined
EDIT:
For appURL I'm using env like: APP_URL=http://example.com/api sails lift
For password I'm using:
var locals;
try {
locals = require('./local');
} catch (e) {
// not local so just ignore
}
module.exports.passport = {
'GoogleAPI.Password': locals ? locals.gAPI.secret : ’some key'
};
You can use the local.js file for environment variables. This file is discussed in-depth here. This is pretty much the go to for storing environment variables in sails.
Important caveats: make sure this file is included in your .gitignore file lest you risk exposing important information to the world, this file will need to be configured for each environment (e.g. local, staging, production), you can access your environment variables via sails.config.variable_name, this file will take priority over the development.js and production.js file in the /env/ sub-directory.
I accessed the sails object from the routes.js like this:
module.exports.routes = {
'/': (req, res) => {
res.redirect(sails.config.frontendUrl)
}
}
In config/global.js add the following line....
sails: true,
This makes sails instance global.

Karma run single test

I use karma to run tests. I have many tests and running all tests is a very slow process. I want to run only a single test in order to spend less time, because all tests are run about 10 minutes.
Is it possible?
If you are using the Karma/Jasmine stack, use:
fdescribe("when ...", function () { // to [f]ocus on a single group of tests
fit("should ...", function () {...}); // to [f]ocus on a single test case
});
... and:
xdescribe("when ...", function () { // to e[x]clude a group of tests
xit("should ...", function () {...}); // to e[x]clude a test case
});
When you're on Karma/Mocha:
describe.only("when ...", function () { // to run [only] this group of tests
it.only("should ...", function () {...}); // to run [only] this test case
});
... and:
describe.skip("when ...", function () { // to [skip] running this group of tests
it.skip("should ...", function () {...}); // to [skip] running this test case
});
Update: karma has changed.
Now use fit() and fdescribe()
f stands for focused!
For Angular users, try the following ways:
1. Visual Studio Code Extension
The easiest way is to use the vscode-test-explorer extension along with its child angular-karma-test-explorer and jasmine-test-adapter, you'll get a list of current test to run one by one if you want:
UPDATE DEC/2021: The extension has been deprecated, please consider using Karma Test Explorer instead.
2. Karma runner improvement
Currently there's an open issue to improve their current behaviour, you can follow their progress at their github page.
3. Directly modify test.ts
In my case, I wasn't able to use the extension way because of this bug, and so, as stated in this answer; I ended up modifying the test.ts file. For example if you want to test a single file named my.file.name.spec.ts:
// By default context looks like this
const context = require.context('./', true, /\.spec\.ts$/);
// Modify it, so it's RegExp matches the files that you're willing to test.
const context = require.context('./', true, /my\.file\.name\.spec\.ts$/);
For more details about require parameters you may find it here at their wiki.
a) You can pass a pattern that describes your single file as command line argument to the karma start command:
# build and run all tests
$ karma start
# build and run only those tests that are in this dir
$ karma start --grep app/modules/sidebar/tests
# build and run only this test file
$ karma start --grep app/modules/sidebar/tests/animation_test.js
Source: https://gist.github.com/KidkArolis/fd5c0da60a5b748d54b2
b) You can use a Gulp (or Grunt ect.) task that starts Karma for you. This gives you more flexibility on how to execute Karma. You are for example able to pass custom command line arguments to those tasks. This strategy is also useful if you want to implement a watch mode that only executes the changed tests. (The Karma watch mode would execute all tests.) Another use case would be to only execute tests for files with local changes before you do a commit.
Also see Gulp examples below.
c) If you use VisualStudio, you might want to add an external tool command to the context menu of the solution explorer. This way, you can start the test from that context menu instead of using the console. Also see
How to execute custom file specific command / task in Visual Studio?
Example Gulp file
//This gulp file is used to execute the Karma test runner
//Several tasks are available, providing different work flows
//for using Karma.
var gulp = require('gulp');
var karma = require('karma');
var KarmaServerConstructor = karma.Server;
var karmaStopper = karma.stopper;
var watch = require('gulp-watch');
var commandLineArguments = require('yargs').argv;
var svn = require('gulp-svn');
var exec = require('child_process').exec;
var fs = require('fs');
//Executes all tests, based on the specifications in karma.conf.js
//Example usage: gulp all
gulp.task('all', function (done) {
var karmaOptions = { configFile: __dirname + '/karma.conf.js' };
var karmaServer = new KarmaServerConstructor(karmaOptions, done);
karmaServer.on('browsers_change', stopServerIfAllBrowsersAreClosed); //for a full list of events see http://karma-runner.github.io/1.0/dev/public-api.html
karmaServer.start();
});
//Executes only one test which has to be passed as command line argument --filePath
//The option --browser also has to be passed as command line argument.
//Example usage: gulp single --browser="Chrome_With_Saved_DevTools_Settings" --filePath="C:\myTest.spec.js"
gulp.task('single', function (done) {
var filePath = commandLineArguments.filePath.replace(/\\/g, "/");
var karmaOptions = {
configFile: __dirname + '/karma.conf.js',
action: 'start',
browsers: [commandLineArguments.browser],
files: [
'./Leen.Managementsystem/bower_components/jquery/dist/jquery.js',
'./Leen.Managementsystem/bower_components/globalize/lib/globalize.js',
{ pattern: './Leen.Managementsystem/bower_components/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/mockFactory.js', included: false },
{ pattern: './Leen.Managementsystem/App/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/*.js', included: false },
{ pattern: filePath, included: false },
'./Leen.Managementsystem.Tests/App/test-main.js',
'./switchKarmaToDebugTab.js' //also see https://stackoverflow.com/questions/33023535/open-karma-debug-html-page-on-startup
]
};
var karmaServer = new KarmaServerConstructor(karmaOptions, done);
karmaServer.on('browsers_change', stopServerIfAllBrowsersAreClosed);
karmaServer.start();
});
//Starts a watch mode for all *.spec.js files. Executes a test whenever it is saved with changes.
//The original Karma watch mode would execute all tests. This watch mode only executes the changed test.
//Example usage: gulp watch
gulp.task('watch', function () {
return gulp //
.watch('Leen.Managementsystem.Tests/App/**/*.spec.js', handleFileChanged)
.on('error', handleGulpError);
function handleFileChange(vinyl) {
var pathForChangedFile = "./" + vinyl.replace(/\\/g, "/");
var karmaOptions = {
configFile: __dirname + '/karma.conf.js',
action: 'start',
browsers: ['PhantomJS'],
singleRun: true,
files: [
'./Leen.Managementsystem/bower_components/jquery/dist/jquery.js',
'./Leen.Managementsystem/bower_components/globalize/lib/globalize.js',
{ pattern: './Leen.Managementsystem/bower_components/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/mockFactory.js', included: false },
{ pattern: './Leen.Managementsystem/App/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/*.js', included: false },
{ pattern: pathForChangedFile, included: false },
'./Leen.Managementsystem.Tests/App/test-main.js'
]
};
var karmaServer = new KarmaServerConstructor(karmaOptions);
karmaServer.start();
}
});
//Executes only tests for files that have local changes
//The option --browser has to be passed as command line arguments.
//Example usage: gulp localChanges --browser="Chrome_With_Saved_DevTools_Settings"
gulp.task('localChanges', function (done) {
exec('svn status -u --quiet --xml', handleSvnStatusOutput);
function handleSvnStatusOutput(error, stdout, stderr) {
if (error) {
throw error;
}
var changedJsFiles = getJavaScriptFiles(stdout);
var specFiles = getSpecFiles(changedJsFiles);
if(specFiles.length>0){
console.log('--- Following tests need to be executed for changed files: ---');
specFiles.forEach(function (file) {
console.log(file);
});
console.log('--------------------------------------------------------------');
} else{
console.log('Finsihed: No modified files need to be tested.');
return;
}
var files = [
'./Leen.Managementsystem/bower_components/jquery/dist/jquery.js',
'./Leen.Managementsystem/bower_components/globalize/lib/globalize.js',
{ pattern: './Leen.Managementsystem/bower_components/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/mockFactory.js', included: false },
{ pattern: './Leen.Managementsystem/App/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/*.js', included: false }];
specFiles.forEach(function (file) {
var pathForChangedFile = "./" + file.replace(/\\/g, "/");
files = files.concat([{ pattern: pathForChangedFile, included: false }]);
});
files = files.concat([ //
'./Leen.Managementsystem.Tests/App/test-main.js', //
'./switchKarmaToDebugTab.js'
]);
var karmaOptions = {
configFile: __dirname + '/karma.conf.js',
action: 'start',
singleRun: false,
browsers: [commandLineArguments.browser],
files: files
};
var karmaServer = new KarmaServerConstructor(karmaOptions, done);
karmaServer.on('browsers_change', stopServerIfAllBrowsersAreClosed);
karmaServer.start();
}
});
function getJavaScriptFiles(stdout) {
var jsFiles = [];
var lines = stdout.toString().split('\n');
lines.forEach(function (line) {
if (line.includes('js">')) {
var filePath = line.substring(9, line.length - 3);
jsFiles.push(filePath);
}
});
return jsFiles;
}
function getSpecFiles(jsFiles) {
var specFiles = [];
jsFiles.forEach(function (file) {
if (file.endsWith('.spec.js')) {
specFiles.push(file);
} else {
if (file.startsWith('Leen\.Managementsystem')) {
var specFile = file.replace('Leen\.Managementsystem\\', 'Leen.Managementsystem.Tests\\').replace('\.js', '.spec.js');
if (fs.existsSync(specFile)) {
specFiles.push(specFile);
} else {
console.error('Missing test: ' + specFile);
}
}
}
});
return specFiles;
}
function stopServerIfAllBrowsersAreClosed(browsers) {
if (browsers.length === 0) {
karmaStopper.stop();
}
}
function handleGulpError(error) {
throw error;
}
Example settings for ExternalToolCommand in VisualStudio:
Title: Run Karma using Chrome
Command: cmd.exe
Arguments: /c gulp single
--browser="Chrome_With_Saved_DevTools_Settings" --filePath=$(ItemPath)
Initial directory: $(SolutionDir)
Use Output window: true
If you want to run karma test with angular, You just need to modify your test.ts file.
Find line const context = require.context('./', true, /\.spec\.ts$/);
If you want to run your.component.spec.ts modify line to: const context = require.context('./', true, /your\.component\.spec\.ts$/);
Changing it() to iit() should work for running single test.
Also, similar, for describe() block we can use ddescribe()
Change your karma conf to only include the test you want to run instead of a full directory.
Inside the files : [...]
You might want to comment the preprocessors if you need/want to debug your test in chrome to avoid having your js minified.
Yes, this is an old thread.
The following situation has occurred on me 2 - 3 times now in the past few years. More so when I haven't done much unit testing and have come back to it.
I started up my Karma and found the tests, after initial start up, should have completed within 1 second to now take 20 seconds. Additionally, attempting to debug the unit tests within Chrome became tediously slow. The network tab showed all the files taking 2 - 3 seconds per file.
Solution: I didn't realize Fiddler was open. Close it and restart your tests.
Answer proposal for special Angular/IE case: The only thing that worked so far for me using "karma-ie-launcher", in order to run IE as browser, was modifying "include" property of tsconfig.spec.json to explicitly reference target test file using universal qualified path and not globs e.g. "C:\filepath\my-test.spec.ts", for compilation purposes. "In addition" the test.ts file should be appropriately amended to target said file for test file limitation purposes. Be aware that the cache will need to be initially deleted in IE for this scheme to take effect.
(For Angular/Chrome case modification of test.ts alone would be sufficient.)

Make the sails.js server monitor file change using chokidar and then emit socket message

I use chokidar to monitor if files have been changed in a folder. At the moment it is triggered when a user updates an Experiment Model in my ExperimentController.
var chokidar = require('chokidar');
...
var watcher = chokidar.watch('assets/output-model-files', {ignored: /[\/\\]\./, persistent: true});
watcher.on('change', function(path) {
...read my changed file and update the content of my database, and send a socket publishUpdate message...
... read file content in an asynchronous way
fs.readFile(path,"utf-8", function (err, data) {
... update the experiment object with the content of the changed file
Experiment.update(req.param('id'), ExpObj, function expUpdated(err) {});
... send a message via socket saying that the exeriment object has been updated
Experiment.publishUpdate(req.param('id'), {name: exp.name,
results: JSON.stringify(myres),
action: ('file has just been updated. nb of trajectories: '+totalNbTrajectories)
});
But I would like to constantly monitor any change in the target folder and send Experiment.publishUpdate messages when it happens from the moment when the sails.js server starts, and not when a user update an experiment object.
Where could I place that chokidar.watch(...) code on the server side so as to update an experiment object from a file change? socket.js?
Ok. I found that locating the code in bootstrap.js seems to do the job perfectly reagrding the even triggering...
My bootstrap.js now looks like:
var chokidar = require('chokidar');
var fs = require('fs');
var os = require('os');
var sys = require('sys');
module.exports.bootstrap = function(cb) {
// It's very important to trigger this callack method when you are finished
// with the bootstrap! (otherwise your server will never lift, since it's waiting on the bootstrap)
User.update({}, {online: false},
function userUpdated(err, users) {
if (err) {
console.log(err);
} else {
var watcher = chokidar.watch('assets/output-model-files', {ignored: /[\/\\]\./, persistent: true});
watcher.on('change', function(path) {
console.log('File', path, 'has been changed');
// do file reading and presumably Experiment publish update here
});
}
cb();
}
)
};

require.js synchronous loading

I'd like to define a module which computes a new dependancy, fetches it and then returns the result. Like so:
define(['defaults', 'get_config_name'], function(defaults, get_config_name) {
var name = get_config_name();
var config;
require.synchronous([configs / '+name'], function(a) {
config = defaults.extend(a);
});
return config;
});
Is there a way to do this or a better way to attack this problem?
You may try to use synchronous RequireJS call require('configs/'+get_config_name()), but it will load a module synchronously only if it is already loaded, otherwise it will throw an exception. Loading module/JavaScript file synchronously is technically impossible.
UPD: It's possible (see Henrique's answer) but highly unrecommended. It blocks JavaScript execution that causes to freezing of the entire page. So, RequireJS doesn't support it.
From your use case it seems that you don't need synchronous RequireJS, you need to return result asynchronously.
AMD pattern allows to define dependencies and load them asynchronously, but module's factory function must return result synchronously. The solution may be in using loader plugin (details here and here):
// config_loader.js
define(['defaults', 'get_config_name'], function(defaults, get_config_name) {
return {
load: function (resourceId, require, load) {
var config_name = 'configs/' + get_config_name();
require([config_name], function(config) {
load(defaults.extend(config));
})
}
}
});
// application.js
define(['config_loader!'], function(config) {
// code using config
});
If get_config_name() contains simple logic and doesn't depend on another modules, the better and simpler is calculating on the fly paths configuration option, or in case your config depends on context - map configuration option.
function get_config_name() {
// do something
}
require.config({
paths: {
'config': 'configs/' + get_config_name()
}
});
require(['application', 'defaults', 'config'], function(application, defaults, config) {
config = defaults.extend(config);
application.start(config);
});
Loading JavaScript synchronously is NOT technically impossible.
function loadJS(file){
var js = $.ajax({ type: "GET", url: file, async: false }).responseText; //No need to append
}
console.log('Test is loading...');
loadJS('test.js');
console.log('Test was loaded:', window.loadedModule); //loadedModule come from test.js