karma-server-side

1.8.0 • Public • Published

Karma Server Side

Ever wanted to interact with the host system when running karma tests? This module allows the tests running in your browser to do things on the server-side, that is, in node. This means you can run API or DB setup code from your tests inside karma.

Also, when you change your server-side files, they'll be reloaded on the next test run. No need to reload karma!

install

npm install karma-server-side

Edit your karma.conf.js to look like this, add server-side to the frameworks array:

module.exports = function(config) {
  config.set({
 
    ...
 
    frameworks: [..., 'server-side'],
 
    ...
 
  });
}

usage

In your tests (in the browser):

var server = require('karma-server-side');
 
server.run(function () {
  console.log('this is run on the server');
  return 'result';
}).then(function (result) {
  // result == 'result'
});

run returns a promise which completes when the function has executed on the server.

require

You can require modules on the server side by using serverRequire or require. Note that if you use require and browserify, then browserify will try to resolve those modules and bundle them into the test code in the browser.

serverRequire and require requires files relative to the current working directory of karma, not from the current test file.

promises

If you return a promise from the function passed to run() then run() will wait for it to complete.

server.run(function () {
  var fs = serverRequire('fs-promise');
  return fs.readFile('afile.txt', 'utf-8');
}).then(function (fileContents) {
  // fileContents is the contents of afile.txt
});

passing arguments

You can pass arguments to the function:

server.run(1, 2, function (a, b) {
  return a + b;
}).then(function (result) {
  // result == 3
});

run context

The this inside the function can be used to store values between calls to run():

server.run(function () {
  this.x = 'something';
}).then(function () {
  server.run(function () {
    return this.x;
  }).then(function (result) {
    // result == 'something'
  });
});

Debug

karma-server-side uses debug so you can see debug information by running karma with a DEBUG=karma-server-side variable:

DEBUG=karma-server-side karma start

Concurrency

Running more than one browser concurrently can be problemmatic for tests that run server-side code. Be sure to limit concurrency in your karma configuration if this applies to your tests:

// karma.conf.js
{
  ...
  concurrency: 1
}

Non-karma environment

Tests that use karma-server-side can also be run without karma (e.g. electron-mocha). In this case run block falls back to running the code in process.

Readme

Keywords

none

Package Sidebar

Install

npm i karma-server-side

Weekly Downloads

488

Version

1.8.0

License

ISC

Unpacked Size

15.7 kB

Total Files

12

Last publish

Collaborators

  • refractalize