angular: [testing] inject could have a better name now that it solves the async problem also

With injectAsync deprecated, I’ve got a lot of tests that looks like:

describe('RaceService', () => {
  let service;
  beforeEachProviders(() => [RaceService]);
  beforeEach(inject([RaceService], raceService => service = raceService));

  it('should return a promise of 2 races', inject([],() => {
    service.list().then(races => {
      expect(races.length).toBe(2);
    });
  }));
});

Works great, but it feels awkward that the test has to use a method called inject, not because the test needs to have something injected but to be sure the asynchronism is correctly handled. Maybe that should be decoupled in another function with another name?

cc @juliemr @wardbell

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 27 (26 by maintainers)

Most upvoted comments

For tests that may still have async behavior that zone can’t predict, such as a WebSocket or WebWorker, there should still be an escape hatch to get a hold of jasmine’s done when using inject. In a quick discussion with @juliemr, we considered having it as the last parameter inside the injectable function:

it('should receive a messages', inject([Dep1], (dep1, done) => {
  var socket = new WebSocket(url);
  socket.onmessage = (msg) => { 
    expect(msg).toEqual({...}); 
    socket.close();
    done();
  };
}));

@cexbrayat - sync() would definitely be optional. It would provide an extra assertion that everything in the test is synchronous and makes the intention of the test clearer. I don’t think we want it to be the default though since you could potentially still write an async test without using aysnc().