Updating the version of Jasmine used in karma-jasmine

You can know the jasmine version you are using by running the following Spec: describe(‘Test to print out jasmine version’, function() { it(‘prints jasmine version’, function() { console.log(‘jasmine-version:’); console.log(jasmine.version || (jasmine.getEnv().versionString && jasmine.getEnv().versionString())); }); }); and then checking the karma output in your console or browser. It should be something like: LOG: ‘jasmine-version:’ LOG: ‘2.3.4’ … Read more

Angular testing how to prevent ngOnInit call to test a method directly

Preventing lifecycle hook (ngOnInit) from being called is a wrong direction. The problem has two possible causes. Either the test isn’t isolated enough, or testing strategy is wrong. Angular guide is quite specific and opinionated on test isolation: However, it’s often more productive to explore the inner logic of application classes with isolated unit tests … Read more

Angular – unit test for a subscribe function in a component

You need this for version rxjs@6 and above. For older rxjs version answer is below: import { of } from ‘rxjs’; it(“should call getUsers and return list of users”, async(() => { const response: User[] = []; spyOn(userService, ‘getUsers’).and.returnValue(of(response)) homeComponent.getUsers(); fixture.detectChanges(); expect(homeComponent.listOfUsers).toEqual(response); })); For old rxjs version change import from: import { of } from … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)