最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if you are using Karma? - Stack Overflow

programmeradmin1浏览0评论

How can I check if I'm running my script on Karma?

When I'm on Karma, I tried with this:

var path = '';

if (typeof window.karma !== 'undefined') {
  path += 'base/';
} 

alert(typeof window.karma); // undefined

I also tried with this:

var path = '';

if (typeof window.__karma__ !== 'undefined') {
  path += 'base/';
} 

alert(typeof window.__karma__); // still undefined

Any ideas?

How can I check if I'm running my script on Karma?

When I'm on Karma, I tried with this:

var path = '';

if (typeof window.karma !== 'undefined') {
  path += 'base/';
} 

alert(typeof window.karma); // undefined

I also tried with this:

var path = '';

if (typeof window.__karma__ !== 'undefined') {
  path += 'base/';
} 

alert(typeof window.__karma__); // still undefined

Any ideas?

Share Improve this question edited Jun 27, 2018 at 11:33 user7637745 9852 gold badges14 silver badges27 bronze badges asked Nov 5, 2014 at 16:16 RunRun 57.3k178 gold badges463 silver badges771 bronze badges 5
  • 2 Can you post at least the karma configuration? Your second attempt should work in theory but if it's not working there's something wrong the the configuration I guess. – MarcoL Commented Nov 5, 2014 at 16:23
  • 1 It now works with the second one and I don't understand why! – Run Commented Nov 5, 2014 at 16:28
  • I'm just curious, why do you need to check this? – glepretre Commented Nov 6, 2014 at 8:28
  • 3 Perhaps author loads runs tests as a separate .html page locally, and uses Karma somewhere else. At least it is the way I got here :). It's easier to debug as a local page. http://localhost:9876/debug.html might help, though. – shaman.sir Commented Sep 16, 2015 at 13:55
  • 2 Looks like the second variant is what are you looking for karma-runner github – The Reason Commented Jun 27, 2018 at 11:55
Add a ment  | 

2 Answers 2

Reset to default 1

It should work if you use it this way

var path = '';

if ((<any>window).__karma__ !== undefined) {
    path += 'base/';
} 

alert(typeof window.__karma__); // still undefined

Karma has configurations which allows to include any lib files, so there you can include your own test specific JS.

In your case it could be a simple flag injected in the global context and used later in some certain test.

https://karma-runner.github.io/6.3/config/files.html

发布评论

评论列表(0)

  1. 暂无评论