-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@schematics/angular): migrate fakeAsync's flush behavior when used in beforeEach #33135
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -154,28 +154,79 @@ describe('transformFakeAsyncTest', () => { | |
| }, | ||
| { | ||
| description: | ||
| 'should transform fakeAsync test to `vi.useFakeTimers()` in `beforeEach`, `afterEach`, `beforeAll`, `afterAll`', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I split the different hooks in different tests as |
||
| 'should transform fakeAsync test to `vi.useFakeTimers()` in `beforeEach` and preserve flush behavior', | ||
| input: ` | ||
| import { fakeAsync } from '@angular/core/testing'; | ||
|
|
||
| describe('My fakeAsync suite', () => { | ||
| beforeAll(fakeAsync(() => { | ||
| console.log('beforeAll'); | ||
|
|
||
| let count = 0; | ||
| beforeEach(fakeAsync(() => { | ||
| setTimeout(() => ++count, 100); | ||
| })); | ||
|
|
||
| afterAll(fakeAsync(() => { | ||
| console.log('afterAll'); | ||
| it('works', fakeAsync(() => { | ||
| expect(count).toBe(1); | ||
| })); | ||
| }); | ||
| `, | ||
| expected: ` | ||
| describe('My fakeAsync suite', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers({ advanceTimeDelta: 1, shouldAdvanceTime: true }); | ||
| }); | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| beforeEach(fakeAsync(() => { | ||
| console.log('beforeEach'); | ||
| })); | ||
| let count = 0; | ||
| beforeEach(async () => { | ||
| setTimeout(() => ++count, 100); | ||
| await vi.runOnlyPendingTimersAsync(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what mimics best While writing this, I realize that it would be more consistent to flush pending timers everywhere and not just in |
||
| }); | ||
|
|
||
| it('works', async () => { | ||
| expect(count).toBe(1); | ||
| }); | ||
| }); | ||
| `, | ||
| }, | ||
| { | ||
| description: 'should transform fakeAsync test to `vi.useFakeTimers()` in `afterEach`', | ||
| input: ` | ||
| import { fakeAsync } from '@angular/core/testing'; | ||
|
|
||
| describe('My fakeAsync suite', () => { | ||
| afterEach(fakeAsync(() => { | ||
| console.log('afterEach'); | ||
| })); | ||
| }); | ||
| `, | ||
| expected: ` | ||
| describe('My fakeAsync suite', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers({ advanceTimeDelta: 1, shouldAdvanceTime: true }); | ||
| }); | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
| afterEach(async () => { | ||
| console.log('afterEach'); | ||
| }); | ||
| }); | ||
| `, | ||
| }, | ||
| { | ||
| description: 'should transform fakeAsync test to `vi.useFakeTimers()` in `beforeAll`', | ||
| input: ` | ||
| import { fakeAsync } from '@angular/core/testing'; | ||
|
|
||
| describe('My fakeAsync suite', () => { | ||
| beforeAll(fakeAsync(() => { | ||
| console.log('beforeAll'); | ||
| })); | ||
| }); | ||
| `, | ||
| expected: ` | ||
| describe('My fakeAsync suite', () => { | ||
| beforeEach(() => { | ||
|
|
@@ -187,17 +238,30 @@ describe('transformFakeAsyncTest', () => { | |
| beforeAll(async () => { | ||
| console.log('beforeAll'); | ||
| }); | ||
| }); | ||
| `, | ||
| }, | ||
| { | ||
| description: 'should transform fakeAsync test to `vi.useFakeTimers()` in `afterAll`', | ||
| input: ` | ||
| import { fakeAsync } from '@angular/core/testing'; | ||
|
|
||
| afterAll(async () => { | ||
| describe('My fakeAsync suite', () => { | ||
| afterAll(fakeAsync(() => { | ||
| console.log('afterAll'); | ||
| })); | ||
| }); | ||
| `, | ||
| expected: ` | ||
| describe('My fakeAsync suite', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers({ advanceTimeDelta: 1, shouldAdvanceTime: true }); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| console.log('beforeEach'); | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| console.log('afterEach'); | ||
| afterAll(async () => { | ||
| console.log('afterAll'); | ||
| }); | ||
| }); | ||
| `, | ||
|
|
@@ -240,6 +304,65 @@ describe('transformFakeAsyncTest', () => { | |
| }); | ||
| `, | ||
| }, | ||
| { | ||
| description: 'should not append `vi.runOnlyPendingTimersAsync()` in `test` or `afterEach`', | ||
| input: ` | ||
| import { fakeAsync } from '@angular/core/testing'; | ||
|
|
||
| describe('My fakeAsync suite', () => { | ||
| afterEach(fakeAsync(() => { | ||
| console.log('afterEach'); | ||
| })); | ||
|
|
||
| it('works', fakeAsync(() => { | ||
| expect(1).toBe(1); | ||
| })); | ||
| }); | ||
| `, | ||
| expected: ` | ||
| describe('My fakeAsync suite', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers({ advanceTimeDelta: 1, shouldAdvanceTime: true }); | ||
| }); | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
| afterEach(async () => { | ||
| console.log('afterEach'); | ||
| }); | ||
|
|
||
| it('works', async () => { | ||
| expect(1).toBe(1); | ||
| }); | ||
| }); | ||
| `, | ||
| }, | ||
| { | ||
| description: | ||
| 'should not append `vi.runOnlyPendingTimersAsync()` if `flush` option is set to false', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To keep the migration simple, we only detect object expression with |
||
| input: ` | ||
| import { fakeAsync } from '@angular/core/testing'; | ||
|
|
||
| describe('My fakeAsync suite', () => { | ||
| beforeEach(fakeAsync(() => { | ||
| console.log('beforeEach'); | ||
| }, {flush: false})); | ||
| }); | ||
| `, | ||
| expected: ` | ||
| describe('My fakeAsync suite', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers({ advanceTimeDelta: 1, shouldAdvanceTime: true }); | ||
| }); | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
| beforeEach(async () => { | ||
| console.log('beforeEach'); | ||
| }); | ||
| }); | ||
| `, | ||
| }, | ||
| ]; | ||
|
|
||
| testCases.forEach(({ description, input, expected }) => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.