Skip to content

Commit

Permalink
Throw meaningful error stubbing ECMAScript Module
Browse files Browse the repository at this point in the history
Ref sinonjs#1711 and sinonjs#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 authored and Franck Romano committed Oct 1, 2019
1 parent 10c3e05 commit 5c6bce1
Showing 5 changed files with 303 additions and 1,185 deletions.
8 changes: 8 additions & 0 deletions lib/sinon/stub.js
Original file line number Diff line number Diff line change
@@ -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)) {
Loading

0 comments on commit 5c6bce1

Please sign in to comment.