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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With