Socket
Socket
Sign inDemoInstall

sinon

Package Overview
Dependencies
Maintainers
4
Versions
206
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sinon - npm Package Compare versions

Comparing version 11.1.1 to 11.1.2

lib/.DS_Store

8

CHANGELOG.md
11.0.1 / 2021-05-26
11.1.2 / 2021-07-27
==================
* Upgrade @sinonjs/fake-timers to latest, see https://github.com/sinonjs/fake-timers/blob/master/CHANGELOG.md#712--2021-05-28
* Copy over acessor properties to target object #2387
11.1.1 / 2021-05-26
==================
* Fix #2379 by using v7 of supports-color

@@ -6,0 +12,0 @@

330

lib/sinon/fake.js

@@ -8,8 +8,244 @@ "use strict";

var slice = arrayProto.slice;
var promiseLib = Promise;
function getError(value) {
return value instanceof Error ? value : new Error(value);
module.exports = fake;
/**
* Returns a `fake` that records all calls, arguments and return values.
*
* When an `f` argument is supplied, this implementation will be used.
*
* @example
* // create an empty fake
* var f1 = sinon.fake();
*
* f1();
*
* f1.calledOnce()
* // true
*
* @example
* function greet(greeting) {
* console.log(`Hello ${greeting}`);
* }
*
* // create a fake with implementation
* var f2 = sinon.fake(greet);
*
* // Hello world
* f2("world");
*
* f2.calledWith("world");
* // true
*
* @param {Function|undefined} f
* @returns {Function}
* @namespace
*/
function fake(f) {
if (arguments.length > 0 && typeof f !== "function") {
throw new TypeError("Expected f argument to be a Function");
}
return wrapFunc(f);
}
/**
* Creates a `fake` that returns the provided `value`, as well as recording all
* calls, arguments and return values.
*
* @example
* var f1 = sinon.fake.returns(42);
*
* f1();
* // 42
*
* @memberof fake
* @param {*} value
* @returns {Function}
*/
fake.returns = function returns(value) {
// eslint-disable-next-line jsdoc/require-jsdoc
function f() {
return value;
}
return wrapFunc(f);
};
/**
* 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
*
* @example
* var f2 = sinon.fake.throws(new TypeError("Invalid argument"));
*
* f2();
* // Uncaught TypeError: Invalid argument
*
* @memberof fake
* @param {*|Error} value
* @returns {Function}
*/
fake.throws = function throws(value) {
// eslint-disable-next-line jsdoc/require-jsdoc
function f() {
throw getError(value);
}
return wrapFunc(f);
};
/**
* Creates a `fake` that returns a promise that resolves to the passed `value`
* argument.
*
* @example
* var f1 = sinon.fake.resolves("apple pie");
*
* await f1();
* // "apple pie"
*
* @memberof fake
* @param {*} value
* @returns {Function}
*/
fake.resolves = function resolves(value) {
// eslint-disable-next-line jsdoc/require-jsdoc
function f() {
return promiseLib.resolve(value);
}
return wrapFunc(f);
};
/**
* 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.
*
* @example
* var f1 = sinon.fake.rejects(":(");
*
* try {
* await ft();
* } catch (error) {
* console.log(error);
* // ":("
* }
*
* @memberof fake
* @param {*} value
* @returns {Function}
*/
fake.rejects = function rejects(value) {
// eslint-disable-next-line jsdoc/require-jsdoc
function f() {
return promiseLib.reject(getError(value));
}
return wrapFunc(f);
};
/**
* Causes `fake` to use a custom Promise implementation, instead of the native
* Promise implementation.
*
* @example
* const bluebird = require("bluebird");
* sinon.fake.usingPromise(bluebird);
*
* @memberof fake
* @param {*} promiseLibrary
* @returns {Function}
*/
fake.usingPromise = function usingPromise(promiseLibrary) {
promiseLib = promiseLibrary;
return fake;
};
/**
* Returns a `fake` that calls the callback with the defined arguments.
*
* @example
* function callback() {
* console.log(arguments.join("*"));
* }
*
* const f1 = sinon.fake.yields("apple", "pie");
*
* f1(callback);
* // "apple*pie"
*
* @memberof fake
* @returns {Function}
*/
fake.yields = function yields() {
var values = slice(arguments);
// eslint-disable-next-line jsdoc/require-jsdoc
function f() {
var callback = arguments[arguments.length - 1];
if (typeof callback !== "function") {
throw new TypeError("Expected last argument to be a function");
}
callback.apply(null, values);
}
return wrapFunc(f);
};
/**
* Returns a `fake` that calls the callback **asynchronously** with the
* defined arguments.
*
* @example
* function callback() {
* console.log(arguments.join("*"));
* }
*
* const f1 = sinon.fake.yields("apple", "pie");
*
* f1(callback);
*
* setTimeout(() => {
* // "apple*pie"
* });
*
* @memberof fake
* @returns {Function}
*/
fake.yieldsAsync = function yieldsAsync() {
var values = slice(arguments);
// eslint-disable-next-line jsdoc/require-jsdoc
function f() {
var callback = arguments[arguments.length - 1];
if (typeof callback !== "function") {
throw new TypeError("Expected last argument to be a function");
}
nextTick(function () {
callback.apply(null, values);
});
}
return wrapFunc(f);
};
var uuid = 0;
/**
* Creates a proxy (sinon concept) from the passed function.
*
* @private
* @param {Function} f
* @returns {Function}
*/
function wrapFunc(f) {

@@ -42,82 +278,12 @@ var proxy;

function fakeClass() {
var promiseLib = null;
if (typeof Promise === "function") {
promiseLib = Promise;
}
function fake(f) {
if (arguments.length > 0 && typeof f !== "function") {
throw new TypeError("Expected f argument to be a Function");
}
return wrapFunc(f);
}
fake.returns = function returns(value) {
function f() {
return value;
}
return wrapFunc(f);
};
fake.throws = function throws(value) {
function f() {
throw getError(value);
}
return wrapFunc(f);
};
fake.resolves = function resolves(value) {
function f() {
return promiseLib.resolve(value);
}
return wrapFunc(f);
};
fake.rejects = function rejects(value) {
function f() {
return promiseLib.reject(getError(value));
}
return wrapFunc(f);
};
fake.usingPromise = function usingPromise(promiseLibrary) {
promiseLib = promiseLibrary;
return fake;
};
function yieldInternal(async, values) {
function f() {
var callback = arguments[arguments.length - 1];
if (typeof callback !== "function") {
throw new TypeError("Expected last argument to be a function");
}
if (async) {
nextTick(function () {
callback.apply(null, values);
});
} else {
callback.apply(null, values);
}
}
return wrapFunc(f);
}
fake.yields = function yields() {
return yieldInternal(false, slice(arguments));
};
fake.yieldsAsync = function yieldsAsync() {
return yieldInternal(true, slice(arguments));
};
return fake;
/**
* 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);
}
module.exports = fakeClass();

@@ -105,9 +105,26 @@ "use strict";

}
Object.defineProperty(dest, prop, {
const descriptors = {
configurable: sourceOwnPropertyDescriptor.configurable,
enumerable: sourceOwnPropertyDescriptor.enumerable,
writable: sourceOwnPropertyDescriptor.writable,
value: sourceOwnPropertyDescriptor.value,
});
};
/*
if the sorce has an Accessor property copy over the accessor functions (get and set)
data properties has writable attribute where as acessor property don't
REF: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#properties
*/
if (hasOwnProperty(sourceOwnPropertyDescriptor, "writable")) {
descriptors.writable = sourceOwnPropertyDescriptor.writable;
descriptors.value = sourceOwnPropertyDescriptor.value;
} else {
if (sourceOwnPropertyDescriptor.get) {
descriptors.get =
sourceOwnPropertyDescriptor.get.bind(dest);
}
if (sourceOwnPropertyDescriptor.set) {
descriptors.set =
sourceOwnPropertyDescriptor.set.bind(dest);
}
}
Object.defineProperty(dest, prop, descriptors);
}

@@ -114,0 +131,0 @@ );

@@ -18,3 +18,3 @@ {

],
"version": "11.1.1",
"version": "11.1.2",
"homepage": "https://sinonjs.org/",

@@ -76,3 +76,3 @@ "author": "Christian Johansen",

"@sinonjs/commons": "^1.8.3",
"@sinonjs/fake-timers": "^7.1.0",
"@sinonjs/fake-timers": "^7.1.2",
"@sinonjs/samsam": "^6.0.2",

@@ -79,0 +79,0 @@ "diff": "^5.0.0",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc