Comparing version 15.2.0 to 16.1.3
@@ -107,2 +107,18 @@ # Contributing to Sinon.JS | ||
### Tooling | ||
To transparently handle all issues with different tool versions we recommend using [_ASDF: The Multiple Runtime Manager_][asdf]. You would then need the Ruby and Node plugins. | ||
<details> | ||
``` | ||
asdf plugin add ruby | ||
asdf plugin add nodejs | ||
asdf install | ||
``` | ||
</details> | ||
[asdf]: https://asdf-vm.com | ||
### Run the tests | ||
@@ -137,2 +153,2 @@ | ||
$ node build.js | ||
$ node build.cjs |
@@ -18,3 +18,5 @@ "use strict"; | ||
fake.exceptionCreator = function () { | ||
const newException = new Error(message || ""); | ||
const newException = new Error( | ||
message || `Sinon-provided ${error}` | ||
); | ||
newException.name = error; | ||
@@ -21,0 +23,0 @@ return newException; |
@@ -38,2 +38,36 @@ "use strict"; | ||
function throwOnAccessors(descriptor) { | ||
if (typeof descriptor.get === "function") { | ||
throw new Error("Use sandbox.replaceGetter for replacing getters"); | ||
} | ||
if (typeof descriptor.set === "function") { | ||
throw new Error("Use sandbox.replaceSetter for replacing setters"); | ||
} | ||
} | ||
function verifySameType(object, property, replacement) { | ||
if (typeof object[property] !== typeof replacement) { | ||
throw new TypeError( | ||
`Cannot replace ${typeof object[ | ||
property | ||
]} with ${typeof replacement}` | ||
); | ||
} | ||
} | ||
function checkForValidArguments(descriptor, property, replacement) { | ||
if (typeof descriptor === "undefined") { | ||
throw new TypeError( | ||
`Cannot replace non-existent property ${valueToString( | ||
property | ||
)}. Perhaps you meant sandbox.define()?` | ||
); | ||
} | ||
if (typeof replacement === "undefined") { | ||
throw new TypeError("Expected replacement argument to be defined"); | ||
} | ||
} | ||
function Sandbox() { | ||
@@ -70,7 +104,2 @@ const sandbox = this; | ||
// this is for testing only | ||
sandbox.getRestorers = function () { | ||
return fakeRestorers; | ||
}; | ||
sandbox.createStubInstance = function createStubInstance() { | ||
@@ -111,2 +140,6 @@ const stubbed = sinonCreateStubInstance.apply(null, arguments); | ||
obj.define = function () { | ||
return sandbox.define.apply(null, arguments); | ||
}; | ||
obj.replace = function () { | ||
@@ -164,19 +197,3 @@ return sandbox.replace.apply(null, arguments); | ||
forEach(collection, function (fake) { | ||
if (typeof fake === "function") { | ||
privateResetHistory(fake); | ||
return; | ||
} | ||
const methods = []; | ||
if (fake.get) { | ||
push(methods, fake.get); | ||
} | ||
if (fake.set) { | ||
push(methods, fake.set); | ||
} | ||
forEach(methods, privateResetHistory); | ||
}); | ||
forEach(collection, privateResetHistory); | ||
}; | ||
@@ -204,21 +221,29 @@ | ||
sandbox.restoreContext = function restoreContext() { | ||
let injectedKeys = sandbox.injectedKeys; | ||
const injectInto = sandbox.injectInto; | ||
if (!injectedKeys) { | ||
if (!sandbox.injectedKeys) { | ||
return; | ||
} | ||
forEach(injectedKeys, function (injectedKey) { | ||
delete injectInto[injectedKey]; | ||
forEach(sandbox.injectedKeys, function (injectedKey) { | ||
delete sandbox.injectInto[injectedKey]; | ||
}); | ||
injectedKeys = []; | ||
sandbox.injectedKeys.length = 0; | ||
}; | ||
function getFakeRestorer(object, property) { | ||
/** | ||
* Creates a restorer function for the property | ||
* | ||
* @param {object|Function} object | ||
* @param {string} property | ||
* @param {boolean} forceAssignment | ||
* @returns {Function} restorer function | ||
*/ | ||
function getFakeRestorer(object, property, forceAssignment = false) { | ||
const descriptor = getPropertyDescriptor(object, property); | ||
const value = object[property]; | ||
function restorer() { | ||
if (descriptor.isOwn) { | ||
if (forceAssignment) { | ||
object[property] = value; | ||
} else if (descriptor?.isOwn) { | ||
Object.defineProperty(object, property, descriptor); | ||
@@ -229,2 +254,3 @@ } else { | ||
} | ||
restorer.object = object; | ||
@@ -248,41 +274,68 @@ restorer.property = property; | ||
/** | ||
* Replace an existing property | ||
* | ||
* @param {object|Function} object | ||
* @param {string} property | ||
* @param {*} replacement a fake, stub, spy or any other value | ||
* @returns {*} | ||
*/ | ||
sandbox.replace = function replace(object, property, replacement) { | ||
const descriptor = getPropertyDescriptor(object, property); | ||
checkForValidArguments(descriptor, property, replacement); | ||
throwOnAccessors(descriptor); | ||
verifySameType(object, property, replacement); | ||
if (typeof descriptor === "undefined") { | ||
throw new TypeError( | ||
`Cannot replace non-existent property ${valueToString( | ||
property | ||
)}` | ||
); | ||
} | ||
verifyNotReplaced(object, property); | ||
if (typeof replacement === "undefined") { | ||
throw new TypeError("Expected replacement argument to be defined"); | ||
} | ||
// store a function for restoring the replaced property | ||
push(fakeRestorers, getFakeRestorer(object, property)); | ||
if (typeof descriptor.get === "function") { | ||
throw new Error("Use sandbox.replaceGetter for replacing getters"); | ||
} | ||
object[property] = replacement; | ||
if (typeof descriptor.set === "function") { | ||
throw new Error("Use sandbox.replaceSetter for replacing setters"); | ||
} | ||
return replacement; | ||
}; | ||
if (typeof object[property] !== typeof replacement) { | ||
sandbox.replace.usingAccessor = function replaceUsingAccessor( | ||
object, | ||
property, | ||
replacement | ||
) { | ||
const descriptor = getPropertyDescriptor(object, property); | ||
checkForValidArguments(descriptor, property, replacement); | ||
verifySameType(object, property, replacement); | ||
verifyNotReplaced(object, property); | ||
// store a function for restoring the replaced property | ||
push(fakeRestorers, getFakeRestorer(object, property, true)); | ||
object[property] = replacement; | ||
return replacement; | ||
}; | ||
sandbox.define = function define(object, property, value) { | ||
const descriptor = getPropertyDescriptor(object, property); | ||
if (descriptor) { | ||
throw new TypeError( | ||
`Cannot replace ${typeof object[ | ||
`Cannot define the already existing property ${valueToString( | ||
property | ||
]} with ${typeof replacement}` | ||
)}. Perhaps you meant sandbox.replace()?` | ||
); | ||
} | ||
if (typeof value === "undefined") { | ||
throw new TypeError("Expected value argument to be defined"); | ||
} | ||
verifyNotReplaced(object, property); | ||
// store a function for restoring the replaced property | ||
// store a function for restoring the defined property | ||
push(fakeRestorers, getFakeRestorer(object, property)); | ||
object[property] = replacement; | ||
object[property] = value; | ||
return replacement; | ||
return value; | ||
}; | ||
@@ -368,4 +421,3 @@ | ||
function commonPostInitSetup(args, spy) { | ||
const object = args[0]; | ||
const property = args[1]; | ||
const [object, property, types] = args; | ||
@@ -383,2 +435,7 @@ const isSpyingOnEntireObject = | ||
usePromiseLibrary(promiseLib, ownMethods); | ||
} else if (Array.isArray(types)) { | ||
for (const accessorType of types) { | ||
addToCollection(spy[accessorType]); | ||
usePromiseLibrary(promiseLib, spy[accessorType]); | ||
} | ||
} else { | ||
@@ -385,0 +442,0 @@ addToCollection(spy); |
@@ -13,2 +13,3 @@ "use strict"; | ||
"fake", | ||
"define", | ||
"replace", | ||
@@ -15,0 +16,0 @@ "replaceSetter", |
"use strict"; | ||
module.exports = function getPropertyDescriptor(object, property) { | ||
/* eslint-disable jsdoc/valid-types */ | ||
/* | ||
* The following type def is strictly an illegal JSDoc, but the expression forms a | ||
* legal Typescript union type and is understood by Visual Studio and the IntelliJ | ||
* family of editors. The "TS" flavor of JSDoc is becoming the de-facto standard these | ||
* days for that reason (and the fact that JSDoc is essentially unmaintained) | ||
*/ | ||
/** | ||
* @typedef {{isOwn: boolean} & PropertyDescriptor} SinonPropertyDescriptor | ||
* a slightly enriched property descriptor | ||
* @property {boolean} isOwn true if the descriptor is owned by this object, false if it comes from the prototype | ||
*/ | ||
/* eslint-enable jsdoc/valid-types */ | ||
/** | ||
* Returns a slightly modified property descriptor that one can tell is from the object or the prototype | ||
* | ||
* @param {*} object | ||
* @param {string} property | ||
* @returns {SinonPropertyDescriptor} | ||
*/ | ||
function getPropertyDescriptor(object, property) { | ||
let proto = object; | ||
@@ -22,2 +44,4 @@ let descriptor; | ||
return descriptor; | ||
}; | ||
} | ||
module.exports = getPropertyDescriptor; |
@@ -18,3 +18,3 @@ { | ||
], | ||
"version": "15.2.0", | ||
"version": "16.1.3", | ||
"homepage": "https://sinonjs.org/", | ||
@@ -48,5 +48,6 @@ "author": "Christian Johansen", | ||
"build": "node ./build.cjs", | ||
"dev-docs": "cd docs; rsync -r --delete release-source/ releases/dev; npm run serve-docs", | ||
"build-docs": "cd docs; bundle exec jekyll build", | ||
"serve-docs": "cd docs; bundle exec jekyll serve --incremental --verbose", | ||
"lint": "eslint --max-warnings 99 '**/*.{js,cjs,mjs}'", | ||
"serve-docs": "cd docs; bundle exec jekyll serve --incremental --verbose --livereload", | ||
"lint": "eslint --max-warnings 101 '**/*.{js,cjs,mjs}'", | ||
"unimported": "unimported .", | ||
@@ -73,3 +74,3 @@ "pretest-webworker": "npm run build", | ||
"lint-staged": { | ||
"*.{js,css,md}": "prettier --check", | ||
"**/*.{js,css,md}": "prettier --write", | ||
"*.js": "eslint --quiet", | ||
@@ -97,3 +98,2 @@ "*.mjs": "eslint --quiet --ext mjs --parser-options=sourceType:module" | ||
"dependency-check": "^4.1.0", | ||
"husky": "^6.0.0", | ||
"lint-staged": "^13.2.0", | ||
@@ -143,8 +143,3 @@ "mocha": "^10.2.0", | ||
"mode": "auto" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
} | ||
} |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
4924602
23
62211
0