Skip to content

feat: add config for testing with zoneless Angular #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions projects/testing-library/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import { Config } from './models';
let config: Config = {
dom: {},
defaultImports: [],
zoneless: false,
};

export function configure(newConfig: Partial<Config> | ((config: Partial<Config>) => Partial<Config>)) {
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,
Expand Down
7 changes: 7 additions & 0 deletions projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,11 @@ export interface Config extends Pick<RenderComponentOptions<any>, '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;
}
12 changes: 10 additions & 2 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -78,6 +79,7 @@ export async function render<SutType, WrapperType = SutType>(
initialRoute = '',
deferBlockStates = undefined,
deferBlockBehavior = undefined,
zoneless = false,
configureTestBed = () => {
/* noop*/
},
Expand Down Expand Up @@ -105,6 +107,7 @@ export async function render<SutType, WrapperType = SutType>(
imports: addAutoImports(sut, {
imports: imports.concat(defaultImports),
routes,
zoneless,
}),
providers: [...providers],
schemas: [...schemas],
Expand Down Expand Up @@ -510,11 +513,16 @@ function addAutoDeclarations<SutType>(

function addAutoImports<SutType>(
sut: Type<SutType> | string,
{ imports = [], routes }: Pick<RenderComponentOptions<any>, 'imports' | 'routes'>,
{
imports = [],
routes,
zoneless,
}: Pick<RenderComponentOptions<any>, 'imports' | 'routes'> & Pick<Config, 'zoneless'>,
) {
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<SutType>(
Expand Down