3

I have created angular 2 a project and a service with angular-cli and tried to test my service.

But API does not fail in async function although it should fail ;moreover, it just ignores those exceptions.

/* tslint:disable:no-unused-variable */

import {
  beforeEach, beforeEachProviders, describe, xdescribe,
  expect, it, xit, async, inject, injectAsync
} from '@angular/core/testing';
import { SearchService } from './search.service';
import {provide} from '@angular/core';
import {MockBackend, MockConnection} from '@angular/http/testing';
import {XHRBackend, Response, ResponseOptions, HTTP_PROVIDERS} from '@angular/http';

describe('Search Service', () => {
  let searchService: SearchService;
  let mockBackend: MockBackend;

  beforeEachProviders(() => [
    HTTP_PROVIDERS,
    MockBackend,
    provide(XHRBackend, { useClass: MockBackend }),
    SearchService
  ]);

  beforeEach(injectAsync([SearchService, MockBackend], (s, m) => {
    searchService = s;
    mockBackend = m;
  }));

  it('async test', () => {
    setTimeout(() => {
      expect(2).toBe(1);
    }, 3000);
  });

It just ignores those minimal test case.

Then I have read some doc and updated my code as follow.

it('async test with done', (done) => {
  setTimeout(() => {
    expect(1).toBe(1);
    done();
  }, 1000);
});

But this time, the test fails although it should pass. The error is as follow.

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

I change default timeout value to bigger value but it is no effect.

2
  • 1
    What Angular2 version are you using? Commented Jun 29, 2016 at 4:14
  • its version is 2.0.0-rc.3 Commented Jun 29, 2016 at 6:50

1 Answer 1

6

injectAsync will not work, use async (stopped working for me after rc2)

angular2 change log beta 16

injectAsync is now deprecated. Instead, use the async function to wrap any asynchronous tests. You will also need to add the dependency 'node_modules/zone.js/dist/async-test.js' as a served file in your Karma or other test configuration.

Before:

it('should wait for returned promises', injectAsync([FancyService], (service) => {
  return service.getAsyncValue().then((value) => { expect(value).toEqual('async value'); });
}));
it('should wait for returned promises', injectAsync([], () => {
  return somePromise.then(() => { expect(true).toEqual(true); });
}));

After:

it('should wait for returned promises', async(inject([FancyService], (service) => {
  service.getAsyncValue().then((value) => { expect(value).toEqual('async value'); });
})));
// Note that if there is no injection, we no longer need `inject` OR `injectAsync`.
it('should wait for returned promises', async(() => {
  somePromise.then(() => { expect(true).toEqual(true); });
}));
Sign up to request clarification or add additional context in comments.

1 Comment

Can you update answer by the update of the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.