how to run tests with karma in another host's browser? - javascript

My general problem is how to run unit tests with karma in IE or other browser installed on a Windows machine, while karma is run on a Linux machine, though in the question below I might ask more specific question based on what I've already tried.
Also it may be worth to mention, that I have Windows installed on a Virtual box machine.
I started karma from a terminal, then opened in IE this url: http://10.0.2.2:9876 and saw the karma page in the browser window (though I spotted some quick blink with a red background, I suppose it is related to an error I'll mention further).
After that I tried to run tests with the command: node_modules/karma/bin/karma run config.js in another terminal tab. When I did this I got this error (I get it even when I use Chrome on my Linux host-machine instead IE on the Virtual box guest, so I suppose the error has nothing to do with networking):
You need to include some adapter that implements __karma__.start method!
I googled for this error and found another question: Error: You need to include some adapter that implements __karma__.start method
Supposing that I need to run tests from the same place I started karma-server I tried to redirect the output to another tab:
$node_modules/karma/bin/karma start &> /dev/pts/17 &
$node_modules/karma/bin/karma run config.js
But this did not help. If I try to start server and run tests in a single command, then I do not have time to register IE.
So could anyone please answer any of the next questions:
how to run unit tests with karma in a browser on other machine?
how to get rid of this error You need to include some adapter that implements __karma__.start method!, if the karma server was started in one terminal tab and the run command was issued in another?
how to start the karma server and run tests in separate steps, i.e. issue the karma start and karma run config.js commands instead of karma start config.js?
My regards and sincerest appreciation in advance for any help.

Currently I've found a workaround for this. I start karma with the config file:
karma start config.js
in the config I have singleRun: false.
Then I manually connect my IE instance, and after this I run the tests from another terminal tab. Why this happens I have no idea. The only inconvenience is that I always have browsers' windows open.

Related

Azure Functions: How to debug in WebStorm?

The azure-functions-cli offers a way to kickoff debugging, but these instructions seem to be Visual Studio specific.
I tried using a similar trick to serverless by setting up a run config in WebStorm to point the JavaScript file to:
\node_modules\azure-functions-cli\lib\main.js
And then passing the Application args:
run myFunctionName --debug
This successfully runs the functions with Azure's tools, but both WebStorm tries to set a debugging port; and when the Azure window opens up it sets its own debugging port.
From Webstorm:
C:\Program Files (x86)\JetBrains\WebStorm 2016.2.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" --debug-brk=60168 --expose_debug_as=v8debug C:\Users\username\AppData\Roaming\npm\node_modules\azure-functions-cli\lib\main.js run myfunction --debug
Debugger listening on [::]:60168
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException:
Likewise, Azure-cli says it opens a debugging port, but they do not match.
As a result, breakpoints set are ignored when the functions are called (Though it does successfully run).
Anybody know how to configure this properly to be able to use WebStorm to debug?
Azure-Functions-CLI was renamed to azure-functions-core-tools. If you still have the Azure-Functions-CLI see my legacy response at the end of this post.
If you're running the new azure-functions-core-tools it looks like they broke the capability to run a remote debugger :-(.
I have the following issue opened and I will update if they tell me otherwise:
https://github.com/Azure/azure-functions-core-tools/issues/378
Fortunately, the new Beta version of the azure-functions-core tools doesn't have all of this C# craziness that prevents it from running on other OSes and requires a remote debugger. To install that version, you can use:
npm i -g azure-functions-core-tools#core
With that version installed, you can launch things with the good 'ol standard Node runtime.
Within WebStorm from Run -> Edit Configurations create a new "Node.JS".
Give the debugging some type of name.
Set the JavaScript file to:
~\AppData\Roaming\npm\node_modules\azure-functions-core-tools\lib\main.js
NOTE: The above assume you installed Azure Functions on a Windows machine with the global flag.
Set the Application Parameters to: start --debug VSCODE
Edit the file ".vscode\launch.json" and change the debug port to 9229 for node.
Within WebStorm choose Run-> Debug:"What_You_Named_the_Remote_Profile"
Add some breakpoints.
Navigate to your API end-point and see that the break-points work.
NOTE: By default it appears the function will be at http://localhost:7071/api/functionName
------------------- EDITED But Below Held for Posterity --------------
Okay, it looks like you can not do this with local debugging, but can with "Remote Debugging" within WebStorm.
Within WebStorm from Run -> Edit Configurations create a new "Node.JS Remote Debug".
Give the debugging some type of name.
Hit the + Sign where it says, "Before Launch: External Tool" and choose "Run External Tool".
Hit the + Sign again and fill it out like the screen-shot (This is assuming you installed the Azure Function CLI globally).
NOTE: The above screenshot has been updated based on the latest version of Azure Functions CLI/. Earlier versions required you to state an app name, and did not require --debug to debug. As a result if you are not updated to the latest version of Azure Functions CLI (now known as Azure-Functions-Core-Tools) you may need to have "run MyApp" in the Parameters field.
Within WebStorm choose Run-> Debug:"What_You_Named_the_Remote_Profile"
Add some breakpoints.
Navigate to your API end-point and see that the break-points work.
NOTE: By default it appears the function will be at http://localhost:7071/api/functionName
Add this to your local.setting.json file under values: "languageWorkers:node:arguments": "--inspect=5858"
Click Edit configuration. Click the plus icon and choose Attachto Node.js/Chrome
Fill in these values for the options: host: localhost - Port: 5858 and set Attac to option to "Crhome or Nod.js>6.3 started with --inpect"
Start the function and run the debugger
See this picture: https://i.stack.imgur.com/hnC74.png

Triggering Karma tests in the test runner

I am having a small issue with WebStorm that I am hoping someone has experienced and solved before.
I am using WebStorm to build a angular.js app and I have it set up to use Karma to run my tests. This is fine for the most part: I have a Karma configuration setup and I can get to to run the tests or debug them with no issue.
My problem is that when I try to run a test individually by clicking on one of the test in the "Test Run" tree it goes off to a node configuration, tries to run it and fails (because its looking for js dependencies). After that I just go back to my 'karma config' and it runs through the whole of the test no problem.
Does anyone know how I can get the IDE hooked up so that I can trigger my tests from the UI?
Running tests from file right-click menu is only supported for those runners that allow executing individual tests (JSTestDriver, for example). There is currently no such possibility for Karma (WEB-13173). See the discussion at https://github.com/karma-runner/karma/issues/1235.
to run individual test files, you can have several karma configuration files with different sets of tests included. Plus you can rename individual tests/suits in the way mentioned in https://github.com/karma-runner/karma/issues/553

Run/debug node.js testcases with jasmine in Webstorm 11

I install Webstorm 11 and want to run my tests (for node.js app) implemented with Jasmine.
However it's not easy to do that. I could just type in command line 'jasmine' command and test will be runned, but in this case I'm not able to debug code.
So is there a way to configure Webstorm to deal with jasmine specs as it should?
Ok, so while no one answer at the moment I will try to provide my version:
This flow will allow to run jasmine testsute from Webstrom and debug testcases
install jasmine (ither locally or globally)
in project folder create folder 'spec/support' In this folder place jasmine.json
tests configuration example:
{
"spec_dir": "tests",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
]
}
Create node.js configuration in Webstorm
In this configuration select source file - jasmine executable file (for localy installed
jasmine it will be 'node_modules\jasmine\bin\jasmine.js' )
So you are ready. However at the current moment when trying to debug this configuration - it fails with error:
Cannot stop on breakpoint due to internal error org.jetbrains.v8.V8CommandProcessor$1:
If you faced with it - you need to change Webstom configuration and set this settings:
-Dnodejs.debugger.use.jb.support=false For more details check there:
So this allow you to run jasmine tests and debug them. However there is still some things which this solution not able to do:
Run individual testcases
Run individual testcases with right click button and Run command from menu
Jetbrains, if you reading this - please fix this already. I started play with node in Webstorm 3 years ago and since that moment and dozens of version there is still no nice way to run tests. It's ridiculous.
Jasmine works with the JSTestDriver which you get out-of-the-box with WebStorm 11:
https://www.jetbrains.com/webstorm/help/enabling-javascript-unit-testing-support.html
This page also details how to add Jasmine within JSTestDriver:
https://www.jetbrains.com/webstorm/help/preparing-to-use-jstestdriver-test-runner.html
At a high level you're going to:
Install JSTestDriver from JetBrains plugin repo
Configure it as a WebStorm JavaScript library (https://www.jetbrains.com/webstorm/help/configuring-javascript-libraries.html#configure)
Open jsTestDriver.conf and type the following code in it:
load:
lib/jasmine/jasmine.js
lib/jasmine-jstd-adapter/JasmineAdapter.js
WebStorm doesn't manage test running directly. This job is done by a test runner. WebStorm supports several test runners - Mocha, Karma, JsTestDriver, nodeunit. Most of them can execute Jasmine tests

Karma not running unit tests due to "No captured browser" message

I'm trying to set up Karma to run AngularJS unit tests using Jasmine, but I can't get the tests to run. I'm sure I'm overlooking something simple. I'm running this on a Windows 7 machine with Node.js installed and karma installed via npm.
My directory structure looks like this:
js/app/ - contains controllers, app, etc
js/config/ - contains karma.conf.js
js/lib/ - contains angular
js/test/ - contains jasmine specs
I'm starting a command prompt in the js directory and running this command:
karma start config/karma.conf.js
That causes Chrome to run on port 9876, but whenever I change any watched files and check the Karma output, I see this info message:
No captured browser, open http://localhost:9876/
Here's my config file:
module.exports = function(config) {
config.set({
basePath: '../',
frameworks: ['jasmine'],
files: [
'lib/angular.js',
'app/**/*.js',
'test/**/*.js'
],
exclude: [
],
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
captureTimeout: 60000,
singleRun: false
});
};
I'm using Angular 1.2.10 and Karma 0.10.9
I had a face palm moment. karma start needs to be run in the same directory as your karma.conf.js file.
I just had the same issue in a different setup (Linux, QUnit, Firefox), though. The problem disappeared after I killed all karma processes and did a fresh karma start.
This error can mean that the browser can't find the server. Check if you can reach the server at the URL it mentions. It could be a misconfigured port number, or even (as it was in my case), localhost being misconfigured. I suppose it could be the server is not running, or any of a dozen other problems.
Check if you can reach the server manually. If you can't, fix that.
In my case the environment variable CHROME_BIN was set to a wrong path. Fix the value (might be defined in /etc/environment) or unset the environment variable temporarily with unset CHROME_BIN.
To find the cause of the problem it was helpful to set log level to debug in Karma's config file:
logLevel: config.LOG_DEBUG,
I had the same problem and it was a bug in a test. Removing that code fixed the error.
This happened to me when I had an endless loop in my code. Commenting out (using xdescribe in jasmine) recent code I wrote got the tests running again.
I had the same problem and tried a lot of the suggested solutions I found, but what finally solved it for me was to delete the node_modules folder and getting everything new via npm install.
same issue: Karma - Chrome failed 2 times (cannot start). Giving up
I had the same problem, no code changes, just re-running tests that worked a month ago.
When I ran from webstorm, it seemed to be taking a long time to capture the browser, so I wondered if it was timing out.
So I tried setting browser to just Chrome (i was using phantomjs as well), and waiting. Then reset back to Chrome and PhantomJS, now it's fine.
I had the same problem and I did all what everyone from here said without any result. In my case I didn't do the init:
C:\Users>cd c:\xampp\htdocs\angularjs-pro\ch07
c:\xampp\htdocs\angularjs-pro\ch07>karma init
Which testing framework do you want to use ?
Press tab to list possible options. Enter to move to the next question.
> jasmine
Do you want to use Require.js ?
This will add Require.js plugin.
Press tab to list possible options. Enter to move to the next question.
> no
Do you want to capture any browsers automatically ?
Press tab to list possible options. Enter empty string to move to the next question.
> Chrome
The second time when I got in the same situation was when I tried to do
karma start in the wrong folder, not where I put karma init.
In both cases after I fixed the problem I got in widows console:
22 11 2016 14:06:51.697:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
22 11 2016 14:06:51.698:INFO [launcher]: Launching browser Chrome with unlimited concurrency
22 11 2016 14:06:51.711:INFO [launcher]: Starting browser Chrome
22 11 2016 14:06:53.936:INFO [Chrome 54.0.2840 (Windows 10 0.0.0)]: Connected on socket /#HnJUQrkRPqtGFa5ZAAAA with id 16058274
Installing karma-cli npm install -g karma-clisolved this issue for me.
Hope this helps.
I was facing the same issue and i resolved it by killing all the chrome instances from Task Manager. It seems that there were 'n' number of test runners running. When you close the karma test runner without stopping the server, it's instance is still alive.
So press 'alt + ctrl + delete', kill all sessions/instances of chrome and restart Karma.
It works!
Had the same error recently:
a project using angular-cli,
tests running on Jenkins in a headless environment.
Turns out I have cancelled a Jenkins job previously in the step where the tests were running, so ng test did not quit (had a handful of running ng processes, paired with [node] <defunct> ones, blocking the further test execution). Solution was to find and kill all hanging ng processes:
$ sudo -su jenkins
# ps aux | grep "ng$" | awk '{print $2}' | xargs kill -9
I had the same message because there was no Chrome installed on my PC. Just Chromium. Well, it turns out that's not the same. Now after installing Chrome, both 'Chrome' and 'ChromeHeadless' browsers are able to start.
Otherwise you might specify 'Chromium' or 'ChromiumHeadless' in config and be fine with it.
TLDR: Make sure you have actually installed the browser you are testing in.

WebStorm, Karma, and Angular. How to debug the angular service

I suppose I'm asking about what the proper procedure is.
I'm running Webstorm 7.0.3. I do not have node setup. I'm using the in place hosting on my local machine.
I have written unit tests, and can put a debug breakpoint in the test.js file. When I right-click and 'debug' my karma.conf.js file, it hits these breakpoints fine. Which is great for for basic stuff, but sometimes I need to go line by line inside a service.
Any breakpoints I place inside the service get skipped and ignored.
I have to assume this is is because karma is wrapping my actual script into some proxy file for execution.
How do I step through the service line by line? I feel like I missed something very obvious.
[Update]
#Eitan's comment was what i needed.
I assumed the karma task runner automatically wrapped all source up into the indecipherable un-debuggable non-sense. I never stopped to assume that putting the coverage pre-processor was responsible for it. Commenting the coverage reporter and pre-processor for karma-coverage now allows me to debug my unit tests from the karma chrome instance and from webstorm.
Are using the coverage reporter? It uses instrumented code so I guess you won't be able to debug your original file
You could launch chrome in your karma config and step through your service with dev-tools. I don't know if you can actually do it from within WebStorm because of the exact reason you mentioned (karma server). Just click the big DEBUG button on the upper right of the karma controlled chrome instance and open the scripts in dev-tools

Categories

Resources