Trying to write a unit test for an Angular v19 Resolver

Discussão em 'Angular' iniciado por bob.mazzo, Abril 7, 2025.

  1. bob.mazzo

    bob.mazzo Guest

    I cannot figure out how to write a successful spec unit test for this resolver. Just a basic test times out, probably due to the resolver not properly tested:

    app/app.component.spec.ts
    ✕ should create RouterOutlet with valid query parameters (13469 ms)

    AppComponent › should create RouterOutlet with valid query parameters

    thrown: "Exceeded timeout of 5000 ms for a test.

    1. Resolver function:


    import { inject } from '@angular/core';
    import { ResolveFn } from '@angular/router';
    import { GlobalConfigLoadService, OcAppSettings } from '@tpn/oc-core';


    import { catchError, map, of } from 'rxjs';

    // Loads global configuration from server, and maps to front end object.
    export const globalConfigResolver: ResolveFn<OcAppSettings> = route => {
    const configService = inject(GlobalConfigLoadService);
    const organizationUUID = route.queryParams['organizationID'];

    return configService.loadGlobalConfigData(organizationID).pipe(
    map(config => {
    return configService.mapGlobalConfigToOctaveUi(config);
    }),
    );
    };

    Current unit test:


    import { TestBed } from '@angular/core/testing';
    import { ResolveFn } from '@angular/router';
    import { OcAppSettings } from '@tpcn/oc-core';
    import { globalConfigResolver } from './global-config.resolver';
    import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';

    describe('globalConfigResolverResolver', () => {
    const executeResolver: ResolveFn<OcAppSettings> = (...resolverParameters) =>
    TestBed.runInInjectionContext(() => globalConfigResolver(...resolverParameters));

    beforeEach(() => {
    TestBed.configureTestingModule({
    imports: [],
    providers: [provideHttpClient(withInterceptorsFromDi())],
    });
    });

    it('should be created', () => {
    expect(executeResolver).toBeTruthy();
    });
    });

    and the routes:


    export const ocRoutes: Routes = [

    {
    path: 'myPath',
    component: MyShellComponent,
    canActivate: [myRouteGuard],
    resolve: { appConfig: globalConfigResolver },
    },
    ];

    Continue reading...

Compartilhe esta Página