Skip to content

Commit 3c216c1

Browse files
atscottAndrewKushnir
authored andcommitted
fix(zone.js): waitForAsync should pass args to the test function (#61755)
This ensures that test functions with arguments (e.g. `it.each` in jest) are forwarded to the test function. This does not apply to jasmine, which assumes the only arguments needed would be the `done` function. fixes #61717 PR Close #61755
1 parent a3c6926 commit 3c216c1

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

packages/zone.js/lib/zone-spec/async-test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export function patchAsyncTest(Zone: ZoneType): void {
248248
throw e;
249249
};
250250
}
251-
runInTestZone(fn, this, done, (err: any) => {
251+
runInTestZone(fn, this, undefined, done, (err: any) => {
252252
if (typeof err === 'string') {
253253
return done.fail(new Error(err));
254254
} else {
@@ -261,16 +261,17 @@ export function patchAsyncTest(Zone: ZoneType): void {
261261
// is finished. This will be correctly consumed by the Mocha framework with
262262
// it('...', async(myFn)); or can be used in a custom framework.
263263
// Not using an arrow function to preserve context passed from call site
264-
return function (this: unknown) {
264+
return function (this: unknown, ...args: unknown[]) {
265265
return new Promise<void>((finishCallback, failCallback) => {
266-
runInTestZone(fn, this, finishCallback, failCallback);
266+
runInTestZone(fn, this, args, finishCallback, failCallback);
267267
});
268268
};
269269
};
270270

271271
function runInTestZone(
272272
fn: Function,
273273
context: any,
274+
applyArgs: unknown[] | undefined,
274275
finishCallback: Function,
275276
failCallback: Function,
276277
) {
@@ -329,7 +330,7 @@ export function patchAsyncTest(Zone: ZoneType): void {
329330
proxyZoneSpec.setDelegate(testZoneSpec);
330331
(testZoneSpec as any).patchPromiseForTest();
331332
});
332-
return Zone.current.runGuarded(fn, context);
333+
return Zone.current.runGuarded(fn, context, applyArgs);
333334
}
334335
});
335336
}

packages/zone.js/test/jest/jest.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const waitForAsync = Zone[Zone.__symbol__('asyncTest')];
2+
13
function assertInsideProxyZone() {
24
expect(Zone.current.name).toEqual('ProxyZone');
35
}
@@ -43,6 +45,18 @@ describe('test', () => {
4345
test.each([[]])('test.each', () => {
4446
assertInsideProxyZone();
4547
});
48+
49+
test.each([
50+
['1', 1],
51+
['2', 2],
52+
])(
53+
'Test.each ["%s", %s]',
54+
waitForAsync((p1, p2) => {
55+
expect(typeof p1).toEqual('string');
56+
expect(typeof p2).toEqual('number');
57+
expect(p1).toEqual('' + p2);
58+
}),
59+
);
4660
});
4761

4862
it('it', () => {

packages/zone.js/test/vitest/vitest.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ require('../../../../dist/bin/packages/zone.js/npm_package/bundles/zone-testing.
33

44
import {expect, test, describe, beforeEach} from 'vitest';
55

6-
const {tick, withProxyZone, fakeAsync} = Zone[Zone.__symbol__('fakeAsyncTest')];
6+
const {tick, withProxyZone, fakeAsync, asyncTest} = Zone[Zone.__symbol__('fakeAsyncTest')];
7+
const waitForAsync = Zone[Zone.__symbol__('asyncTest')];
78

89
describe('proxy zone behavior', () => {
910
const spec = new Zone['ProxyZoneSpec']();
@@ -85,6 +86,16 @@ test(
8586
),
8687
);
8788

89+
test.each([[1, 2]])(
90+
'it.each',
91+
withProxyZone(
92+
waitForAsync((arg1, arg2) => {
93+
expect(arg1).toBe(1);
94+
expect(arg2).toBe(2);
95+
}),
96+
),
97+
);
98+
8899
describe('can use withProxyZone and beforeEach', () => {
89100
let forkedZone;
90101
beforeEach(

0 commit comments

Comments
 (0)