Jest-preset-angular: Getting "done is not a function" errors when updating to 8.1.0

Created on 11 Mar 2020  路  11Comments  路  Source: thymikee/jest-preset-angular

Downgrading back to 8.0.0 removes the error, so there is something in this update that is causing done() not be recognized as a function in any of my spec files.

Bug

Most helpful comment

Ok, I know how to fix this one, will create a PR now.

All 11 comments

Can you provide an example of your test?

The code below works fine on 8.0.0 but errors on 8.1.0. There were others, but I got rid of unnecessary uses of done, but I need to use it here.

  it('should call setTitle with activatedEventData', async done => {
    (fixture.ngZone as NgZone).run(async () => {
      await router.navigate(['']);
    });

    fixture.detectChanges();

    fixture.whenStable().then(() => {
      expect(activatedEventSpy).toHaveBeenCalled();
      expect(setTitleSpy).toHaveBeenCalledWith('Home');
      expect(setTitleServiceSpy).toHaveBeenCalled();
      done();
    });
  });

@JiaLiPassion can you have a quick look at this?
It might be due to the usage of Promises + done, specifically the change from

https://github.com/thymikee/jest-preset-angular/blob/7c72e9385117ff7b4f89067536689e7beeb3c9cf/src/zone-patch/index.js#L51-L53
to
https://github.com/thymikee/jest-preset-angular/blob/17dc5bf93caf40a6bb469cb54083d86a8f01115b/src/zone-patch/index.js#L51-L53
While I personally would not mix promises and done, but prefer a test as follows, still we should support these kind of tests.

    fixture.detectChanges();

    await fixture.whenStable()
    expect(activatedEventSpy).toHaveBeenCalled();
    expect(setTitleSpy).toHaveBeenCalledWith('Home');
    expect(setTitleServiceSpy).toHaveBeenCalled();

@wtho, sure, Got it, I will check it now.

probably related to changes in zone-patch with this test

import { async } from '@angular/core/testing';
import { Injectable } from '@angular/core';
import { fromEvent, Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root',
})
class MessagingService {
  private readonly message$: Observable<MessageEvent>;

  constructor() {
    this.message$ = fromEvent<MessageEvent>(window, 'message').pipe(
      tap(data => {
        console.warn(data);
      }),
    );
  }

  getMessage$(): Observable<MessageEvent> {
    return this.message$;
  }
}

describe('Async test', () => {
  let messagingService = new MessagingService();

  beforeEach(() => {
    messagingService.getMessage$().subscribe();
  });

  test(`should print console warn`, async(() => {
    window.postMessage({
      foo: 'foo',
    }, '*');
  }));
});

8.1.0 console.warn doesn't print anything while 8.0.0 there is console.warn

Yeah, I know where the problem is, will need to find out how to fix it, because revert to original logic

 return testBody.length === 0 
   ? () => testProxyZone.run(testBody, null) 
   : done => testProxyZone.run(testBody, null, [done]); 

will not support the case like this.

it.each([[1, 2]])('it.each', (arg1, arg2) => {
  expect(arg1).toBe(1);
  expect(arg2).toBe(2);
  done();
});

Ok, I know how to fix this one, will create a PR now.

Awesome!
Would be great if the PR includes a test case for this.

@wtho, I created a PR #357, please review. Thanks.

@qdouble can you please confirm this is resolved in v8.1.1?

@wtho it appears to work just fine now, thanks.

Was this page helpful?
0 / 5 - 0 ratings