Skip to content

Commit

Permalink
Throw meaningful error stubbing ECMAScript Module
Browse files Browse the repository at this point in the history
Ref #1711 and #1623 for background.

This feature is simply about throwing a meaningful error, instead
one that tells the user that the property cannot be deleted. As
the exports from ECMAScript modules are immutable once created we
cannot do anything about them from the view of Sinon, so it's
preferable to simply tell the user that when we can.

This should not affect transpiled code (say, using Babel), as the
resulting modules are no longer true ECMAScript modules (but rather
some form of CommonJS module).
  • Loading branch information
fatso83 committed Mar 5, 2018
1 parent b491a57 commit 3ede6ee
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 1,185 deletions.
8 changes: 8 additions & 0 deletions lib/sinon/stub.js
Expand Up @@ -18,6 +18,14 @@ function stub(object, property) {
throw new TypeError("stub(obj, 'meth', fn) has been removed, see documentation");
}

if (
object &&
typeof Symbol !== "undefined" &&
object[Symbol.toStringTag] === "Module"
) {
throw new TypeError("No support for stubbing ES Modules");
}

throwOnFalsyObject.apply(null, arguments);

if (object && typeof property !== "undefined" && !(property in object)) {
Expand Down

0 comments on commit 3ede6ee

Please sign in to comment.