From d952b82b8308d45f4e80e62ab0e3d11db0888522 Mon Sep 17 00:00:00 2001 From: Noah Encke <78610362+noencke@users.noreply.github.com> Date: Sat, 1 Feb 2025 00:32:34 +0000 Subject: [PATCH] Enable esm testing in odsp-driver --- packages/drivers/odsp-driver/package.json | 5 +- .../drivers/odsp-driver/src/fetchSnapshot.ts | 6 +- .../drivers/odsp-driver/src/getFileLink.ts | 6 +- packages/drivers/odsp-driver/src/mockify.ts | 67 +++++++++++++++++++ packages/drivers/odsp-driver/src/odspUtils.ts | 4 +- .../drivers/odsp-driver/src/socketModule.ts | 6 +- .../src/test/fetchSnapshot.spec.ts | 18 ++--- .../src/test/joinSessionCacheTests.spec.ts | 9 +-- .../src/test/joinSessionPeriodicCall.spec.ts | 5 +- .../src/test/localOdspDriver.spec.ts | 13 +++- .../drivers/odsp-driver/src/test/mockFetch.ts | 9 +-- .../odspDriverUrlResolverForShareLink.spec.ts | 5 +- .../deltaConnectionUpdateTests.spec.ts | 9 +-- .../src/test/socketTests/socketTests.spec.ts | 5 +- packages/drivers/odsp-driver/src/vroom.ts | 6 +- 15 files changed, 134 insertions(+), 39 deletions(-) create mode 100644 packages/drivers/odsp-driver/src/mockify.ts diff --git a/packages/drivers/odsp-driver/package.json b/packages/drivers/odsp-driver/package.json index 17aff9b57d53..d4231903e5da 100644 --- a/packages/drivers/odsp-driver/package.json +++ b/packages/drivers/odsp-driver/package.json @@ -59,8 +59,7 @@ "build:docs": "api-extractor run --local", "build:esnext": "tsc --project ./tsconfig.json", "build:genver": "gen-version", - "build:test": "npm run build:test:esm && npm run build:test:cjs", - "build:test:cjs": "fluid-tsc commonjs --project ./src/test/tsconfig.cjs.json", + "build:test": "npm run build:test:esm", "build:test:esm": "tsc --project ./src/test/tsconfig.json", "check:are-the-types-wrong": "attw --pack .", "check:biome": "biome check .", @@ -86,7 +85,7 @@ "lint:fix": "fluid-build . --task eslint:fix --task format", "test": "npm run test:mocha", "test:coverage": "c8 npm test", - "test:mocha": "npm run test:mocha:cjs && echo \"ADO #7404 - ESM modules cannot be stubbed - npm run test:mocha:esm\"", + "test:mocha": "npm run test:mocha:esm", "test:mocha:cjs": "mocha --recursive \"dist/test/**/*.spec.*js\" --exit", "test:mocha:esm": "mocha --recursive \"lib/test/**/*.spec.*js\" --exit", "test:mocha:verbose": "cross-env FLUID_TEST_VERBOSE=1 npm run test:mocha", diff --git a/packages/drivers/odsp-driver/src/fetchSnapshot.ts b/packages/drivers/odsp-driver/src/fetchSnapshot.ts index 07d673494891..4ce2a261375a 100644 --- a/packages/drivers/odsp-driver/src/fetchSnapshot.ts +++ b/packages/drivers/odsp-driver/src/fetchSnapshot.ts @@ -46,6 +46,7 @@ import { import { EpochTracker } from "./epochTracker.js"; import { getQueryString } from "./getQueryString.js"; import { getHeadersWithAuth } from "./getUrlAndHeadersWithAuth.js"; +import { mockify } from "./mockify.js"; import { convertOdspSnapshotToSnapshotTreeAndBlobs } from "./odspSnapshotParser.js"; import { checkForKnownServerFarmType } from "./odspUrlHelper.js"; import { @@ -693,7 +694,7 @@ function getTreeStatsCore(snapshotTree: ISnapshotTree, stats: ITreeStats): void * @param epochTracker - epoch tracker used to add/validate epoch in the network call. * @returns fetched snapshot. */ -export async function downloadSnapshot( +async function downloadSnapshot( odspResolvedUrl: IOdspResolvedUrl, getAuthHeader: InstrumentedStorageTokenFetcher, tokenFetchOptions: TokenFetchOptionsEx, @@ -775,6 +776,9 @@ export async function downloadSnapshot( }; } +const mockableDownloadSnapshot = mockify(downloadSnapshot); +export { mockableDownloadSnapshot as downloadSnapshot }; + function isRedeemSharingLinkError( odspResolvedUrl: IOdspResolvedUrl, error: Partial, diff --git a/packages/drivers/odsp-driver/src/getFileLink.ts b/packages/drivers/odsp-driver/src/getFileLink.ts index 8a47fbfa542f..62e79e895592 100644 --- a/packages/drivers/odsp-driver/src/getFileLink.ts +++ b/packages/drivers/odsp-driver/src/getFileLink.ts @@ -20,6 +20,7 @@ import { } from "@fluidframework/telemetry-utils/internal"; import { getHeadersWithAuth } from "./getUrlAndHeadersWithAuth.js"; +import { mockify } from "./mockify.js"; import { fetchHelper, getWithRetryForTokenRefresh, @@ -42,7 +43,7 @@ const fileLinkCache = new Map>(); * @param logger - used to log results of operation, including any error * @returns Promise which resolves to file link url when successful; otherwise, undefined. */ -export async function getFileLink( +async function getFileLink( getToken: TokenFetcher, resolvedUrl: IOdspResolvedUrl, logger: ITelemetryLoggerExt, @@ -101,6 +102,9 @@ export async function getFileLink( return fileLink; } +const mockableGetFileLink = mockify(getFileLink); +export { mockableGetFileLink as getFileLink }; + /** * Handles location redirection while fulfilling the getFileLink call. We don't want browser to handle * the redirection as the browser will retry with same auth token which will not work as we need app diff --git a/packages/drivers/odsp-driver/src/mockify.ts b/packages/drivers/odsp-driver/src/mockify.ts new file mode 100644 index 000000000000..14b20af48a39 --- /dev/null +++ b/packages/drivers/odsp-driver/src/mockify.ts @@ -0,0 +1,67 @@ +/*! + * Copyright (c) Microsoft Corporation and contributors. All rights reserved. + * Licensed under the MIT License. + */ + +/** + * A special key used to store the original function in a {@link Mockable | mockable} function. + * @remarks Use {@link mockify | `mockify.key`} as a convenient way to access this key. + */ +export const mockifyMockKey = Symbol("`mockify` mock function key"); + +/** + * A function that can be mocked after being decorated by {@link mockify | mockify()}. + */ +export interface Mockable unknown> { + (...args: Parameters): ReturnType; + [mockifyMockKey]: T; +} + +/** + * Decorates a function to allow it to be mocked. + * @param fn - The function that will become mockable. + * @returns A function with a {@link mockifyMockKey | special property } that can be overwritten to mock the original function. + * By default, this property is set to the original function. + * If overwritten with a new function, the new function will be called instead of the original. + * @example + * ```typescript + * const original = () => console.log("original"); + * const mockable = mockify(original); + * mockable(); // logs "original" + * mockable[mockify.key] = () => console.log("mocked"); + * mockable(); // logs "mocked" + * mockable[mockify.key] = original; + * mockable(); // logs "original" + * ``` + * + * This pattern is useful for mocking top-level exported functions in a module. + * For example, + * ```typescript + * export function fn() { /* ... * / } + * ``` + * becomes + * ```typescript + * import { mockify } from "./mockify.js"; + * export const fn = mockify(() => { /* ... * / }); + * ``` + * and can now be mocked by another module that imports it. + * ```typescript + * import * as sinon from "sinon"; + * import { mockify } from "./mockify.js"; + * import { fn } from "./module.js"; + * sinon.stub(fn, mockify.key).callsFake(() => { + * // ... mock function implementation ... + * }); + * // ... + * sinon.restore(); + * ``` + */ +export function mockify unknown>(fn: T): Mockable { + const mockable = (...args: Parameters): ReturnType => { + return mockable[mockifyMockKey](...args) as ReturnType; + }; + mockable[mockifyMockKey] = fn; + return mockable; +} + +mockify.key = mockifyMockKey; diff --git a/packages/drivers/odsp-driver/src/odspUtils.ts b/packages/drivers/odsp-driver/src/odspUtils.ts index 5da0ac75b809..1df657b2987c 100644 --- a/packages/drivers/odsp-driver/src/odspUtils.ts +++ b/packages/drivers/odsp-driver/src/odspUtils.ts @@ -138,7 +138,7 @@ export async function fetchHelper( const start = performanceNow(); // Node-fetch and dom have conflicting typing, force them to work by casting for now - return fetch(requestInfo, requestInit).then( + return fetchHelper.fetch(requestInfo, requestInit).then( async (fetchResponse) => { const response = fetchResponse as unknown as Response; // Let's assume we can retry. @@ -221,6 +221,8 @@ export async function fetchHelper( }, ); } +// This allows `fetch` to be mocked (e.g. with sinon `stub()`) +fetchHelper.fetch = fetch; /** * A utility function to fetch and parse as JSON with support for retries diff --git a/packages/drivers/odsp-driver/src/socketModule.ts b/packages/drivers/odsp-driver/src/socketModule.ts index 66037b412628..e9f37ac81011 100644 --- a/packages/drivers/odsp-driver/src/socketModule.ts +++ b/packages/drivers/odsp-driver/src/socketModule.ts @@ -5,6 +5,6 @@ import { io } from "socket.io-client"; -// Import is required for side-effects. -// eslint-disable-next-line unicorn/prefer-export-from -export const SocketIOClientStatic = io; +import { mockify, type Mockable } from "./mockify.js"; + +export const SocketIOClientStatic: Mockable = mockify(io); diff --git a/packages/drivers/odsp-driver/src/test/fetchSnapshot.spec.ts b/packages/drivers/odsp-driver/src/test/fetchSnapshot.spec.ts index faff377bf407..b65b242d31e1 100644 --- a/packages/drivers/odsp-driver/src/test/fetchSnapshot.spec.ts +++ b/packages/drivers/odsp-driver/src/test/fetchSnapshot.spec.ts @@ -25,8 +25,8 @@ import { convertToCompactSnapshot } from "../compactSnapshotWriter.js"; import { HostStoragePolicyInternal } from "../contracts.js"; import { createOdspUrl } from "../createOdspUrl.js"; import { EpochTracker } from "../epochTracker.js"; -import * as fetchSnapshotImport from "../fetchSnapshot.js"; -import { ISnapshotRequestAndResponseOptions } from "../fetchSnapshot.js"; +import { downloadSnapshot, ISnapshotRequestAndResponseOptions } from "../fetchSnapshot.js"; +import { mockify } from "../mockify.js"; import { LocalPersistentCache, NonPersistentCache } from "../odspCache.js"; import { OdspDocumentStorageService } from "../odspDocumentStorageManager.js"; import { OdspDriverUrlResolver } from "../odspDriverUrlResolver.js"; @@ -139,7 +139,7 @@ describe("Tests1 for snapshot fetch", () => { _response: Promise, callback: () => Promise, ): Promise { - const getDownloadSnapshotStub = stub(fetchSnapshotImport, "downloadSnapshot"); + const getDownloadSnapshotStub = stub(downloadSnapshot, mockify.key); getDownloadSnapshotStub.returns(_response); try { return await callback(); @@ -181,7 +181,7 @@ describe("Tests1 for snapshot fetch", () => { _response: Promise, callback: () => Promise, ): Promise { - const getDownloadSnapshotStub = stub(fetchSnapshotImport, "downloadSnapshot"); + const getDownloadSnapshotStub = stub(downloadSnapshot, mockify.key); getDownloadSnapshotStub.returns(_response); try { return await callback(); @@ -222,7 +222,7 @@ describe("Tests1 for snapshot fetch", () => { _response: Promise, callback: () => Promise, ): Promise { - const getDownloadSnapshotStub = stub(fetchSnapshotImport, "downloadSnapshot"); + const getDownloadSnapshotStub = stub(downloadSnapshot, mockify.key); getDownloadSnapshotStub.returns(_response); try { return await callback(); @@ -270,7 +270,7 @@ describe("Tests1 for snapshot fetch", () => { _response: Promise, callback: () => Promise, ): Promise { - const getDownloadSnapshotStub = stub(fetchSnapshotImport, "downloadSnapshot"); + const getDownloadSnapshotStub = stub(downloadSnapshot, mockify.key); getDownloadSnapshotStub.returns(_response); try { return await callback(); @@ -336,7 +336,7 @@ describe("Tests1 for snapshot fetch", () => { _response: Promise, callback: () => Promise, ): Promise { - const getDownloadSnapshotStub = stub(fetchSnapshotImport, "downloadSnapshot"); + const getDownloadSnapshotStub = stub(downloadSnapshot, mockify.key); getDownloadSnapshotStub.returns(_response); try { return await callback(); @@ -401,7 +401,7 @@ describe("Tests1 for snapshot fetch", () => { _response: Promise, callback: () => Promise, ): Promise { - const getDownloadSnapshotStub = stub(fetchSnapshotImport, "downloadSnapshot"); + const getDownloadSnapshotStub = stub(downloadSnapshot, mockify.key); getDownloadSnapshotStub.returns(_response); try { return await callback(); @@ -455,7 +455,7 @@ describe("Tests1 for snapshot fetch", () => { _response: Promise, callback: () => Promise, ): Promise { - const getDownloadSnapshotStub = stub(fetchSnapshotImport, "downloadSnapshot"); + const getDownloadSnapshotStub = stub(downloadSnapshot, mockify.key); getDownloadSnapshotStub.returns(_response); try { return await callback(); diff --git a/packages/drivers/odsp-driver/src/test/joinSessionCacheTests.spec.ts b/packages/drivers/odsp-driver/src/test/joinSessionCacheTests.spec.ts index 633e959a2a08..2c43ab0fc737 100644 --- a/packages/drivers/odsp-driver/src/test/joinSessionCacheTests.spec.ts +++ b/packages/drivers/odsp-driver/src/test/joinSessionCacheTests.spec.ts @@ -19,11 +19,12 @@ import { stub, type SinonStub } from "sinon"; import { Socket } from "socket.io-client"; import { createOdspUrl } from "../createOdspUrl.js"; +import { mockify } from "../mockify.js"; import { OdspDocumentServiceFactory } from "../odspDocumentServiceFactory.js"; import { OdspDriverUrlResolver } from "../odspDriverUrlResolver.js"; import { getJoinSessionCacheKey } from "../odspUtils.js"; -import * as socketModule from "../socketModule.js"; -import * as joinSession from "../vroom.js"; +import { SocketIOClientStatic } from "../socketModule.js"; +import { fetchJoinSession } from "../vroom.js"; // eslint-disable-next-line import/no-internal-modules import { ClientSocketMock } from "./socketTests/socketMock.js"; @@ -56,14 +57,14 @@ describe("expose joinSessionInfo Tests", () => { ); function addJoinSessionStub(): SinonStub { - const joinSessionStub = stub(joinSession, "fetchJoinSession").callsFake( + const joinSessionStub = stub(fetchJoinSession, mockify.key).callsFake( async () => joinSessionResponse, ); return joinSessionStub; } async function mockSocket(_response: Socket, callback: () => Promise): Promise { - const getSocketCreationStub = stub(socketModule, "SocketIOClientStatic"); + const getSocketCreationStub = stub(SocketIOClientStatic, mockify.key); getSocketCreationStub.returns(_response); try { return await callback(); diff --git a/packages/drivers/odsp-driver/src/test/joinSessionPeriodicCall.spec.ts b/packages/drivers/odsp-driver/src/test/joinSessionPeriodicCall.spec.ts index a75076d54760..b26a8756361c 100644 --- a/packages/drivers/odsp-driver/src/test/joinSessionPeriodicCall.spec.ts +++ b/packages/drivers/odsp-driver/src/test/joinSessionPeriodicCall.spec.ts @@ -12,13 +12,14 @@ import { SinonFakeTimers, SinonStub, stub, useFakeTimers } from "sinon"; import { OdspFluidDataStoreLocator } from "../contractsPublic.js"; import { createOdspUrl } from "../createOdspUrl.js"; +import { mockify } from "../mockify.js"; import { LocalPersistentCache } from "../odspCache.js"; import * as odspDocumentDeltaConnection from "../odspDocumentDeltaConnection.js"; import { OdspDocumentDeltaConnection } from "../odspDocumentDeltaConnection.js"; import { OdspDocumentService } from "../odspDocumentService.js"; import { OdspDocumentServiceFactory } from "../odspDocumentServiceFactory.js"; import { OdspDriverUrlResolver } from "../odspDriverUrlResolver.js"; -import * as joinSession from "../vroom.js"; +import { fetchJoinSession } from "../vroom.js"; describe("joinSessions Tests", () => { let clock: SinonFakeTimers; @@ -65,7 +66,7 @@ describe("joinSessions Tests", () => { function addJoinSessionStub(time: number): SinonStub { joinSessionResponse.refreshSessionDurationSeconds = time; - const joinSessionStub = stub(joinSession, "fetchJoinSession").callsFake( + const joinSessionStub = stub(fetchJoinSession, mockify.key).callsFake( async () => joinSessionResponse, ); return joinSessionStub; diff --git a/packages/drivers/odsp-driver/src/test/localOdspDriver.spec.ts b/packages/drivers/odsp-driver/src/test/localOdspDriver.spec.ts index e0d0e13c4d83..e9fe32523742 100644 --- a/packages/drivers/odsp-driver/src/test/localOdspDriver.spec.ts +++ b/packages/drivers/odsp-driver/src/test/localOdspDriver.spec.ts @@ -5,6 +5,8 @@ import { strict as assert } from "node:assert"; import fs from "node:fs"; +import { dirname } from "node:path"; +import { fileURLToPath } from "node:url"; import { IClient, SummaryType } from "@fluidframework/driver-definitions"; import { @@ -50,7 +52,7 @@ describe("Local Odsp driver", () => { }; const localSnapshot = fs.readFileSync( - `${__dirname}/../../src/test/localSnapshots/localSnapshot1.json`, + `${getDirname()}/../../src/test/localSnapshots/localSnapshot1.json`, { encoding: "utf8" }, ); @@ -152,7 +154,7 @@ describe("Local Odsp driver", () => { it("Delta storage service returns trailing ops", async () => { const snapshotWithTrailingOps = fs.readFileSync( - `${__dirname}/../../src/test/localSnapshots/localSnapshot2.json`, + `${getDirname()}/../../src/test/localSnapshots/localSnapshot2.json`, { encoding: "utf8" }, ); const service = new LocalOdspDocumentService( @@ -290,3 +292,10 @@ describe("Local Odsp driver", () => { }); }); }); + +/** + * Retrieves the directory in which this module resides (equivalent to `__dirname` in CJS) + */ +function getDirname(): string { + return dirname(fileURLToPath(import.meta.url)); +} diff --git a/packages/drivers/odsp-driver/src/test/mockFetch.ts b/packages/drivers/odsp-driver/src/test/mockFetch.ts index f8ec3bef8d89..96220e69e440 100644 --- a/packages/drivers/odsp-driver/src/test/mockFetch.ts +++ b/packages/drivers/odsp-driver/src/test/mockFetch.ts @@ -5,10 +5,11 @@ import assert from "node:assert"; -import * as fetchModule from "node-fetch"; import { Headers } from "node-fetch"; import { stub } from "sinon"; +import { fetchHelper } from "../odspUtils.js"; + /** * Mock response returned by {@link createResponse}. */ @@ -49,14 +50,14 @@ export async function mockFetchMultiple( responses: (() => Promise)[], type: FetchCallType = "single", ): Promise { - const fetchStub = stub(fetchModule, "default"); + const fetchStub = stub(fetchHelper, "fetch"); fetchStub.callsFake(async () => { if (type === "external") { fetchStub.restore(); } const cb = responses.shift(); assert(cb !== undefined, "the end"); - return cb() as Promise; + return cb() as Promise; }); try { return await callback(); @@ -89,7 +90,7 @@ export async function mockFetchError( response: Error, type: FetchCallType = "single", ): Promise { - const fetchStub = stub(fetchModule, "default"); + const fetchStub = stub(fetchHelper, "fetch"); fetchStub.callsFake(async () => { if (type === "external") { fetchStub.restore(); diff --git a/packages/drivers/odsp-driver/src/test/odspDriverUrlResolverForShareLink.spec.ts b/packages/drivers/odsp-driver/src/test/odspDriverUrlResolverForShareLink.spec.ts index d95be38314e6..efe90f0d4364 100644 --- a/packages/drivers/odsp-driver/src/test/odspDriverUrlResolverForShareLink.spec.ts +++ b/packages/drivers/odsp-driver/src/test/odspDriverUrlResolverForShareLink.spec.ts @@ -14,7 +14,8 @@ import { stub } from "sinon"; import { SharingLinkHeader } from "../contractsPublic.js"; import { createOdspCreateContainerRequest } from "../createOdspCreateContainerRequest.js"; import { createOdspUrl } from "../createOdspUrl.js"; -import * as fileLinkImport from "../getFileLink.js"; +import { getFileLink } from "../getFileLink.js"; +import { mockify } from "../mockify.js"; import { OdspDriverUrlResolverForShareLink } from "../odspDriverUrlResolverForShareLink.js"; import { getLocatorFromOdspUrl, @@ -80,7 +81,7 @@ describe("Tests for OdspDriverUrlResolverForShareLink resolver", () => { response: Promise, callback: () => Promise, ): Promise { - const getFileLinkStub = stub(fileLinkImport, "getFileLink"); + const getFileLinkStub = stub(getFileLink, mockify.key); getFileLinkStub.returns(response); try { return await callback(); diff --git a/packages/drivers/odsp-driver/src/test/socketTests/deltaConnectionUpdateTests.spec.ts b/packages/drivers/odsp-driver/src/test/socketTests/deltaConnectionUpdateTests.spec.ts index d4b319ee306c..35d3d3d9f4af 100644 --- a/packages/drivers/odsp-driver/src/test/socketTests/deltaConnectionUpdateTests.spec.ts +++ b/packages/drivers/odsp-driver/src/test/socketTests/deltaConnectionUpdateTests.spec.ts @@ -15,14 +15,15 @@ import { Socket } from "socket.io-client"; import { OdspFluidDataStoreLocator } from "../../contractsPublic.js"; import { createOdspUrl } from "../../createOdspUrl.js"; import { EpochTracker } from "../../epochTracker.js"; +import { mockify } from "../../mockify.js"; import { LocalPersistentCache } from "../../odspCache.js"; import { OdspDocumentDeltaConnection } from "../../odspDocumentDeltaConnection.js"; import { OdspDocumentService } from "../../odspDocumentService.js"; import { OdspDocumentServiceFactory } from "../../odspDocumentServiceFactory.js"; import { OdspDriverUrlResolver } from "../../odspDriverUrlResolver.js"; import { getHashedDocumentId } from "../../odspPublicUtils.js"; -import * as socketModule from "../../socketModule.js"; -import * as joinSession from "../../vroom.js"; +import { SocketIOClientStatic } from "../../socketModule.js"; +import { fetchJoinSession } from "../../vroom.js"; import { ClientSocketMock } from "./socketMock.js"; @@ -78,7 +79,7 @@ describe("DeltaConnectionMetadata update tests", () => { labels: label, timestamp: Date.now(), }); - const joinSessionStub = stub(joinSession, "fetchJoinSession").callsFake( + const joinSessionStub = stub(fetchJoinSession, mockify.key).callsFake( async () => joinSessionResponse, ); return joinSessionStub; @@ -121,7 +122,7 @@ describe("DeltaConnectionMetadata update tests", () => { }); async function mockSocket(_response: Socket, callback: () => Promise): Promise { - const getSocketCreationStub = stub(socketModule, "SocketIOClientStatic"); + const getSocketCreationStub = stub(SocketIOClientStatic, mockify.key); getSocketCreationStub.returns(_response); try { return await callback(); diff --git a/packages/drivers/odsp-driver/src/test/socketTests/socketTests.spec.ts b/packages/drivers/odsp-driver/src/test/socketTests/socketTests.spec.ts index a4dce16d39a3..bf718661a4af 100644 --- a/packages/drivers/odsp-driver/src/test/socketTests/socketTests.spec.ts +++ b/packages/drivers/odsp-driver/src/test/socketTests/socketTests.spec.ts @@ -22,10 +22,11 @@ import { Socket } from "socket.io-client"; import { v4 as uuid } from "uuid"; import { EpochTracker } from "../../epochTracker.js"; +import { mockify } from "../../mockify.js"; import { LocalPersistentCache } from "../../odspCache.js"; import { OdspDocumentDeltaConnection } from "../../odspDocumentDeltaConnection.js"; import { getHashedDocumentId } from "../../odspPublicUtils.js"; -import * as socketModule from "../../socketModule.js"; +import { SocketIOClientStatic } from "../../socketModule.js"; import { ClientSocketMock } from "./socketMock.js"; @@ -115,7 +116,7 @@ describe("OdspDocumentDeltaConnection tests", () => { }); async function mockSocket(_response: Socket, callback: () => Promise): Promise { - const getSocketCreationStub = stub(socketModule, "SocketIOClientStatic"); + const getSocketCreationStub = stub(SocketIOClientStatic, mockify.key); getSocketCreationStub.returns(_response); try { return await callback(); diff --git a/packages/drivers/odsp-driver/src/vroom.ts b/packages/drivers/odsp-driver/src/vroom.ts index b0ad5a2fdcad..7dc325b0d54c 100644 --- a/packages/drivers/odsp-driver/src/vroom.ts +++ b/packages/drivers/odsp-driver/src/vroom.ts @@ -16,6 +16,7 @@ import { import { v4 as uuid } from "uuid"; import { EpochTracker } from "./epochTracker.js"; +import { mockify } from "./mockify.js"; import { getApiRoot } from "./odspUrlHelper.js"; import { TokenFetchOptionsEx } from "./odspUtils.js"; import { runWithRetry } from "./retryUtils.js"; @@ -42,7 +43,7 @@ interface IJoinSessionBody { * This is optional and used only when collab session is being joined by client acting in app-only mode (i.e. without user context). * If not specified client display name is extracted from the access token that is used to join session. */ -export async function fetchJoinSession( +async function fetchJoinSession( urlParts: IOdspUrlParts, path: string, method: "GET" | "POST", @@ -139,3 +140,6 @@ export async function fetchJoinSession( }, ); } + +const mockableFetchJoinSession = mockify(fetchJoinSession); +export { mockableFetchJoinSession as fetchJoinSession };