diff --git a/lib/sinon/fake.js b/lib/sinon/fake.js index 8ed39ce3e..8b42542d9 100644 --- a/lib/sinon/fake.js +++ b/lib/sinon/fake.js @@ -74,32 +74,26 @@ fake.returns = function returns(value) { /** * Creates a `fake` that throws an Error. - * If the `value` argument does not have Error in its prototype chain, it will - * be used for creating a new error. * * @example * var f1 = sinon.fake.throws("hello"); * - * f1(); - * // Uncaught Error: hello - * + * try { f1(); } catch (err} { console.log(typeof err, err); } + * // string hello * @example * var f2 = sinon.fake.throws(new TypeError("Invalid argument")); * - * f2(); + * try { f2(); } catch (err} { console.log(typeof err, err); } * // Uncaught TypeError: Invalid argument * * @memberof fake - * @param {*|Error} value + * @param {*} err * @returns {Function} */ -fake.throws = function throws(value) { - // eslint-disable-next-line jsdoc/require-jsdoc - function f() { - throw getError(value); - } - - return wrapFunc(f); +fake.throws = function throws(err) { + return wrapFunc(() => { + throw err; + }); }; /** @@ -127,8 +121,7 @@ fake.resolves = function resolves(value) { /** * Creates a `fake` that returns a promise that rejects to the passed `value` - * argument. When `value` does not have Error in its prototype chain, it will be - * wrapped in an Error. + * argument. * * @example * var f1 = sinon.fake.rejects(":("); @@ -136,18 +129,18 @@ fake.resolves = function resolves(value) { * try { * await ft(); * } catch (error) { - * console.log(error); - * // ":(" + * console.log(typeof error, error); + * // "string :(" * } * * @memberof fake - * @param {*} value + * @param {*} reason * @returns {Function} */ -fake.rejects = function rejects(value) { +fake.rejects = function rejects(reason) { // eslint-disable-next-line jsdoc/require-jsdoc function f() { - return promiseLib.reject(getError(value)); + return promiseLib.reject(reason); } return wrapFunc(f); @@ -273,15 +266,3 @@ function wrapFunc(f) { return proxy; } - -/** - * Returns an Error instance from the passed value, if the value is not - * already an Error instance. - * - * @private - * @param {*} value [description] - * @returns {Error} [description] - */ -function getError(value) { - return value instanceof Error ? value : new Error(value); -} diff --git a/test/fake-test.js b/test/fake-test.js index 41bb49b9e..54c6e8a33 100644 --- a/test/fake-test.js +++ b/test/fake-test.js @@ -229,7 +229,7 @@ describe("fake", function () { try { myFake(); } catch (error) { - assert.equals(error.message, expectedMessage); + assert.equals(error, expectedMessage); } /* eslint-disable no-restricted-syntax */ }); @@ -249,21 +249,6 @@ describe("fake", function () { } /* eslint-disable no-restricted-syntax */ }); - - describe("when passed a String", function () { - it("should throw an Error", function () { - const expected = "lorem ipsum"; - const myFake = fake.throws(expected); - - /* eslint-disable no-restricted-syntax */ - try { - myFake(); - } catch (actual) { - assert.isTrue(actual instanceof Error); - } - /* eslint-disable no-restricted-syntax */ - }); - }); }); describe(".resolves", function () { @@ -290,14 +275,14 @@ describe("fake", function () { const myFake = fake.rejects(expectedMessage); return myFake().catch(function (actual) { - assert.equals(actual.message, expectedMessage); + assert.equals(actual, expectedMessage); }); }); // eslint-disable-next-line mocha/no-setup-in-describe verifyProxy(fake.rejects, "42"); - it("should return the same error type as it is passed", function () { + it("should return the same reason as it is passed", function () { const expected = new TypeError("hello world"); const myFake = fake.rejects(expected); @@ -305,15 +290,6 @@ describe("fake", function () { assert.isTrue(actual instanceof TypeError); }); }); - - it("should reject with an Error when passed a String", function () { - const expected = "lorem ipsum"; - const myFake = fake.rejects(expected); - - return myFake().catch(function (actual) { - assert.isTrue(actual instanceof Error); - }); - }); }); describe(".yields", function () {