Skip to content

Commit

Permalink
Make fake#reject and fake#throws not convert strings to Error
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Nov 5, 2023
1 parent 9fef780 commit 5fdc8e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 60 deletions.
47 changes: 14 additions & 33 deletions lib/sinon/fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
};

/**
Expand Down Expand Up @@ -127,27 +121,26 @@ 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(":(");
*
* 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);
Expand Down Expand Up @@ -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);
}
30 changes: 3 additions & 27 deletions test/fake-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
});
Expand All @@ -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 () {
Expand All @@ -290,30 +275,21 @@ 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);

return myFake().catch(function (actual) {
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 () {
Expand Down

0 comments on commit 5fdc8e8

Please sign in to comment.