Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Configure the Angular CLI to use the Karma Mocha test reporter

    Juri StrumpflohnerJuri Strumpflohner

    Every Angular CLI generated project comes already with Karma preinstalled as well a couple of executable Jasmine specs. The default test reporter is "progress" which simply logs out the number of passed and failed tests. In this lesson we learn how to add the Karma Mocha test reporter to our Angular CLI setup.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: When we create a new project with the Angular CLI, it already configures a couple of things for us. One of those things is automated test running. As you can see here, we have a kernel configuration file, which is basically the driver which runs our tests.

    In the source folder, we have a couple of specs defined which follow the Jasmine approach of defining tests. In the package.json, we have already a test script in place. Therefore, if we open up our terminal, and we run npm test, it will execute our Karma tests.

    However, the output here is not as nice as it could be. The only thing we see, that there have been executed three tests, and three of three, basically, run successfully. The reason for that is because the default runner is basically the progress runner, which is configured with Karma.

    If we go into the Karma configuration down here in the reporter section, we see progress. Now, what we can do is to install the Mocha reporter, which is a much nicer way of visualizing the test result. Let's open up our console, and run npm install karma-mocha-reporter, and save it to the dev section.

    Next, we can go to the Karma config again. At the very top, where the plugin section is, we add our new Mocha reporter. We import it here. Finally, we need to go down here, and we can simply remove that one, and enter here Mocha as the reporter.

    We save, and we execute again, npm test. We now get a much better and nicer looking overview of which test seeds have been executed, as well as how many tests there are in there, which of them have passed, and which of them have failed.