Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 891
jbasko Avatar asked Feb 06 '14 12:02

jbasko


People also ask

How do I run a test case in AngularJS?

Load the Angular App beforeEach(module('MyApp')); //3. Describe the object by name describe('compute', function () { var compute; //4. Initialize the filter beforeEach(inject(function ($filter) { compute = $filter('compute', {}); })); //5. Write the test in the it block along with expectations.

What is console log in AngularJS?

AngularJS includes a logging service called $log, which logs the messages to the client browser's console. The $log service includes different methods to handle the log for error, information, warning or debug information. It can be useful in debugging and auditing.


2 Answers

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);
}));
like image 197
Carl G Avatar answered Nov 07 '22 04:11

Carl G


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);
like image 36
jbasko Avatar answered Nov 07 '22 04:11

jbasko