Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update odsp-driver package tests to run in esm instead of cjs #23728

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

noencke
Copy link
Contributor

@noencke noencke commented Jan 31, 2025

Description

This required updating a few places where functions were being mocked by the sinon library.

The sinon stub() function mocks a given function on a given object. For top-level functions, this meant that the exporting module object's reference to the function was being replaced with the stub. This is no longer permitted in ES6 since module objects are readonly.

To solve this, there is now a helper function mockify() which can decorate a function with a property that allows it to be easily mocked by sinon. All the problematic functions and their mock sites have been updated to use this helper. One function, fetchHelper was already a wrapper around fetch (which was being mocked) - so it has been updated to use the same pattern but in a slightly different way.

This PR updates the package test scripts to build and run in ESM rather than CJS.

It also had to replace two usages of the CJS-supported __dirname with the equivalent code based on import.meta.url in order to build in ESM.

@Copilot Copilot bot review requested due to automatic review settings January 31, 2025 19:23
@github-actions github-actions bot added area: driver Driver related issues area: odsp-driver base: main PRs targeted against main branch labels Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 15 changed files in this pull request and generated no comments.

Files not reviewed (10)
  • packages/drivers/odsp-driver/package.json: Language not supported
  • packages/drivers/odsp-driver/src/test/joinSessionCacheTests.spec.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/test/fetchSnapshot.spec.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/test/socketTests/socketTests.spec.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/test/joinSessionPeriodicCall.spec.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/test/odspDriverUrlResolverForShareLink.spec.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/test/socketTests/deltaConnectionUpdateTests.spec.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/odspUtils.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/test/mockFetch.ts: Evaluated as low risk
  • packages/drivers/odsp-driver/src/vroom.ts: Evaluated as low risk
Comments suppressed due to low confidence (1)

packages/drivers/odsp-driver/src/fetchSnapshot.ts:697

  • Ensure that the changes to use mockify for downloadSnapshot are covered by tests.
export const downloadSnapshot = mockify(
@noencke noencke force-pushed the odsp-driver-esm-tests branch from 603cbfe to d952b82 Compare February 1, 2025 00:32
@@ -101,6 +102,9 @@ export async function getFileLink(
return fileLink;
}

const mockableGetFileLink = mockify(getFileLink);
export { mockableGetFileLink as getFileLink };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also inline mockify when defining the original function, but for this PR it makes the diff really unreadable.

@noencke noencke force-pushed the odsp-driver-esm-tests branch from d952b82 to 15d8a18 Compare February 1, 2025 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: driver Driver related issues area: odsp-driver base: main PRs targeted against main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant