Questions tagged [karma-jasmine]

Karma-Jasmine is an adapter for the Jasmine testing framework, which is shipped with Karma by default.

Karma-Jasmine is an adapter for the Jasmine testing framework.

Installation

This plugin ships with Karma by default, and there isn't any need to install it.

The easiest way is to keep karma-jasmine as a devDependency in your package.json file.

{
  "devDependencies": {
    "karma": "~0.10",
    "karma-jasmine": "~0.1"
  }
}

You can simply do it by:

npm install karma-jasmine --save-dev

Configuration

// karma.conf.js
module.exports = function(config) {
  config.set({
    frameworks: ['jasmine'],

    files: [
      '*.js'
    ]
  });
};
6563 questions
33
votes
2 answers

When to use waitForAsync in angular

From documentation we can read: waitForAsync(fn: Function): (done: any) => any Wraps a test function in an asynchronous test zone. The test will automatically complete when all asynchronous calls within this zone are done. Can be used to wrap an…
Thomas Banderas
  • 1,681
  • 1
  • 21
  • 43
31
votes
2 answers

Karma: use Windows' Chrome from WSL

I am trying to launch karma from WSL using the Windows version of Google Chrome. In the karma.conf.js I simply use the Chrome browser: [...], browsers: ['Chrome'], [...] And I export the CHROME_BIN environment variable like this: export…
31
votes
3 answers

Angular 2 Final Release Router Unit Test

How do I unit test routers in Angular version 2.0.0 with karma and jasmine? Here's what my old unit test looks like in version 2.0.0-beta.14 import { it, inject, injectAsync, beforeEach, beforeEachProviders, …
xphong
  • 1,114
  • 2
  • 13
  • 20
30
votes
5 answers

Error: : fromEvent is not declared writable or has no setter

The old code uses rxjs v5.5.12, We copied the same code to our new project which uses rxjs v6.4.0. We are getting this error when we tried to run the test case. Old Code: import * as ObservableEvents from…
Raju
  • 319
  • 1
  • 3
  • 7
30
votes
3 answers

How to test angular 2 component with nested components inside with their own dependencies ? (TestBed.configureTestingModule)

I have a component A that use a component B,c,D in its template: ###template-compA.html ...etc To simplify, let's say their is only one directive in…
Stefdelec
  • 2,711
  • 3
  • 33
  • 40
30
votes
1 answer

What does npm mean by 'Skipping failed optional dependency'?

Latest version of node and npm causing problems in running karma. When I try to install karma-cli npm i -g karma karma-cli I get following warning: npm WARN optional Skipping failed optional dependency /chokidar/fsevents: npm WARN notsup Not…
Mithun Shreevatsa
  • 3,588
  • 9
  • 50
  • 95
29
votes
3 answers

Refactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed. How to refactor and reduce the complexity

how to reduce the complexity of the given piece of code? I am getting this error in Sonarqube---> Refactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed. this.deviceDetails = this.data && {...this.data.deviceInfo} || {}; …
sd_30
  • 576
  • 1
  • 6
  • 21
29
votes
5 answers

Unit Testing/mocking Window properties in Angular2 (TypeScript)

I'm building some unit tests for a service in Angular2. Within my Service I have the following code: var hash: string; hash = this.window.location.hash; However when I run a test which contains this code, it will fail. It'd be great to utilise all…
Rhys
  • 375
  • 1
  • 5
  • 10
28
votes
4 answers

Can webpack 4 modules be configured as to allow Jasmine to spy on their members?

I've been unable to get my test jasmine test suite running with webpack 4. After upgrading webpack, I get the following error for almost every test: Error: : getField is not declared writable or has no setter This is due to a common…
Ryan
  • 599
  • 2
  • 5
  • 14
28
votes
2 answers

Angular 2.0.0 - Testing " imported by the module 'DynamicTestModule' "

I am having a problem in testing app.component.ts in Angular 2. I am using angular-cli. Whenever I run ng test, my app.component.spec.ts makes the console prompt with the error: Failed: Unexpected directive 'HomeModuleComponent' imported by the…
xiotee
  • 1,519
  • 2
  • 14
  • 23
28
votes
5 answers

Conditionally ignore individual tests with Karma / Jasmine

I have some tests that fail in PhantomJS but not other browsers. I'd like these tests to be ignored when run with PhantomJS in my watch task (so new browser windows don't take focus and perf is a bit faster), but in my standard test task and my CI…
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
27
votes
2 answers

Karma Jasmine - Uncaught TypeError: __webpack_require__(...).context is not a function

I was trying to update my whole project dependencies, and managed to do it, but when I try to run the tests with ng test --no-watch they don't work, giving me the following error. 23 11 2022 12:33:58.268:INFO [Chrome Headless 107.0.5304.107 (Windows…
Jarvan Jarvencio
  • 689
  • 2
  • 12
  • 19
27
votes
2 answers

Karma unit testing error: Unexpected value imported by the module. Please add a @NgModule annotation

I've created a fresh new component via: ng g mytest1 Then I changed the constructor line to this: constructor(private dialogRef: MatDialogRef) { } , and added the required import: import { MatDialogRef } from…
MrProgrammer
  • 589
  • 1
  • 7
  • 17
27
votes
1 answer

Failed: Unexpected directive 'ContactDetailsComponent' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation

I have created a new component and did ng test, but failing with the below error Failed: Unexpected directive 'ContactDetailsComponent' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation. import { async,…
Ramana
  • 1,692
  • 3
  • 18
  • 34
27
votes
3 answers

How to make material components work with Karma in unit testing Angular

I have an angular CLI project set up. I've made a form that uses angular material components, like . I'm just starting out with writing my first Karma/Jasmine unit test, following the steps in the angular docs. This is my component…
Snowman
  • 2,465
  • 6
  • 21
  • 32