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
This commit is contained in:
Andrew Kushnir
2025-01-16 18:11:30 -08:00
committed by Pawel Kozlowski
parent f6e97763cf
commit 8ea691a9d8
+9 -7
View File
@@ -93,18 +93,20 @@ export function hydrate(
hydrationFeatures?: () => HydrationFeature<HydrationFeatureKind>[];
} = {},
) {
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()),
];