diff --git a/.unimportedrc.json b/.unimportedrc.json index 568b965b8..5f7633de4 100644 --- a/.unimportedrc.json +++ b/.unimportedrc.json @@ -1,8 +1,23 @@ { - "entry": ["lib/sinon.js", "lib/sinon-esm.js"], - "extensions": [".js"], - "ignorePatterns": ["**/node_modules/**"], + "entry": [ + "lib/sinon.js", + "lib/sinon-esm.js", + "test/**/*-test.js", + "test/webworker/webworker-script.js", + "test/webworker/webworker-support-assessment.js" + ], + "extensions": [ + ".js" + ], + "ignorePatterns": [ + "**/node_modules/**" + ], "ignoreUnresolved": [], - "ignoreUnimported": ["docs/**", "pkg/**", "test/**"], + "ignoreUnimported": [ + "docs/**", + "pkg/**", + "vendor/**/*", + "test/es2015/check-esm-bundle-is-runnable.js" + ], "ignoreUnused": [] } diff --git a/lib/create-sinon-api.js b/lib/create-sinon-api.js index 8e9d1ca49..9ef9ec126 100644 --- a/lib/create-sinon-api.js +++ b/lib/create-sinon-api.js @@ -21,7 +21,6 @@ module.exports = function createApi(opts = { sinonXhrLib: nise }) { const apiMethods = { createSandbox: createSandbox, - assert: require("./sinon/assert"), match: require("@sinonjs/samsam").createMatcher, restoreObject: require("./sinon/restore-object"), diff --git a/lib/sinon/assert.js b/lib/sinon/assert.js index fc7dbdbe3..43a555804 100644 --- a/lib/sinon/assert.js +++ b/lib/sinon/assert.js @@ -1,4 +1,5 @@ "use strict"; +/** @module */ const arrayProto = require("@sinonjs/commons").prototypes.array; const calledInOrder = require("@sinonjs/commons").calledInOrder; @@ -15,12 +16,48 @@ const forEach = arrayProto.forEach; const join = arrayProto.join; const splice = arrayProto.splice; -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 = { 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; throw error; diff --git a/lib/sinon/create-sandbox.js b/lib/sinon/create-sandbox.js index b958daf0c..2d29cf29c 100644 --- a/lib/sinon/create-sandbox.js +++ b/lib/sinon/create-sandbox.js @@ -7,7 +7,7 @@ const forEach = arrayProto.forEach; const push = arrayProto.push; function prepareSandboxFromConfig(config) { - const sandbox = new Sandbox(); + const sandbox = new Sandbox({ assertOptions: config.assertOptions }); if (config.useFakeServer) { if (typeof config.useFakeServer === "object") { @@ -41,6 +41,34 @@ function exposeValue(sandbox, config, key, value) { } } +/** + * Customize the sandbox. + * This is mostly an integration feature most users will not need + * + * @typedef {object} SandboxConfig + * @property {string[]} properties The properties of the API to expose on the sandbox. Examples: ['spy', 'fake', 'restore'] + * @property {(object|null)} injectInto TBD + * @property {boolean} useFakeTimers whether timers are faked by default + * @property {boolean} useFakeServer whether XHR's are faked and the server feature enabled by default + * @property {object} [assertOptions] see CreateAssertOptions in ./assert + */ +// This type def is really suffering from JSDoc not being +// able to reference types in other modules + +/** + * A configured sinon sandbox. + * + * @typedef {object} ConfiguredSinonSandboxType + * @augments Sandbox + * @property {string[]} injectedKeys the keys that have been injected (from config.injectInto) + * @property {*} injectInto TBD + * @property {*[]} args the arguments for the sandbox + */ + +/** + * @param config {SandboxConfig} + * @returns {Sandbox} + */ function createSandbox(config) { if (!config) { return new Sandbox(); diff --git a/lib/sinon/sandbox.js b/lib/sinon/sandbox.js index ce25a2240..6bab45f96 100644 --- a/lib/sinon/sandbox.js +++ b/lib/sinon/sandbox.js @@ -69,8 +69,16 @@ function checkForValidArguments(descriptor, property, replacement) { } } -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 = []; let promiseLib; @@ -91,7 +99,7 @@ function Sandbox() { } } - sandbox.assert = sinonAssert.createAssertObject(); + sandbox.assert = sinonAssert.createAssertObject(assertOptions); sandbox.serverPrototype = fakeServer; diff --git a/test/assert-test.js b/test/assert-test.js index 036dc2231..69b675017 100644 --- a/test/assert-test.js +++ b/test/assert-test.js @@ -65,6 +65,18 @@ describe("assert", function () { sinonAssert.failException = this.exceptionName; }); + it("can be configured to limit the error message length", function () { + const customAssert = sinonAssert.createAssertObject({ + shouldLimitAssertionLogs: true, + assertionLogLimit: 10, + }); + + assert.exception( + () => customAssert.fail("1234567890--THIS SHOULD NOT SHOW--"), + { message: "1234567890" }, + ); + }); + it("throws exception", function () { assert.exception( function () { diff --git a/test/create-sandbox-test.js b/test/create-sandbox-test.js new file mode 100644 index 000000000..66656c13b --- /dev/null +++ b/test/create-sandbox-test.js @@ -0,0 +1,22 @@ +"use strict"; + +const createSandbox = require("../lib/sinon/create-sandbox"); +const { assert } = require("@sinonjs/referee"); + +describe("create-sandbox", function () { + it("can be configured to limit the error message length", function () { + // Arrange & Act + const sb = createSandbox({ + assertOptions: { + shouldLimitAssertionLogs: true, + assertionLogLimit: 10, + }, + }); + + // Assert + assert.exception( + () => sb.assert.fail("1234567890--THIS SHOULD NOT SHOW--"), + { message: "1234567890" }, + ); + }); +});