From e259832783a8bd870d3cc38bc2aa691c2833eaeb Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Sun, 8 Jun 2025 20:25:18 +0200 Subject: [PATCH] feat: add config for testing with zoneless Angular --- projects/testing-library/src/lib/config.ts | 4 +--- projects/testing-library/src/lib/models.ts | 7 +++++++ projects/testing-library/src/lib/testing-library.ts | 12 ++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/projects/testing-library/src/lib/config.ts b/projects/testing-library/src/lib/config.ts index bd8ee9bb..cafa7b06 100644 --- a/projects/testing-library/src/lib/config.ts +++ b/projects/testing-library/src/lib/config.ts @@ -3,16 +3,14 @@ import { Config } from './models'; let config: Config = { dom: {}, defaultImports: [], + zoneless: false, }; export function configure(newConfig: Partial | ((config: Partial) => Partial)) { if (typeof newConfig === 'function') { - // Pass the existing config out to the provided function - // and accept a delta in return newConfig = newConfig(config); } - // Merge the incoming config delta config = { ...config, ...newConfig, diff --git a/projects/testing-library/src/lib/models.ts b/projects/testing-library/src/lib/models.ts index 33cd71a8..c095a5e6 100644 --- a/projects/testing-library/src/lib/models.ts +++ b/projects/testing-library/src/lib/models.ts @@ -497,4 +497,11 @@ export interface Config extends Pick, 'excludeCompon * Imports that are added to the imports */ defaultImports: any[]; + /** + * Set to `true` to use zoneless change detection. + * This automatically adds `provideZonelessChangeDetection` to the default imports. + * + * @default false + */ + zoneless?: boolean; } diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index b7888d2d..5035220c 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -10,6 +10,7 @@ import { SimpleChanges, Type, isStandalone, + provideZonelessChangeDetection, } from '@angular/core'; import { ComponentFixture, DeferBlockBehavior, DeferBlockState, TestBed, tick } from '@angular/core/testing'; import { NavigationExtras, Router } from '@angular/router'; @@ -78,6 +79,7 @@ export async function render( initialRoute = '', deferBlockStates = undefined, deferBlockBehavior = undefined, + zoneless = false, configureTestBed = () => { /* noop*/ }, @@ -105,6 +107,7 @@ export async function render( imports: addAutoImports(sut, { imports: imports.concat(defaultImports), routes, + zoneless, }), providers: [...providers], schemas: [...schemas], @@ -510,11 +513,16 @@ function addAutoDeclarations( function addAutoImports( sut: Type | string, - { imports = [], routes }: Pick, 'imports' | 'routes'>, + { + imports = [], + routes, + zoneless, + }: Pick, 'imports' | 'routes'> & Pick, ) { const routing = () => (routes ? [RouterTestingModule.withRoutes(routes)] : []); const components = () => (typeof sut !== 'string' && isStandalone(sut) ? [sut] : []); - return [...imports, ...components(), ...routing()]; + const provideZoneless = () => (zoneless ? [provideZonelessChangeDetection()] : []); + return [...imports, ...components(), ...routing(), ...provideZoneless()]; } async function renderDeferBlock(