Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if you are using Karma?

Tags:

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?

like image 422
Run Avatar asked Nov 05 '14 16:11

Run


People also ask

What is karma testing?

Karma is essentially a tool which spawns a web server that executes source code against test code for each of the browsers connected. The results of each test against each browser are examined and displayed via the command line to the developer such that they can see which browsers and tests passed or failed.

How do I check karma version?

By installing Karma globally, you'll have access to the “karma” command no matter your current location. To verify whether the installation was successful, just run “karma –version” and you should see the current version number. You're also going to need specific plugins, depending on the other testing tools you use.

How do I run a karma test in Chrome?

To launch Chrome from karma, we need to use karma-chrome-launcher. Run the command npm install karma-chrome-launcher --save to install to the application. Add the karma-chrome-launcher plugin to the plugins list in your karma.

How do you implement karma?

The recommended installation location of Karma is in your global node_modules directory. Installing Karma is done via the Node Package Manager (NPM). From any command prompt, enter the command: npm install -g karma. Should you desire to install Karma to your local directory you can use: npm install karma -save-dev.


2 Answers

It should work if you use it this way

var path = '';  if ((<any>window).__karma__ !== undefined) {     path += 'base/'; }   alert(typeof window.__karma__); // still undefined 
like image 148
ozerhakan Avatar answered Sep 30 '22 17:09

ozerhakan


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

like image 26
Damask Avatar answered Sep 30 '22 15:09

Damask