Skip to content

Commit 0859a99

Browse files
refactor(core): replace TestBed.flushEffects with tick (angular#60959)
Instead of stabilizing the TestBed.flushEffects() API we intend to replace it with the tick() method (equivalent of ApplicationRef.tick(). The reasoning here is that we prefer tests running the entire synchronization process (as in production apps) instead of invoking parts of the synchronization process in a way that would naver happen in a running application. PR Close angular#60959
1 parent c990265 commit 0859a99

File tree

6 files changed

+84
-61
lines changed

6 files changed

+84
-61
lines changed

goldens/public-api/core/testing/index.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export interface TestBed {
118118
createComponent<T>(component: Type<T>): ComponentFixture<T>;
119119
// (undocumented)
120120
execute(tokens: any[], fn: Function, context?: any): any;
121+
// @deprecated
121122
flushEffects(): void;
122123
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, options?: TestEnvironmentOptions): void;
123124
// (undocumented)
@@ -165,6 +166,7 @@ export interface TestBed {
165166
// (undocumented)
166167
resetTestingModule(): TestBed;
167168
runInInjectionContext<T>(fn: () => T): T;
169+
tick(): void;
168170
}
169171

170172
// @public

packages/common/http/test/resource_spec.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('httpResource', () => {
2525
it('should send a basic request', async () => {
2626
const backend = TestBed.inject(HttpTestingController);
2727
const res = httpResource(() => '/data', {injector: TestBed.inject(Injector)});
28-
TestBed.flushEffects();
28+
TestBed.tick();
2929
const req = backend.expectOne('/data');
3030
req.flush([]);
3131
await TestBed.inject(ApplicationRef).whenStable();
@@ -36,14 +36,14 @@ describe('httpResource', () => {
3636
const id = signal(0);
3737
const backend = TestBed.inject(HttpTestingController);
3838
const res = httpResource(() => `/data/${id()}`, {injector: TestBed.inject(Injector)});
39-
TestBed.flushEffects();
39+
TestBed.tick();
4040
const req1 = backend.expectOne('/data/0');
4141
req1.flush(0);
4242
await TestBed.inject(ApplicationRef).whenStable();
4343
expect(res.value()).toEqual(0);
4444

4545
id.set(1);
46-
TestBed.flushEffects();
46+
TestBed.tick();
4747
const req2 = backend.expectOne('/data/1');
4848
req2.flush(1);
4949
await TestBed.inject(ApplicationRef).whenStable();
@@ -56,21 +56,21 @@ describe('httpResource', () => {
5656
const res = httpResource(() => (id() !== 1 ? `/data/${id()}` : undefined), {
5757
injector: TestBed.inject(Injector),
5858
});
59-
TestBed.flushEffects();
59+
TestBed.tick();
6060
backend.expectOne('/data/0').flush(0);
6161
await TestBed.inject(ApplicationRef).whenStable();
6262
expect(res.value()).toEqual(0);
6363

6464
id.set(1);
65-
TestBed.flushEffects();
65+
TestBed.tick();
6666

6767
// Verify no requests have been made.
6868
backend.verify({ignoreCancelled: false});
6969
await TestBed.inject(ApplicationRef).whenStable();
7070
backend.verify({ignoreCancelled: false});
7171

7272
id.set(2);
73-
TestBed.flushEffects();
73+
TestBed.tick();
7474
backend.expectOne('/data/2').flush(2);
7575
await TestBed.inject(ApplicationRef).whenStable();
7676
expect(res.value()).toBe(2);
@@ -93,7 +93,7 @@ describe('httpResource', () => {
9393
}),
9494
{injector: TestBed.inject(Injector)},
9595
);
96-
TestBed.flushEffects();
96+
TestBed.tick();
9797
const req = backend.expectOne('/data?fast=yes');
9898
expect(req.request.method).toBe('POST');
9999
expect(req.request.body).toEqual({message: 'Hello, backend!'});
@@ -109,7 +109,7 @@ describe('httpResource', () => {
109109
it('should return response headers & status when resolved', async () => {
110110
const backend = TestBed.inject(HttpTestingController);
111111
const res = httpResource(() => '/data', {injector: TestBed.inject(Injector)});
112-
TestBed.flushEffects();
112+
TestBed.tick();
113113
const req = backend.expectOne('/data');
114114
req.flush([], {
115115
headers: {
@@ -125,7 +125,7 @@ describe('httpResource', () => {
125125
it('should return response headers & status when request errored', async () => {
126126
const backend = TestBed.inject(HttpTestingController);
127127
const res = httpResource(() => '/data', {injector: TestBed.inject(Injector)});
128-
TestBed.flushEffects();
128+
TestBed.tick();
129129
const req = backend.expectOne('/data');
130130
req.flush([], {
131131
headers: {
@@ -149,7 +149,7 @@ describe('httpResource', () => {
149149
}),
150150
{injector: TestBed.inject(Injector)},
151151
);
152-
TestBed.flushEffects();
152+
TestBed.tick();
153153
const req = backend.expectOne('/data');
154154
req.event({
155155
type: HttpEventType.DownloadProgress,
@@ -192,7 +192,7 @@ describe('httpResource', () => {
192192
injector: TestBed.inject(Injector),
193193
},
194194
);
195-
TestBed.flushEffects();
195+
TestBed.tick();
196196

197197
const req = TestBed.inject(HttpTestingController).expectOne('/data?fast=yes');
198198
expect(req.request.headers.get('X-Tag')).toEqual('alpha,beta');
@@ -215,7 +215,7 @@ describe('httpResource', () => {
215215
parse: (value) => JSON.stringify(value),
216216
},
217217
);
218-
TestBed.flushEffects();
218+
TestBed.tick();
219219
const req = backend.expectOne('/data');
220220
req.flush([1, 2, 3]);
221221

@@ -229,7 +229,7 @@ describe('httpResource', () => {
229229
injector: TestBed.inject(Injector),
230230
equal: (_a, _b) => true,
231231
});
232-
TestBed.flushEffects();
232+
TestBed.tick();
233233
const req = backend.expectOne('/data');
234234
req.flush(1);
235235

@@ -249,7 +249,7 @@ describe('httpResource', () => {
249249
}),
250250
{injector: TestBed.inject(Injector)},
251251
);
252-
TestBed.flushEffects();
252+
TestBed.tick();
253253
const req = backend.expectOne('/data');
254254
req.flush('[1,2,3]');
255255

@@ -266,7 +266,7 @@ describe('httpResource', () => {
266266
}),
267267
{injector: TestBed.inject(Injector)},
268268
);
269-
TestBed.flushEffects();
269+
TestBed.tick();
270270
const req = backend.expectOne('/data');
271271
const buffer = new ArrayBuffer();
272272
req.flush(buffer);

packages/core/test/render3/reactive_safety_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,5 @@ function expectNotToThrowInReactiveContext(fn: () => void): void {
236236
},
237237
{injector},
238238
);
239-
TestBed.flushEffects();
239+
TestBed.tick();
240240
}

packages/core/test/render3/reactivity_spec.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,11 @@ describe('reactivity', () => {
268268
const log: number[] = [];
269269
TestBed.runInInjectionContext(() => effect(() => log.push(counter())));
270270

271-
TestBed.flushEffects();
271+
TestBed.tick();
272272
expect(log).toEqual([0]);
273273

274274
counter.set(1);
275-
TestBed.flushEffects();
275+
TestBed.tick();
276276
expect(log).toEqual([0, 1]);
277277
});
278278

@@ -282,11 +282,11 @@ describe('reactivity', () => {
282282
const log: number[] = [];
283283
effect(() => log.push(counter()), {injector: TestBed.inject(Injector)});
284284

285-
TestBed.flushEffects();
285+
TestBed.tick();
286286
expect(log).toEqual([0]);
287287

288288
counter.set(1);
289-
TestBed.flushEffects();
289+
TestBed.tick();
290290
expect(log).toEqual([0, 1]);
291291
});
292292

@@ -300,16 +300,16 @@ describe('reactivity', () => {
300300
injector: TestBed.inject(Injector),
301301
});
302302

303-
TestBed.flushEffects();
303+
TestBed.tick();
304304
expect(log).toEqual([0]);
305305

306306
counter.set(1);
307-
TestBed.flushEffects();
307+
TestBed.tick();
308308
expect(log).toEqual([0, 1]);
309309

310310
ref.destroy();
311311
counter.set(2);
312-
TestBed.flushEffects();
312+
TestBed.tick();
313313
expect(log).toEqual([0, 1]);
314314
});
315315

@@ -518,11 +518,11 @@ describe('reactivity', () => {
518518
);
519519
expect(effectCounter).toBe(0);
520520
effectRef.destroy();
521-
TestBed.flushEffects();
521+
TestBed.tick();
522522
expect(effectCounter).toBe(0);
523523

524524
counter.set(2);
525-
TestBed.flushEffects();
525+
TestBed.tick();
526526
expect(effectCounter).toBe(0);
527527
});
528528

@@ -543,11 +543,11 @@ describe('reactivity', () => {
543543
fixture.detectChanges();
544544
expect(effectCounter).toBe(0);
545545

546-
TestBed.flushEffects();
546+
TestBed.tick();
547547
expect(effectCounter).toBe(0);
548548

549549
fixture.componentInstance.counter.set(2);
550-
TestBed.flushEffects();
550+
TestBed.tick();
551551
expect(effectCounter).toBe(0);
552552
});
553553
});
@@ -558,7 +558,7 @@ describe('reactivity', () => {
558558
const counter = signal(0);
559559

560560
effect(() => counter.set(1), {injector: TestBed.inject(Injector)});
561-
TestBed.flushEffects();
561+
TestBed.tick();
562562
expect(counter()).toBe(1);
563563
});
564564

@@ -804,7 +804,7 @@ describe('reactivity', () => {
804804
}
805805

806806
const fixture = TestBed.createComponent(TestCmp);
807-
TestBed.flushEffects();
807+
TestBed.tick();
808808
expect(log).toEqual([]);
809809
fixture.detectChanges();
810810
expect(log).toEqual(['init', 'effect']);
@@ -882,7 +882,7 @@ describe('reactivity', () => {
882882
fixture.componentInstance.vcr.createComponent(TestCmp);
883883

884884
// Verify that simply creating the component didn't schedule the effect.
885-
TestBed.flushEffects();
885+
TestBed.tick();
886886
expect(log).toEqual([]);
887887

888888
// Running change detection should schedule and run the effect.
@@ -914,7 +914,7 @@ describe('reactivity', () => {
914914
}
915915

916916
const fixture = TestBed.createComponent(TestCmp);
917-
TestBed.flushEffects();
917+
TestBed.tick();
918918
expect(log).toEqual([]);
919919
fixture.detectChanges();
920920
expect(log).toEqual(['init', 'effect']);

0 commit comments

Comments
 (0)