Karma tests with modal problem


Topic: Karma tests with modal problem

jimsgroup pro asked 7 years ago

I'm have unit test that is simply testing that a component is created. However, I am using md-modal in that component and I get the following error when attempting run run the unit test
Failed: Template parse errors: There is no directive with "exportAs" set to "mdb-modal" ("ngIf="isModalShown" [config]="{ show: true }" (onHidden)="onExistingCustomerModalHidden()" mdbModal [ERROR ->]#existingCustomerModal="mdb-modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">"): ng:///DynamicTestModule/SubServicesChooseComponent.html@23:106 Can't bind to 'config' since it isn't a known property of 'div'. ("
Is there something I need to do to get the unit test to work?

Sebastian Kaczmarek staff pro premium answered 7 years ago

Hi jimsgroup,

If you are getting this error it means that you forgot to import MDBBootstrapModule into your app.component.spec.ts file. Remember, that Karma does not use the same modules as your app. It uses different modules and because of that, you have to import to your spec files every dependency used in your app.

So first thing you need to do is to add this line to your tsconfig.spec.json file:

"include": [
    "../node_modules/ng-mdb-pro/index.ts", // <-- this line
    "**/*.spec.ts",
    "**/*.d.ts"
  ]

Next, add this to your app.component.spec.ts file:

import { MDBBootstrapModules } from 'ng-mdb-pro';
import { NO_ERRORS_SCHEMA } from '@angular/core';

Then make the similar block of code look like this:

describe('AppComponent', () => {

beforeEach(async(() => {

TestBed.configureTestingModule({

imports: [

MDBBootstrapModules.forRoot() // <-- this line is important

],

declarations: [

AppComponent

],

schemas: [

NO_ERRORS_SCHEMA // <-- this line is also important

]

}).compileComponents();

}));

it('should create the app', async(() => {

constfixture=TestBed.createComponent(AppComponent);

constapp=fixture.debugElement.componentInstance;

expect(app).toBeTruthy();

}));

});

Now run npm test again and see if it helps.

Best regards,
Sebastian


syedadeel2 commented 7 years ago

After following your post I'm getting new error: Failed: Cannot read property 'forRoot' of undefined TypeError: Cannot read property 'forRoot' of undefined at UserContext. (http://localhost:9876/_karma_webpack_/webpack:/src/app/components/common/header/header.component.spec.ts:22:28) at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:388:1) at AsyncTestZoneSpec.webpackJsonp../node_modules/zone.js/dist/zone-testing.js.AsyncTestZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:713:1) at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:285:1) at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:387:1) at Zone.webpackJsonp../node_modules/zone.js/dist/zone.js.Zone.runGuarded (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:151:1) at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/esm5/testing.js:107:1) at UserContext. (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/esm5/testing.js:46:1) at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:388:1) at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:288:1)

Sebastian Kaczmarek staff pro premium commented 7 years ago

Hi syedadeel2, It's been a while since the error above occurred. MDB Angular has changed a lot since then. Please tell me, which version of MDB Angular do you use? Also, please provide me with the version of CLI and Karma which you use. If there is any more important info, provide it as well. Regards, Sebastian

Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue
  • User: Pro
  • Premium support: No
  • Technology: MDB Angular
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: Yes
  • Provided link: No