Sinon: Questions: are es6 modules supported?

Created on 3 Dec 2019  路  1Comment  路  Source: sinonjs/sinon

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

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 = 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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zimtsui picture zimtsui  路  3Comments

brettz9 picture brettz9  路  3Comments

stevenmusumeche picture stevenmusumeche  路  3Comments

tinganho picture tinganho  路  3Comments

stephanwlee picture stephanwlee  路  3Comments