Update odsp-driver package tests to run in esm instead of cjs #23728
+134
−40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 aroundfetch
(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 onimport.meta.url
in order to build in ESM.