Is your feature request related to a problem? Please describe.
Just a question - are es6 modules supported (given that its cache is not exposed via hooks)?
I need this info to update my testing guide here:
https://github.com/goldbergyoni/javascript-testing-best-practices
Yes and no. Not sure what you mean by caching, as that usually implies some detail of the runtime environment (like Node's require cache). See this test for how Sinon is imported as a true ES module.
If you mean whether stubbing and spying on ES Module, is supported the answer is that _it depends_. This test lists what is possible and not: https://github.com/sinonjs/sinon/blob/master/test/es2015/module-support-assessment-test.es6
The EcmaScript spec itself dictates that the _runtime_ should prevent you from modifying the ES Module, meaning the runtime (not Sinon) will throw an error if you try to do stuff such as aModule.foo = 2 if that module had named exports like this:
export const foo = 1;
and was imported like import * as aModule from './a-module', where aModule would act as a namespace.
On the other hand, if you had an export like this:
export default { foo: 1 }
imported using import aModule from './another-module', then you would have no issues mutating that export using aModule.foo = 2, as we are not modifying the ES Module, but the object that was exported as the default. The namespace of the Module would be unchanged.
As Sinon's stubbing features rely on modifying EcmaScript objects/functions, the same rules apply to it as any other software dealing with EcmaScript Modules
Those rules start bending as soon as you tamper with or _simulate_ the runtime, though. For instance, if using Babel and/or Webpack, etc. to bundle ES2015+ for ES5.1 browsers, or requiring the esm in Node package (like we do).
The behaviour of those environment, how it is done (which varies from one major version to another) and what you can do is totally unsupported from us. That doesn't mean it is not possible, just that we cannot explicitly say "Sinon supports stubbing ES Modules in Webpack", as that is a machinery in constant movement and outside of our control. We _can_ include or link to tutorials for how it's done, though.
Most helpful comment
Yes and no. Not sure what you mean by caching, as that usually implies some detail of the runtime environment (like Node's require cache). See this test for how Sinon is imported as a true ES module.
If you mean whether stubbing and spying on ES Module, is supported the answer is that _it depends_. This test lists what is possible and not: https://github.com/sinonjs/sinon/blob/master/test/es2015/module-support-assessment-test.es6
The EcmaScript spec itself dictates that the _runtime_ should prevent you from modifying the ES Module, meaning the runtime (not Sinon) will throw an error if you try to do stuff such as
aModule.foo = 2if that module had named exports like this:and was imported like
import * as aModule from './a-module', whereaModulewould act as a namespace.On the other hand, if you had an export like this:
imported using
import aModule from './another-module', then you would have no issues mutating that export usingaModule.foo = 2, as we are not modifying the ES Module, but the object that was exported as the default. The namespace of the Module would be unchanged.As Sinon's stubbing features rely on modifying EcmaScript objects/functions, the same rules apply to it as any other software dealing with EcmaScript Modules
Those rules start bending as soon as you tamper with or _simulate_ the runtime, though. For instance, if using Babel and/or Webpack, etc. to bundle ES2015+ for ES5.1 browsers, or requiring the
esmin Node package (like we do).The behaviour of those environment, how it is done (which varies from one major version to another) and what you can do is totally unsupported from us. That doesn't mean it is not possible, just that we cannot explicitly say "Sinon supports stubbing ES Modules in Webpack", as that is a machinery in constant movement and outside of our control. We _can_ include or link to tutorials for how it's done, though.