@nestjs/testing

  • Version 12.2.0
  • Published
  • 44.3 kB
  • 1 dependency
  • MIT license

Install

npm i @nestjs/testing
yarn add @nestjs/testing
pnpm add @nestjs/testing

Overview

Nest - modern, fast, powerful node.js web framework (@testing)

Index

Classes

class CapturingLogger

class CapturingLogger extends ConsoleLogger {}
  • A logger for tests that records every log call with its structured data, so that tests can assert on it. Pass it to setLogger() of the testing module builder, or to app.useLogger(), to capture the framework's logs and the logs of Logger instances.

    Every level is captured unless logLevels is set; the NEST_LOG_LEVEL environment variable is ignored.

    Example 1

    const logger = new CapturingLogger(); const moduleRef = await Test.createTestingModule({ providers: [PaymentsService] }) .setLogger(logger) .compile();

    await moduleRef.get(PaymentsService).charge(order);

    logger.assertLogged({ level: 'warn', context: 'PaymentsService', message: /retrying/ });

constructor

constructor(options?: CapturingLoggerOptions);

    property entries

    readonly entries: CapturedLog[];
    • The recorded entries, oldest first.

    method assertLogged

    assertLogged: (matcher: CapturedLogMatcher) => CapturedLog;
    • Throws an error listing the captured entries if no entry matches.

      Returns

      The first entry that matches.

    method assertNotLogged

    assertNotLogged: (matcher: CapturedLogMatcher) => void;
    • Throws an error listing the matching entries if any entry matches.

    method clear

    clear: () => void;
    • Removes the recorded entries.

    method debug

    debug: (message: any, ...optionalParams: any[]) => void;

      method error

      error: (message: any, ...optionalParams: any[]) => void;

        method fatal

        fatal: (message: any, ...optionalParams: any[]) => void;

          method filter

          filter: (matcher?: CapturedLogMatcher) => CapturedLog[];
          • Returns the entries that match.

          method find

          find: (matcher: CapturedLogMatcher) => CapturedLog | undefined;
          • Returns the first entry that matches.

          method log

          log: (message: any, ...optionalParams: any[]) => void;

            method verbose

            verbose: (message: any, ...optionalParams: any[]) => void;

              method warn

              warn: (message: any, ...optionalParams: any[]) => void;

                class Test

                class Test {}

                  method createTestingModule

                  static createTestingModule: (
                  metadata: ModuleMetadata,
                  options?: TestingModuleOptions
                  ) => TestingModuleBuilder;

                    class TestingModule

                    class TestingModule extends NestApplicationContext {}

                    constructor

                    constructor(
                    container: NestContainer,
                    graphInspector: GraphInspector,
                    contextModule: Module,
                    applicationConfig: ApplicationConfig,
                    scope?: Type<any>[]
                    );

                      property graphInspector

                      protected readonly graphInspector: GraphInspector;

                        method createNestApplication

                        createNestApplication: {
                        <T extends INestApplication = INestApplication>(
                        httpAdapter: HttpServer | AbstractHttpAdapter,
                        options?: NestApplicationOptions
                        ): T;
                        <T extends INestApplication = INestApplication>(
                        options?: NestApplicationOptions
                        ): T;
                        };

                          method createNestMicroservice

                          createNestMicroservice: <T extends object>(
                          options: NestMicroserviceOptions & T
                          ) => INestMicroservice;

                            class TestingModuleBuilder

                            class TestingModuleBuilder {}

                            constructor

                            constructor(
                            metadataScanner: MetadataScanner,
                            metadata: ModuleMetadata,
                            options?: TestingModuleOptions
                            );

                              method compile

                              compile: (
                              options?: Pick<NestApplicationContextOptions, 'snapshot' | 'preview'>
                              ) => Promise<TestingModule>;

                                method overrideFilter

                                overrideFilter: <T = any>(typeOrToken: T) => OverrideBy;

                                  method overrideGuard

                                  overrideGuard: <T = any>(typeOrToken: T) => OverrideBy;

                                    method overrideInterceptor

                                    overrideInterceptor: <T = any>(typeOrToken: T) => OverrideBy;

                                      method overrideModule

                                      overrideModule: (moduleToOverride: ModuleDefinition) => OverrideModule;

                                        method overridePipe

                                        overridePipe: <T = any>(typeOrToken: T) => OverrideBy;

                                          method overrideProvider

                                          overrideProvider: <T = any>(typeOrToken: T) => OverrideBy;

                                            method setLogger

                                            setLogger: (testingLogger: LoggerService) => this;

                                              method useMocker

                                              useMocker: (mocker: MockFactory) => TestingModuleBuilder;

                                                Interfaces

                                                interface CapturedLog

                                                interface CapturedLog {}
                                                • A log call recorded by CapturingLogger.

                                                property args

                                                args: unknown[];
                                                • The arguments, as passed to the logger (including the context).

                                                property context

                                                context?: string;
                                                • The context, e.g. the one passed to new Logger('Context').

                                                property error

                                                error?: Error;
                                                • The first Error passed to the call, as the message or after it.

                                                property level

                                                level: LogLevel;

                                                  property message

                                                  message: unknown;
                                                  • The message. A lazy message (a function) is evaluated, and when an Error is logged as the message, this is the error's message.

                                                  property params

                                                  params?: Record<string, any>;
                                                  • The plain objects passed after the message, merged.

                                                  property stack

                                                  stack?: string;
                                                  • The stack trace passed as a string, as in logger.error(message, stack).

                                                  interface CapturedLogFilter

                                                  interface CapturedLogFilter {}
                                                  • Criteria for CapturingLogger queries. Every criterion that is set must match.

                                                  property context

                                                  context?: string | RegExp;
                                                  • A string matches the context exactly.

                                                  property level

                                                  level?: LogLevel;

                                                    property message

                                                    message?: string | RegExp;
                                                    • A string matches when the message contains it. Only string messages match (an Error logged as the message counts, through its message).

                                                    property params

                                                    params?: Record<string, unknown>;
                                                    • Matches when each of these params is deeply equal to the logged one.

                                                    interface CapturingLoggerOptions

                                                    interface CapturingLoggerOptions extends ConsoleLoggerOptions {}

                                                    property print

                                                    print?: boolean;
                                                    • If enabled, logs are also printed, as a ConsoleLogger with the same options would print them. false

                                                    interface OverrideBy

                                                    interface OverrideBy {}

                                                    property useClass

                                                    useClass: (metatype: any) => TestingModuleBuilder;

                                                      property useFactory

                                                      useFactory: (options: OverrideByFactoryOptions) => TestingModuleBuilder;

                                                        property useValue

                                                        useValue: (value: any) => TestingModuleBuilder;

                                                          interface OverrideByFactoryOptions

                                                          interface OverrideByFactoryOptions {}

                                                          property factory

                                                          factory: (...args: any[]) => any;

                                                            property inject

                                                            inject?: any[];

                                                              Type Aliases

                                                              type CapturedLogMatcher

                                                              type CapturedLogMatcher = CapturedLogFilter | ((entry: CapturedLog) => boolean);

                                                              type MockFactory

                                                              type MockFactory = (token?: InjectionToken) => any;

                                                                type TestingModuleOptions

                                                                type TestingModuleOptions = Pick<
                                                                NestApplicationContextOptions,
                                                                'moduleIdGeneratorAlgorithm'
                                                                >;

                                                                Package Files (8)

                                                                Dependencies (1)

                                                                Dev Dependencies (0)

                                                                No dev dependencies.

                                                                Peer Dependencies (4)

                                                                Badge

                                                                To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@nestjs/testing.

                                                                • Markdown
                                                                  [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@nestjs/testing)
                                                                • HTML
                                                                  <a href="https://www.jsdocs.io/package/@nestjs/testing"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>