Angular 2 Unit Tests: Cannot find name ‘describe’
I hope you’ve installed – npm install –save-dev @types/jasmine Then put following import at the top of the hero.spec.ts file – import ‘jasmine’; It should solve the problem.
I hope you’ve installed – npm install –save-dev @types/jasmine Then put following import at the top of the hero.spec.ts file – import ‘jasmine’; It should solve the problem.
Each of your .spec.ts file have all its tests grouped in describe block like this: describe(‘SomeComponent’, () => {…} You can easily run just this single block, by prefixing the describe function name with f: fdescribe(‘SomeComponent’, () => {…} If you have such function, no other describe blocks will run. Btw. you can do similar … Read more
IMO the best solution for this use case is to just make a synchronous mock service . You can’t use fakeAsync for this particular case because of the XHR call for templateUrl. And personally I don’t think the “hack” to make ngOnInit return a promise is very elegant. And you should not have to call … Read more
Per the documentation (emphasis mine): You can create a spy object with several properties on it quickly by passing an array or hash of properties as a third argument to createSpyObj. In this case you won’t have a reference to the created spies, so if you need to change their spy strategies later, you will … Read more
The problem is that you just overwrite a property of the window object. And if you can do that, the browser can do that as well. So mocking a function or property of a global object that everyone can access isn’t a good idea in general, because you can never be sure that your changes … Read more
If you want to mock a module that declare one or more services I have used this code: beforeEach(function(){ module(‘moduleToMock’); module(function ($provide) { $provide.value(‘yourService’, serviceMock); }); }); This is useful if the service you want to mock is also a service that you want to unit test (in another jasmine describe). The solution proposed by … Read more
The correct way for Angular (ver. 2+): it(‘retrieves all the cars’, waitForAsync(inject([CarService], (carService) => { carService.getCars().subscribe(result => expect(result.length).toBeGreaterThan(0)); })); Async Observables vs Sync Observables It is important to understand that Observables can be either synchronous or asynchronous. In your specific example the Observable is asynchronous (it wraps an http call). Therefore you have to use … Read more
See angular element api docs. If you use element.scope() you get the element’s scope that you defined in the scope property of your directive. If you use element.isolateScope() you get the entire isolated scope. For example, if your directive looks something like this : scope : { myScopeThingy : ‘=’ }, controller : function($scope){ $scope.myIsolatedThingy … Read more
My objective is to check if the ‘onEditButtonClick’ is getting invoked when the user clicks the edit button and not checking just the console.log being printed. You will need to first set up the test using the Angular TestBed. This way you can actually grab the button and click it. What you will do is … Read more
Depending on what you want to do, you can try: browser.waitForAngular(); or btnLoginEl.click().then(function() { // do some stuff }); to solve the promise. It would be better if you can do that in the beforeEach. NB: I noticed that the expect() waits for the promise inside (i.e. getCurrentUrl) to be solved before comparing.