Setu-TS
    Preparing search index...

    Function overrideProvider

    • Replaces a provider in the application's dependency-injection container with a test double, before anything is constructed from it.

      overrideCapability replaces a capability in the kernel's service registry. With DiPlugin registered, DecoratorPlugin puts each @Injectable class into the CONTAINER instead, where that cannot reach it, and the container refuses a second registration of a token. This plugin registers the double on the container first and stands in for the container while the rest of the application registers, so the application's own registration of the real provider is skipped and every class that injects the token — a controller included — is constructed with the double.

      The plugin depends on CAPABILITIES.DI_CONTAINER, so the kernel orders it after DiPlugin, and registers at PLUGIN_PRIORITY.NORMAL, ahead of DecoratorPlugin (PLUGIN_PRIORITY.LOW).

      Type Parameters

      • T

      Parameters

      • token: ContainerToken<T>

        The container token to replace: an @Injectable({ token }) value, or the token the decorator derived for the class.

      • provider: Provider<T>

        The double: { useValue }, { useFactory } or { useClass }

      • Optionaloptions: ProviderOptions<T>

        Lifecycle scope for the double

      Returns IPlugin

      A plugin to append to createTestApp's overrides, or to pass to app.register() on an un-started application

      During start(). From register() if the application has no DI container, or if the real provider was registered first (the double would never be resolved). From an onInit hook if nothing in the application registered or asked for token, which is how a mistyped token surfaces.

      import { createTestApp, overrideProvider } from '@setu-ts/testing';
      import { createApp } from '../setu.config.ts';

      const app = await createTestApp({
      app: createApp(),
      overrides: [overrideProvider('pricing-service', { useValue: { price: () => 0 } })],
      });

      0.9.0