Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sinon

Package Overview
Dependencies
Maintainers
4
Versions
208
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 17.0.0 to 17.0.1

lib/create-sinon-api.js

60

lib/sinon.js
"use strict";
const behavior = require("./sinon/behavior");
const createSandbox = require("./sinon/create-sandbox");
const extend = require("./sinon/util/core/extend");
const fakeTimers = require("./sinon/util/fake-timers");
const nise = require("nise");
const Sandbox = require("./sinon/sandbox");
const stub = require("./sinon/stub");
const promise = require("./sinon/promise");
const createApi = require("./create-sinon-api");
/**
* @param {object} opts injection point to override the default XHR lib in testing
* @param {object} opts.sinonXhrLib
* @returns {object} a configured sandbox
*/
function createApi({ sinonXhrLib }) {
const apiMethods = {
createSandbox: createSandbox,
assert: require("./sinon/assert"),
match: require("@sinonjs/samsam").createMatcher,
restoreObject: require("./sinon/restore-object"),
expectation: require("./sinon/mock-expectation"),
defaultConfig: require("./sinon/util/core/default-config"),
// fake timers
timers: fakeTimers.timers,
// fake XHR
xhr: sinonXhrLib.fakeXhr.xhr,
FakeXMLHttpRequest: sinonXhrLib.fakeXhr.FakeXMLHttpRequest,
// fake server
fakeServer: sinonXhrLib.fakeServer,
fakeServerWithClock: sinonXhrLib.fakeServerWithClock,
createFakeServer: sinonXhrLib.fakeServer.create.bind(
sinonXhrLib.fakeServer,
),
createFakeServerWithClock: sinonXhrLib.fakeServerWithClock.create.bind(
sinonXhrLib.fakeServerWithClock,
),
addBehavior: function (name, fn) {
behavior.addBehavior(stub, name, fn);
},
// fake promise
promise: promise,
};
const sandbox = new Sandbox();
return extend(sandbox, apiMethods);
}
const api = createApi({ sinonXhrLib: nise });
module.exports = api;
// solely exposed for easier testing
module.exports.createApi = createApi;
module.exports = createApi();
"use strict";
/** @module */

@@ -18,3 +19,32 @@ const arrayProto = require("@sinonjs/commons").prototypes.array;

function createAssertObject() {
function applyDefaults(obj, defaults) {
for (const key of Object.keys(defaults)) {
const val = obj[key];
if (val === null || typeof val === "undefined") {
obj[key] = defaults[key];
}
}
}
/**
* @typedef {object} CreateAssertOptions
* @global
*
* @property {boolean} [shouldLimitAssertionLogs] default is false
* @property {number} [assertionLogLimit] default is 10K
*/
/**
* Create an assertion object that exposes several methods to invoke
*
* @param {CreateAssertOptions} [opts] options bag
* @returns {object} object with multiple assertion methods
*/
function createAssertObject(opts) {
const cleanedAssertOptions = opts || {};
applyDefaults(cleanedAssertOptions, {
shouldLimitAssertionLogs: false,
assertionLogLimit: 1e4,
});
const assert = {

@@ -24,3 +54,10 @@ failException: "AssertError",

fail: function fail(message) {
const error = new Error(message);
let msg = message;
if (cleanedAssertOptions.shouldLimitAssertionLogs) {
msg = message.substring(
0,
cleanedAssertOptions.assertionLogLimit,
);
}
const error = new Error(msg);
error.name = this.failException || assert.failException;

@@ -27,0 +64,0 @@

@@ -10,3 +10,3 @@ "use strict";

function prepareSandboxFromConfig(config) {
const sandbox = new Sandbox();
const sandbox = new Sandbox({ assertOptions: config.assertOptions });

@@ -45,2 +45,39 @@ if (config.useFakeServer) {

/**
* Options to customize a sandbox
*
* The sandbox's methods can be injected into another object for
* convenience. The `injectInto` configuration option can name an
* object to add properties to.
*
* @typedef {object} SandboxConfig
* @property {string[]} properties The properties of the API to expose on the sandbox. Examples: ['spy', 'fake', 'restore']
* @property {object} injectInto an object in which to inject properties from the sandbox (a facade). This is mostly an integration feature (sinon-test being one).
* @property {boolean} useFakeTimers whether timers are faked by default
* @property {boolean|object} useFakeServer whether XHR's are faked and the server feature enabled by default. It could also be a different default fake server implementation to use
* @property {object} [assertOptions] see CreateAssertOptions in ./assert
*
* This type def is really suffering from JSDoc not having standardized
* how to reference types defined in other modules :(
*/
/**
* A configured sinon sandbox (private type)
*
* @typedef {object} ConfiguredSinonSandboxType
* @private
* @augments Sandbox
* @property {string[]} injectedKeys the keys that have been injected (from config.injectInto)
* @property {*[]} args the arguments for the sandbox
*/
/**
* Create a sandbox
*
* As of Sinon 5 the `sinon` instance itself is a Sandbox, so you
* hardly ever need to create additional instances for the sake of testing
*
* @param config {SandboxConfig}
* @returns {Sandbox}
*/
function createSandbox(config) {

@@ -47,0 +84,0 @@ if (!config) {

@@ -33,7 +33,44 @@ "use strict";

const SKIP_OPTIONS_FOR_YIELDS = {
skipReturn: true,
skipThrows: true,
};
function clear(fake, options) {
fake.fakeFn = undefined;
fake.callsThrough = undefined;
fake.callsThroughWithNew = undefined;
if (!options || !options.skipThrows) {
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.throwArgAt = undefined;
}
fake.callArgAt = undefined;
fake.callbackArguments = undefined;
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = undefined;
if (!options || !options.skipReturn) {
fake.returnValue = undefined;
fake.returnValueDefined = undefined;
fake.returnArgAt = undefined;
fake.returnThis = undefined;
}
fake.resolve = undefined;
fake.resolveThis = undefined;
fake.resolveArgAt = undefined;
fake.reject = undefined;
}
const defaultBehaviors = {
callsFake: function callsFake(fake, fn) {
clear(fake);
fake.fakeFn = fn;
fake.exception = undefined;
fake.exceptionCreator = undefined;
},

@@ -45,8 +82,6 @@

}
clear(fake);
fake.callArgAt = index;
fake.callbackArguments = [];
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = false;
},

@@ -58,2 +93,3 @@

}
clear(fake);

@@ -63,4 +99,2 @@ fake.callArgAt = index;

fake.callbackContext = context;
fake.callArgProp = undefined;
fake.callbackAsync = false;
},

@@ -72,8 +106,6 @@

}
clear(fake);
fake.callArgAt = index;
fake.callbackArguments = slice(arguments, 2);
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = false;
},

@@ -85,2 +117,3 @@

}
clear(fake);

@@ -90,4 +123,2 @@ fake.callArgAt = index;

fake.callbackContext = context;
fake.callArgProp = undefined;
fake.callbackAsync = false;
},

@@ -100,38 +131,34 @@

yields: function (fake) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);
fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice(arguments, 1);
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},
yieldsRight: function (fake) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);
fake.callArgAt = useRightMostCallback;
fake.callbackArguments = slice(arguments, 1);
fake.callbackContext = undefined;
fake.callArgProp = undefined;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},
yieldsOn: function (fake, context) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);
fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice(arguments, 2);
fake.callbackContext = context;
fake.callArgProp = undefined;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},
yieldsTo: function (fake, prop) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);
fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice(arguments, 2);
fake.callbackContext = undefined;
fake.callArgProp = prop;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},
yieldsToOn: function (fake, prop, context) {
clear(fake, SKIP_OPTIONS_FOR_YIELDS);
fake.callArgAt = useLeftMostCallback;

@@ -141,4 +168,2 @@ fake.callbackArguments = slice(arguments, 3);

fake.callArgProp = prop;
fake.callbackAsync = false;
fake.fakeFn = undefined;
},

@@ -150,9 +175,6 @@

returns: function returns(fake, value) {
clear(fake);
fake.returnValue = value;
fake.resolve = false;
fake.reject = false;
fake.returnValueDefined = true;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

@@ -164,2 +186,3 @@

}
clear(fake);

@@ -173,2 +196,3 @@ fake.returnArgAt = index;

}
clear(fake);

@@ -179,2 +203,4 @@ fake.throwArgAt = index;

returnsThis: function returnsThis(fake) {
clear(fake);
fake.returnThis = true;

@@ -184,10 +210,7 @@ },

resolves: function resolves(fake, value) {
clear(fake);
fake.returnValue = value;
fake.resolve = true;
fake.resolveThis = false;
fake.reject = false;
fake.returnValueDefined = true;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

@@ -199,11 +222,6 @@

}
clear(fake);
fake.resolveArgAt = index;
fake.returnValue = undefined;
fake.resolve = true;
fake.resolveThis = false;
fake.reject = false;
fake.returnValueDefined = false;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

@@ -221,10 +239,7 @@

}
clear(fake);
fake.returnValue = reason;
fake.resolve = false;
fake.resolveThis = false;
fake.reject = true;
fake.returnValueDefined = true;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;

@@ -235,13 +250,10 @@ return fake;

resolvesThis: function resolvesThis(fake) {
fake.returnValue = undefined;
fake.resolve = false;
clear(fake);
fake.resolveThis = true;
fake.reject = false;
fake.returnValueDefined = false;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},
callThrough: function callThrough(fake) {
clear(fake);
fake.callsThrough = true;

@@ -251,2 +263,4 @@ },

callThroughWithNew: function callThroughWithNew(fake) {
clear(fake);
fake.callsThroughWithNew = true;

@@ -253,0 +267,0 @@ },

@@ -72,4 +72,12 @@ "use strict";

function Sandbox() {
/**
* A sinon sandbox
*
* @param opts
* @param {object} [opts.assertOptions] see the CreateAssertOptions in ./assert
* @class
*/
function Sandbox(opts = {}) {
const sandbox = this;
const assertOptions = opts.assertOptions || {};
let fakeRestorers = [];

@@ -95,3 +103,3 @@ let promiseLib;

sandbox.assert = sinonAssert.createAssertObject();
sandbox.assert = sinonAssert.createAssertObject(assertOptions);

@@ -98,0 +106,0 @@ sandbox.serverPrototype = fakeServer;

"use strict";
const arrayProto = require("@sinonjs/commons").prototypes.array;
const Colorizer = require("./color");
const color = new Colorizer();
const Colorizer = require("./colorizer");
const colororizer = new Colorizer();
const match = require("@sinonjs/samsam").createMatcher;

@@ -28,5 +28,5 @@ const timesInWords = require("./util/core/times-in-words");

if (!matcher.test(calledArg)) {
matcherMessage = color.red(matcher.message);
matcherMessage = colororizer.red(matcher.message);
if (calledArgumentMessage) {
calledArgumentMessage = color.green(calledArgumentMessage);
calledArgumentMessage = colororizer.green(calledArgumentMessage);
}

@@ -46,5 +46,5 @@ }

if (part.added) {
text = color.green(text);
text = colororizer.green(text);
} else if (part.removed) {
text = color.red(text);
text = colororizer.red(text);
}

@@ -51,0 +51,0 @@ if (diff.length === 2) {

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

],
"version": "17.0.0",
"version": "17.0.1",
"homepage": "https://sinonjs.org/",

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

"test-cloud": "npm run test-headless -- --wd",
"test-webworker": "mochify --no-detect-globals --https-server 8080 --no-request-interception test/webworker/webworker-support-assessment.js",
"test-webworker": "mochify --no-detect-globals --https-server 0 --no-request-interception test/webworker/webworker-support-assessment.js",
"test-esm-support": "mocha test/es2015/module-support-assessment-test.mjs",

@@ -43,0 +43,0 @@ "check-esm-bundle-runs-in-browser": "node test/es2015/check-esm-bundle-is-runnable.js",

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

Sorry, the diff of this file is not supported yet

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