Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zone.js): add jest fakeTimers support #39016

Closed
wants to merge 1 commit into from

Conversation

JiaLiPassion
Copy link
Contributor

Close #38851, support jest fakeTimers APIs' integration with fakeAsync().
After enable this feature, calling jest.useFakeTimers() will make all test
run into fakeAsync() automatically.

beforeEach(() => {
    jest.useFakeTimers('modern');
  });
  afterEach(() => {
    jest.useRealTimers();
  });

  test('should run into fakeAsync() automatically', () => {
    const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
    expect(fakeAsyncZoneSpec).toBeTruthy();
  });

Also there are mappings between jest and zone APIs.

  • jest.runAllTicks() will call flushMicrotasks().
  • jest.runAllTimers() will call flush().
  • jest.advanceTimersByTime() will call tick()
  • jest.runOnlyPendingTimers() will call flushOnlyPendingTimers()
  • jest.advanceTimersToNextTimer() will call tickToNext()
  • jest.clearAllTimers() will call removeAllTimers()
  • jest.getTimerCount() will call getTimerCount()

@JiaLiPassion JiaLiPassion marked this pull request as draft September 27, 2020 14:29
@JiaLiPassion JiaLiPassion requested review from mhevery and removed request for IgorMinar September 28, 2020 00:15
@JiaLiPassion JiaLiPassion force-pushed the jest-fake-timer branch 2 times, most recently from 4be1bee to 8347cd5 Compare September 29, 2020 07:12
@JiaLiPassion JiaLiPassion added the target: major This PR is targeted for the next major release label Sep 29, 2020
@JiaLiPassion JiaLiPassion marked this pull request as ready for review September 29, 2020 12:11
Close angular#38851, support `jest` fakeTimers APIs' integration with `fakeAsync()`.
After enable this feature, calling `jest.useFakeTimers()` will make all test
run into `fakeAsync()` automatically.

```
beforeEach(() => {
    jest.useFakeTimers('modern');
  });
  afterEach(() => {
    jest.useRealTimers();
  });

  test('should run into fakeAsync() automatically', () => {
    const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
    expect(fakeAsyncZoneSpec).toBeTruthy();
  });
```

Also there are mappings between `jest` and `zone` APIs.

- `jest.runAllTicks()` will call `flushMicrotasks()`.
- `jest.runAllTimers()` will call `flush()`.
- `jest.advanceTimersByTime()` will call `tick()`
- `jest.runOnlyPendingTimers()` will call `flushOnlyPendingTimers()`
- `jest.advanceTimersToNextTimer()` will call `tickToNext()`
- `jest.clearAllTimers()` will call `removeAllTimers()`
- `jest.getTimerCount()` will call `getTimerCount()`
@ahnpnl
Copy link
Contributor

ahnpnl commented Sep 30, 2020

I like this 👍

  • Regarding to mapping between jest and zone APIs, that can be added to jest-preset-angular README so users don't always have to look into this PR.

  • Regarding to detail usage, maybe jest-preset-angular can add to README as well for convenience.

@JiaLiPassion
Copy link
Contributor Author

@ahnpnl , thanks, and this feature will not affect current zone.js integration with jest. So we can keep the backward compatibility for jest version <27, and also since jest use kind of sandbox for every test file, so I have to use a new test environment https://github.com/angular/angular/pull/39016/files#diff-90d0b342f3b5fa57a61adfc412606ddc to do the monkey patch, I will create a PR in jest-angular-preset to do it.

And since I am planning to add a new doc for jest integration later, so I will create doc in another PR.

@mhevery
Copy link
Contributor

mhevery commented Oct 1, 2020

presubmit

@IgorMinar IgorMinar added the action: merge The PR is ready for merge by the caretaker label Oct 5, 2020
Copy link
Contributor

@IgorMinar IgorMinar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, but I have to admit that I don't have the domain knowledge to thoroughly review some parts of this PR.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes target: major This PR is targeted for the next major release
Projects
None yet
5 participants