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?