angular cli exclude files/directory for `ng test –code-coverage`

With the latest CLI, inside angular.json “test”: { “builder”: “@angular-devkit/build-angular:karma”, “options”: { “main”: “src/test.ts”, “polyfills”: “src/polyfills.ts”, “tsConfig”: “src/tsconfig.spec.json”, “karmaConfig”: “./karma.conf.js”, “codeCoverageExclude”: [“src/testing/**/*”],

Error: You need to include some adapter that implements __karma__.start method

It does seem like this is a very general error, however in my case the problem was either that I didn’t run karma start from the correct folder, or that I didn’t restart it after changing the configuration. I’ll leave this question open and hopefully it can become a resource for others who experience this … Read more

how to fix 404 warnings for images during karma unit testing

That is because you need to configurate karma to load then serve them when requested 😉 In your karma.conf.js file you should already have defined files and/or patterns like : // list of files / patterns to load in the browser files : [ {pattern: ‘app/lib/angular.js’, watched: true, included: true, served: true}, {pattern: ‘app/lib/angular-*.js’, watched: … Read more

UI-router interfers with $httpbackend unit test, angular js

Take this gist https://gist.github.com/wilsonwc/8358542 angular.module(‘stateMock’,[]); angular.module(‘stateMock’).service(“$state”, function($q){ this.expectedTransitions = []; this.transitionTo = function(stateName){ if(this.expectedTransitions.length > 0){ var expectedState = this.expectedTransitions.shift(); if(expectedState !== stateName){ throw Error(“Expected transition to state: ” + expectedState + ” but transitioned to ” + stateName ); } }else{ throw Error(“No more transitions were expected! Tried to transition to “+ stateName ); … Read more

jasmine tests in karma: Uncaught ReferenceError: require is not defined

I was facing same issue, when trying to use require(‘module_name’) (CommonJS style modules) inside a test case and running it using Karma. The reason was require function is not available to browser (it is undefined). To provide it to browser we can browserify the test js files before Karma runs test case in browser using … Read more

Karma run single test

If you are using the Karma/Jasmine stack, use: fdescribe(“when …”, function () { // to [f]ocus on a single group of tests fit(“should …”, function () {…}); // to [f]ocus on a single test case }); … and: xdescribe(“when …”, function () { // to e[x]clude a group of tests xit(“should …”, function () {…}); … Read more

Can Protractor and Karma be used together?

Not recommended by the current maintainer of Protractor: https://github.com/angular/protractor/issues/9#issuecomment-19927049 Protractor and Karma should not be used together; instead they provide separate systems for running tests. Protractor and Karma cover different aspects of testing – Karma is intended mostly for unit tests, while Protractor should be used for end to end testing. Protractor is built on … Read more