Comparing version 16.0.0 to 16.1.0
@@ -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 |
@@ -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() { | ||
@@ -201,7 +230,18 @@ const stubbed = sinonCreateStubInstance.apply(null, arguments); | ||
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); | ||
@@ -231,32 +271,34 @@ } else { | ||
/** | ||
* 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 | ||
)}. Perhaps you meant sandbox.define()?` | ||
); | ||
} | ||
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) { | ||
throw new TypeError( | ||
`Cannot replace ${typeof object[ | ||
property | ||
]} with ${typeof replacement}` | ||
); | ||
} | ||
sandbox.replace.usingAccessor = function replaceUsingAccessor( | ||
object, | ||
property, | ||
replacement | ||
) { | ||
const descriptor = getPropertyDescriptor(object, property); | ||
checkForValidArguments(descriptor, property, replacement); | ||
verifySameType(object, property, replacement); | ||
@@ -266,3 +308,3 @@ verifyNotReplaced(object, property); | ||
// store a function for restoring the replaced property | ||
push(fakeRestorers, getFakeRestorer(object, property)); | ||
push(fakeRestorers, getFakeRestorer(object, property, true)); | ||
@@ -269,0 +311,0 @@ object[property] = replacement; |
@@ -18,3 +18,3 @@ { | ||
], | ||
"version": "16.0.0", | ||
"version": "16.1.0", | ||
"homepage": "https://sinonjs.org/", | ||
@@ -48,5 +48,6 @@ "author": "Christian Johansen", | ||
"build": "node ./build.cjs", | ||
"dev-docs": "cd docs; cp -rl 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 .", | ||
@@ -53,0 +54,0 @@ "pretest-webworker": "npm run build", |
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
4924683
62211