Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REPL tool for angular/jasmine/karma

I would like to have something like binding.pry in ruby, basically, I want to be able to add a line to my code, and have a debugger stop there, while karma is running my angular/jasmine tests

it('runs my jasmine test', function () {
  var a = true;
  binding.pry // stops code and enters REPL prompt
  expect(a).toBe(true);
});

The result would then be a prompt

#

Where I could do things to the variables available in that scope, at that point in time

# a = false;

Then I could exit and continue execution.

# exit

Just like debugging with dev tools, but I would like to have this outside of the browser environment and inside the terminal under a karma process. I've also found https://github.com/alidavut/locus, however it doesn't seem to work under karma.

like image 823
Cosmin Avatar asked Jan 15 '15 20:01

Cosmin


1 Answers

I'm not aware of any way to launch a repl in the karma process, but what you can do is simply write:

debugger;

at the point where you want to debug. Then, if you have the browser's dev tools already open when that line is executed, the execution will pause and you'll be able to use "watch expressions" which might be enough for you. You have access to the call stack and all the local variables. You can also assign to local variables in a watch expression and the new values will persist when you resume execution.

I've only tested this on Chrome. What I have to do is:

  1. Put the debugger; statement in.
  2. Start karma.
  3. Open the Chrome dev tools.
  4. Save one of the watched karma files (so now the tests will run again with the dev tools already open).
  5. Profit!

Making a REPL on the karma side would require a lot more effort as all the test code is executed on the browser. To control a REPL from the karma process you would need to setup events to communicate via the sockets that karma sets up to talk to the browser. Should be doable though if you're so inclined. EDIT: actually, to do this you would still need to be able to make Javascript block execution at a particular statement, and I'm pretty sure that debugger; is the only way to do this.

like image 92
Andrew Magee Avatar answered Nov 03 '22 06:11

Andrew Magee