jbasko
jbasko

Reputation: 7330

How to see $log calls in Terminal when running angularjs unit tests with karma?

In a project based on angular-seed project, I am running unit tests with ./scripts/test.sh --log-level debug but none of the log messages that I see are coming from my app. How do I get to see them? In my app, I am logging with $log.

Upvotes: 10

Views: 4495

Answers (3)

doug31415
doug31415

Reputation: 275

for clarity, you must structure it like this:

beforeEach(module('myapp'));

beforeEach(module('myapp', function($provide) {
  // Output messages
  $provide.value('$log', console);
})); 

Upvotes: 4

Carl G
Carl G

Reputation: 18260

Thanks. As mentioned, tests can inject console for $log using $provide. For posterity, $provide is not available via inject(function(...) { ... }) but instead must be injected into a function argument to module:

beforeEach(module('myapp', function($provide) {
  // Output messages
  $provide.value('$log', console);
}));

Upvotes: 15

jbasko
jbasko

Reputation: 7330

Got it working! Had I used console.log directly it would have worked but now that I am using $log I had to patch it in a beforeEach (looks like by default it wasn't wired by angularjs):

$provide.value('$log', console);

Upvotes: 12

Related Questions