Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine 2.0 async done() and angular-mocks inject() in same test it()

My usual test case looks like

it("should send get request", inject(function(someServices) {      //some test })); 

And Jasmine 2.0 async test should look like

it("should send get request", function(done) {      someAsync.then(function(){          done();      }); }); 

How can I use both done and inject in one test?

like image 772
huston007 Avatar asked Aug 12 '14 21:08

huston007


2 Answers

This should work; I ran into the same problem when I updated to Jasmine 2.0

it("should send get request", function(done) {     inject(function(someServices) {         //some async test         done();     })(); // function returned by 'inject' has to be invoked }); 
like image 192
Scott Boring Avatar answered Sep 19 '22 19:09

Scott Boring


An IMPORTANT note is the brackets after the inject call. Eg.

inject(function(someServices) {    //some async test    done(); })();  <-- these brackets here important. 

If you look at the type of inject:

export declare function inject(tokens: any[], fn: Function): () => any; 

You can see it returns a function, so you were getting no output because you forgot to call the function!!

If you think about it, it makes sense that it returns a function, because it takes a function!

So the extra parentheses should solve all problem!

Working Example:

  it('should allow you to observe for changes', function(done) {     inject([GlobalStateService], (globalStateService: GlobalStateService) => {       globalStateService.observe("user", storageType.InMemoryStorage, (user: string) => {         expect(user).toBe("bla");         done();       });        globalStateService.write({ user: "bla"}, storageType.InMemoryStorage);     })();   }); 
like image 33
Arie Milner Avatar answered Sep 22 '22 19:09

Arie Milner