Tengo el siguiente código
import { readdir } from 'fs/promises'; const files: string[] = await readdir('/original/file/path');que estoy tratando de probar
import * as fsPromisesModule from "fs/promises"; stub(fsPromisesModule, "readdir").callsFake(async (): Promise<string[]> => ['/test/file/path']);pero me sale el siguiente error
Type 'Promise<string[]>' is not assignable to type 'Promise<Dirent[]>'. Type 'string[]' is not assignable to type 'Dirent[]'. Type 'string' is not assignable to type 'Dirent'.ts(2322) El archivo promises.d.ts contiene las siguientes firmas
function readdir( path: PathLike, options?: | (ObjectEncodingOptions & { withFileTypes?: false | undefined; }) | BufferEncoding | null ): Promise<string[]>; function readdir( path: PathLike, options: | { encoding: 'buffer'; withFileTypes?: false | undefined; } | 'buffer' ): Promise<Buffer[]>; function readdir( path: PathLike, options?: | (ObjectEncodingOptions & { withFileTypes?: false | undefined; }) | BufferEncoding | null ): Promise<string[] | Buffer[]>; function readdir( path: PathLike, options: ObjectEncodingOptions & { withFileTypes: true; } ): Promise<Dirent[]>;La última firma se usa en el código auxiliar, pero quiero usar la primera firma. ¿Cómo puedo decirle a TypeScript que es la firma que quiero usar en mis pruebas?
Según la respuesta vinculada a @andreyunugros, lo siguiente funcionó;
(stub(fsPromisesModule, "readdir") as unknown as SinonStub<[p: PathLike], Promise<string[]>>).callsFake(async (): Promise<string[]> => (['/file/path/name']);