question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setup globals via karma.conf.js

See original GitHub issue

It would be helpful to allow karma to setup globals in karma.conf.js via a globals object. This would be, for instance, helpful with tests that run agains an API (like contract tests).

If the configuration is kept in a json file (common scenario), it currently requires a trickery like this:

var fs = require('fs');
var MY_GLOBAL_API = 'some global value';
fs.writeFileSync('globals.js'), 'apiEndpoint = "' + MY_GLOBAL_API + '";', 'utf8');

module.exports = function(config) {

  var configuration = {
    files: [
        'globals.js'
        .....

It would have been a bit easier with:

var MY_GLOBAL_API = 'some global value';
module.exports = function(config) {

  var configuration = {
    globals.apiEndpoint = MY_GLOBAL_API;
    .....

Sort of similar to how protractor does it. I’d be happy to work on a PR.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:20
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
dignifiedquirecommented, Jun 13, 2016

I think this could be a plugin rather than core functionality. It would have a config very similar to what you suggesed:

module.exports = function (config) {
  config.set({
    globals: {
      myGlobal: 42
    }
  })
}

and would under the hood just generate a globals file and inject it into the files array.

8reactions
mgenovcommented, Feb 12, 2018

Does anyone has solution of this issue since 2016 ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Global variables in Karma test runner - Stack Overflow
You either declare that global variable within your test file: var global = "something"; describe('Your test suit', function() { ... });.
Read more >
Configuration File - Karma
The easiest way to generate an initial configuration file is by using the karma ... The Karma configuration file can be written in...
Read more >
Angular provide a global Module for testing - Developapa
Here are the snippets for Jest and Karma how to add such a module / configuration. Jest. In your jest.setup.ts/js. import 'jest-preset ...
Read more >
Jest and Karma in one Angular project | by Oleg Sytnik - Medium
Here we have karma configuration (src/karma.conf.js). You can find the list of available properties and a description of what they do here.
Read more >
Untitled
The recommended installation location of Karma is in your global node_modules directory. Installing Karma is done via the Node Package Manager (NPM).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found