Skip to content

Commit 733ab57

Browse files
committed
ensure change detection completes even if test code throws
1 parent dbdbfb4 commit 733ab57

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/cdk/testing/change-detection.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,27 @@ async function batchChangeDetection<T>(fn: () => Promise<T>, triggerBeforeAndAft
7777
isDisabled: true,
7878
onDetectChangesNow: resolve,
7979
}));
80-
const result = await fn();
81-
await new Promise(resolve => autoChangeDetectionSubject.next({
82-
isDisabled: false,
83-
onDetectChangesNow: resolve,
84-
}));
85-
return result;
80+
// The function passed in may throw (e.g. if the user wants to make an expectation of an error
81+
// being thrown. If this happens, we need to make sure we still re-enable change detection, so
82+
// we wrap it in a `finally` block.
83+
try {
84+
return await fn();
85+
} finally {
86+
await new Promise(resolve => autoChangeDetectionSubject.next({
87+
isDisabled: false,
88+
onDetectChangesNow: resolve,
89+
}));
90+
}
8691
} else {
8792
autoChangeDetectionSubject.next({isDisabled: true});
88-
const result = await fn();
89-
autoChangeDetectionSubject.next({isDisabled: false});
90-
return result;
93+
// The function passed in may throw (e.g. if the user wants to make an expectation of an error
94+
// being thrown. If this happens, we need to make sure we still re-enable change detection, so
95+
// we wrap it in a `finally` block.
96+
try {
97+
return await fn();
98+
} finally {
99+
autoChangeDetectionSubject.next({isDisabled: false});
100+
}
91101
}
92102
}
93103

0 commit comments

Comments
 (0)