Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When karma-jasmine gives `WARN:` messages, how do I track down which spec file or component is causing them?

I have a lot of tests that give warnings.

Example of a ng test output

It's almost impossible to track down which spec file is causing each error without sifting through each spec one by one, enabling and disabling each one. Is there any way I can get the WARN: to show the file/component/service that's causing it?

like image 469
Yash Capoor Avatar asked Jun 27 '20 23:06

Yash Capoor


People also ask

How do I run a spec TS file?

spec. ts extension will run. To run your tests using the Angular CLI, you use the ng test command in your terminal. As a result, Karma will open up the default browser and run all the tests written with the aid of Jasmine and will display the outcome of those tests.

What is karma configuration file in angular?

The karma. conf. js file is a partial Karma configuration file. The CLI constructs the full runtime configuration in memory, based on application structure specified in the angular. json file, supplemented by karma.

How does Jasmine define angular spec?

We use the Jasmine-provided it function to define a spec. The first parameter of it is a text description of what the spec will be testing — in this case we have a defined component. The second parameter is a function that will run the test. We then use Jasmine's expect function to define our expectation.

How do you use karma in Jasmine test cases?

Create an Angular project with jasmine and karma By doing this, the jasmine and karma configuration is resolved for us. Install and create a new project with angular-cli: Install npm -g @angular/cli. Ng new angular-unit-testing angular unit-testing.


1 Answers

2021 January

I was looking for a good solution (like enable some option in karma.conf.js for example) but didn't find any...

One way I found is to install this library karma-spec-reporter (https://www.npmjs.com/package/karma-spec-reporter). It shows you each test executed in order. So to track down warnings you just have to look at the component's name below the warning message.

Example: screen_capture_running_tests.png

In my example warnings are related to EditGroupComponent

To install:

npm i karma-spec-reporter

Then add these lines to your karma.conf.js:

config.set({
  ...

  plugins: [
    ...
    require('karma-spec-reporter')
  ],

  // With extra options if you want
  specReporter: {
    suppressErrorSummary: false, // do not print error summary
    suppressFailed: false,       // do not print information about failed tests
    suppressPassed: false,       // do not print information about passed tests
    suppressSkipped: true,       // do not print information about skipped tests
    showSpecTiming: false,       // print the time elapsed for each spec
    failFast: false              // test would finish with error when a first fail occurs. 
  },

  // And replace 'progress' by 'spec'
  reporters: ['spec'],

  ...
});

If you don't like having another dependencie in your project. Install it, correct all and uninstall it.

like image 78
Jaraxxus Avatar answered Sep 28 '22 03:09

Jaraxxus