How to debug phantomjs tests in another browser
What can you do if your karma unit tests are failing or throwing an error?
Coding unit tests creates a similar set of problems as programming the code you wish to test.
- They can throw an error
- They can throw an exception
- Unexpectantly failing a test
- Just plain failing
Running karma with phantomjs
To use phantomjs as your testing browser in karma your need to install the node package karma-phantomjs-launcher and then reference it in your karma config.
But if it is a headless browser running in a console how do debug it?
Phantomjs is a headless browser that runs in your console which, is great when your tests are all passing.
But if your output looks like below then you probably wish you could open the testing browsers Developer Tools and debug the test manually.
You can with karma
Even though, using karma as your test runner and phantomjs as the headless browser, you can still step through your code in another browsers Developer Tools.
In your karma config you specify the port that karma’s webserver will be listening on.
You can combine your local host address, the port number and the karma debug page in your browser of choice and then step through the code.
If you want to start debugging at a particular point you can add a debugger statement to your tests to break at that line.