最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Choose a specific type overload when mocking via Node's native test module - Stack Overflow

programmeradmin3浏览0评论

I am using the native node:test mock library to mock an overloaded function create;

import { LoggerService } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { equal } from "node:assert/strict";
import { suite, test } from "node:test";

suite("Example", async () => {
  test("How to type the override", async ({ mock }) => {
    const create = mock.method(NestFactory, "create", async () => ({}));

    equal(
      create.mock.calls[0].arguments[1].logger instanceof LoggerService,
      true,
    );
  })
})

The type of create.mock.calls[0].arguments[1] chooses the last overload of two defined overloads;

create<T extends INestApplication = INestApplication>(module: IEntryNestModule, options?: NestApplicationOptions): Promise<T>;

create<T extends INestApplication = INestApplication>(module: IEntryNestModule, httpAdapter: AbstractHttpAdapter, options?: NestApplicationOptions): Promise<T>;

Giving me the following error;

Property 'logger' does not exist on type 'AbstractHttpAdapter<any, any, any>'.ts(2339)

My code uses the first overload, using an options object with a logger property.

I have found solutions for using Jest mocking, but not node:test mocking. I understand I can use type assertions to choose the correct type, but is there an alternative solution using the generics of the mock functions?

发布评论

评论列表(0)

  1. 暂无评论