In a scenario akin to a previous inquiry, my goal is to mock an external library utilizing sinon
. The challenge lies in the fact that this library exports two functions and a namespace under the identical name of FastGlob
.
While I possess a basic understanding of function overloading, there is uncertainty regarding how namespaces interact with function overloading in this specific case.
My objective is to mock the initial function definition; however, sinon
seems to be detecting the namespace instead.
declare function FastGlob(source: PatternInternal | PatternInternal[], options: OptionsInternal & EntryObjectPredicate): Promise<EntryInternal[]>;
The following depicts the library's definition file:
import { Options as OptionsInternal } from './settings';
import { Entry as EntryInternal, FileSystemAdapter as FileSystemAdapterInternal, Pattern as PatternInternal } from './types';
declare function FastGlob(source: PatternInternal | PatternInternal[], options: OptionsInternal & EntryObjectPredicate): Promise<EntryInternal[]>;
declare function FastGlob(source: PatternInternal | PatternInternal[], options?: OptionsInternal): Promise<string[]>;
declare namespace FastGlob {
type Options = OptionsInternal;
type Entry = EntryInternal;
type Task = taskManager.Task;
type Pattern = PatternInternal;
type FileSystemAdapter = FileSystemAdapterInternal;
function sync(source: PatternInternal | PatternInternal[], options: OptionsInternal & EntryObjectPredicate): EntryInternal[];
function sync(source: PatternInternal | PatternInternal[], options?: OptionsInternal): string[];
function stream(source: PatternInternal | PatternInternal[], options?: OptionsInternal): NodeJS.ReadableStream;
function generateTasks(source: PatternInternal | PatternInternal[], options?: OptionsInternal): Task[];
function isDynamicPattern(source: PatternInternal, options?: OptionsInternal): boolean;
function escapePath(source: PatternInternal): PatternInternal;
}
export = FastGlob;
Various attempts have been made, but TypeScript seems only able to locate the functions within the namespace (such as sync, stream, etc.). Removing the function's string identifier leads to a different issue.
import * as FastGlob from 'fast-glob';
import { stub, SinonStub } from "sinon";
import { Pattern, Entry, Options } from "fast-glob";
(stub(FastGlob, "FastGlob") as unknown as SinonStub<[s: Pattern | Pattern[], o: Options], Promise<Entry[]>>).resolves([{test: '/test/'} as unknown as Entry])
This is how the application code integrates the library:
import * as glob from 'fast-glob';
const paths: Array<string> = await glob('./my/glob/**/*.ts', { absolute: true });