From 8ea691a9d8af85ab03cc8a3039feaad3ed679889 Mon Sep 17 00:00:00 2001 From: Andrew Kushnir Date: Thu, 16 Jan 2025 18:11:30 -0800 Subject: [PATCH] test(platform-server): fix flaky hydration test (#59579) This commit updates testing setup logic to apply correct `document` reference, which should be used by the runtime. Previously, the timing of that operation was less predictable and in some cases led to reusing document state from previous tests. PR Close #59579 --- packages/platform-server/test/dom_utils.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/platform-server/test/dom_utils.ts b/packages/platform-server/test/dom_utils.ts index 20c42a755b7..57219253a44 100644 --- a/packages/platform-server/test/dom_utils.ts +++ b/packages/platform-server/test/dom_utils.ts @@ -93,18 +93,20 @@ export function hydrate( hydrationFeatures?: () => HydrationFeature[]; } = {}, ) { - function _document(): any { - ɵsetDocument(doc); - global.document = doc; // needed for `DefaultDomRenderer2` - return doc; - } - const {envProviders = [], hydrationFeatures = () => []} = options; + // Apply correct reference to the `document` object, + // which will be used by runtime. + ɵsetDocument(doc); + + // Define `document` to make `DefaultDomRenderer2` work, since it + // references `document` directly to create style tags. + global.document = doc; + const providers = [ ...envProviders, {provide: PLATFORM_ID, useValue: 'browser'}, - {provide: DOCUMENT, useFactory: _document, deps: []}, + {provide: DOCUMENT, useFactory: () => doc}, provideClientHydration(...hydrationFeatures()), ];