Skip to content

Commit

Permalink
Extract ES Module detection and improve error
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Mar 5, 2018
1 parent 3ede6ee commit f6b89a1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/sinon/stub.js
Expand Up @@ -6,6 +6,7 @@ var spy = require("./spy");
var extend = require("./util/core/extend");
var functionToString = require("./util/core/function-to-string");
var getPropertyDescriptor = require("./util/core/get-property-descriptor");
var isEsModule = require("./util/core/is-es-module");
var wrapMethod = require("./util/core/wrap-method");
var stubEntireObject = require("./stub-entire-object");
var throwOnFalsyObject = require("./throw-on-falsy-object");
Expand All @@ -18,12 +19,8 @@ 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");
if (isEsModule(object)) {
throw new TypeError("ES Modules cannot be stubbed");
}

throwOnFalsyObject.apply(null, arguments);
Expand Down
16 changes: 16 additions & 0 deletions lib/sinon/util/core/is-es-module.js
@@ -0,0 +1,16 @@
"use strict";

/**
* Verify if an object is a ECMAScript Module
*
* As the exports from a module is immutable we cannot alter the exports
* using spies or stubs. Let the consumer know this to avoid bug reports
* on weird error messages.
*/
module.exports = function (object) {
return (
object &&
typeof Symbol !== "undefined" &&
object[Symbol.toStringTag] === "Module"
);
};

This comment has been minimized.

Copy link
@jdalton

jdalton Mar 13, 2018

Contributor

The namespace object will also have a null [[Prototype]] via Object.getPrototypeOf(object) === null.

0 comments on commit f6b89a1

Please sign in to comment.