How do I unit test if an element is visible when the *ngIf directive is used using Jasmine in Angular

If the element is hidden, then it wont be rendered inside the dom. You can check expect(fixture.debugElement.query(By.css(‘.header’))).toBeUndefined(); EDIT : toBeNull() works better in the above case expect(fixture.debugElement.query(By.css(‘.header’))).toBeNull(); And also you have a syntax error while fetching the button element. nativeElement is not a function. Change it this way : const button = fixture.debugElement.query(By.css(‘button’)).nativeElement;

How do I exclude files from karma code coverage report?

You can use several techniques here: karma uses minimatch globs for file paths and use can take advantage of that to exclude some paths. As first solution I’d say try to add only the paths of the file to preprocess with the coverage: // karma.conf.js module.exports = function(config) { config.set({ files: [ ‘src/**/*.js’, ‘test/**/*.js’ ], …

Read more

MatDialog Service Unit Test Angular 6 Error

Testing mat-dialogs can be tricky. I tend to use a spy object for the return from a dialog open (dialogRefSpyObj below) so I can more easily track and control tests. In your case it might look something like the following: describe(‘ModalService’, () => { let modalService: ModalService; let dialogSpy: jasmine.Spy; let dialogRefSpyObj = jasmine.createSpyObj({ afterClosed …

Read more

Expect not toThrow function with arguments – Jasmine

toThrow matcher requires function to be passed as argument to expect so you can simply wrap your function call in anonymous function: expect(function() { myFunc(arg1, arg2, arg3); }).not.toThrow(); You can also use bind to create new ‘version’ of your function that when called will be passed provided arguments: expect(myFunc.bind(null, arg1, arg2, arg3)).not.toThrow();

Angular 4 – Failed: Can’t resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?)

You want to inject a fake ActivatedRoute to your component, since you create it yourself in the test, and the router thus doesn’t create it for you and inject an ActivatedRoute. So you can use something like this: describe(‘SomeComponent’, () => { const fakeActivatedRoute = { snapshot: { data: { … } } } as …

Read more