-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
RFC: stubbing fonction expressions in ESM modules #2621
Comments
Related issue: #1832 |
It looks like there is some progress being made in Node's test runner: https://github.com/nodejs/node/pull/52848/files#diff-7ef19622f4f1c831ca0e6ed0edc03cb517f16969f1f1b0972a117bd3fa65c44aR1963-R1990. Is that a viable path forwards for you? |
This might also be a pattern that works for you and is already supported: #2538 |
The pattern using I don't think this custom approach should be part of Sinon. After all, you can create a utility wrapper a la this: // I have not run this, probably has some runtime issue or typo
addMockableSupport(sandbox) {
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/get
const handler = {
get: function (target, prop, receiver) {
if (prop === 'stub') {
return function stubWithMockableSupport(obj,prop,val){
if(obj[prop]['__mockHandle']) {
sandbox.stub(obj[prop], '__mockHandle', val)
} else {
sandbox.stub(obj, prop, val)
}
}
}
return Reflect.get(...arguments);
}
}
return new Proxy(sandbox, handler);
} Using that, you can now mySinon = addMockableSupport(sinon)
// should now be able to stub out "mockables", that have been treated with the `mockable(f)` function above
mySinon.stub(someEsmModule, fooMockable) |
Is your feature request related to a problem? Please describe.
We have a large nodejs code base full of fonction expressions such as
it was all nice, and we were able to mock such functions with sinon just fine. But, all nice things have an end, and we now have to migrate to ESM (no specific needs except that third party modules start dropping support for commonjs) and of course we have to deal with
ES Modules cannot be stubbed
.We could move everything to top level objects or default export, but then we would have to change lots of code (as we need to modify all the imports and the call sites) and most annoyingly, we need to somehow make sure that all the call to the function are done using the reference in the top level object,ie
is no good, as stubbing MyModule.myFunction will not work for myFunction2.
Describe the solution you'd like
I figured out a solution that solve my problem in a rather idiomatic JS way (which means it's both horrible and nice)
I defined a small helper function, which adds a property
__mockHandle
to a function, which contains the actual function, and is used in the body of the function:this can then be used that way
and in tests
This seems to work fine, and I'm happy with the solution. What I'm wondering if it would be interesting to add some simple support in sinon for this pattern to allow implicitly use
__mockHandle
(actual name tbd) when calling the stub function with a single parameter to allow doing:I think it would not be a lot of code, is not very intrusive and it would make this pattern a bit nicer to use.
Describe alternatives you've considered
Already mentioned above.
Additional context
Some extra thoughts
mockable
helper, I have not looked into it, but it does seem to be a good use casemockable
should be in sinon. First having it in the user code base allow to skip it in production (by detecting the test framework in my case) and second, this is a run time dependency rather than a dev deps as sinon would generally be.Let me know what you think.
The text was updated successfully, but these errors were encountered: