npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

karma-server-side

v1.8.0

Published

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.

Downloads

1,886

Readme

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.