Skip to content

Commit f7d180c

Browse files
Spencer Goossenssgoossensfatso83
authoredOct 27, 2023
fix: assertion log limit (#2485)
* Cleanup unimported setup & include test files * Remove redundant assign The 'assert' object is already present on the sandbox * fix(#2484): add assertion log limit * Document the existing sandbox and create-sandbox * Expose opts.assertOptions on createSandbox(opts) * Verify options are passed down * Remove needless accessors * Make test behavior oriented rather than implementation specific. --------- Co-authored-by: Spencer Goossens <[email protected]> Co-authored-by: Carl-Erik Kopseng <[email protected]>
1 parent 40caf21 commit f7d180c

File tree

7 files changed

+131
-10
lines changed

7 files changed

+131
-10
lines changed
 

Diff for: ‎.unimportedrc.json

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
{
2-
"entry": ["lib/sinon.js", "lib/sinon-esm.js"],
3-
"extensions": [".js"],
4-
"ignorePatterns": ["**/node_modules/**"],
2+
"entry": [
3+
"lib/sinon.js",
4+
"lib/sinon-esm.js",
5+
"test/**/*-test.js",
6+
"test/webworker/webworker-script.js",
7+
"test/webworker/webworker-support-assessment.js"
8+
],
9+
"extensions": [
10+
".js"
11+
],
12+
"ignorePatterns": [
13+
"**/node_modules/**"
14+
],
515
"ignoreUnresolved": [],
6-
"ignoreUnimported": ["docs/**", "pkg/**", "test/**"],
16+
"ignoreUnimported": [
17+
"docs/**",
18+
"pkg/**",
19+
"vendor/**/*",
20+
"test/es2015/check-esm-bundle-is-runnable.js"
21+
],
722
"ignoreUnused": []
823
}

Diff for: ‎lib/create-sinon-api.js

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ module.exports = function createApi(opts = { sinonXhrLib: nise }) {
2121

2222
const apiMethods = {
2323
createSandbox: createSandbox,
24-
assert: require("./sinon/assert"),
2524
match: require("@sinonjs/samsam").createMatcher,
2625
restoreObject: require("./sinon/restore-object"),
2726

Diff for: ‎lib/sinon/assert.js

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use strict";
2+
/** @module */
23

34
const arrayProto = require("@sinonjs/commons").prototypes.array;
45
const calledInOrder = require("@sinonjs/commons").calledInOrder;
@@ -15,12 +16,48 @@ const forEach = arrayProto.forEach;
1516
const join = arrayProto.join;
1617
const splice = arrayProto.splice;
1718

18-
function createAssertObject() {
19+
function applyDefaults(obj, defaults) {
20+
for (const key of Object.keys(defaults)) {
21+
const val = obj[key];
22+
if (val === null || typeof val === "undefined") {
23+
obj[key] = defaults[key];
24+
}
25+
}
26+
}
27+
28+
/**
29+
* @typedef {object} CreateAssertOptions
30+
* @global
31+
*
32+
* @property {boolean} [shouldLimitAssertionLogs] default is false
33+
* @property {number} [assertionLogLimit] default is 10K
34+
*/
35+
36+
/**
37+
* Create an assertion object that exposes several methods to invoke
38+
*
39+
* @param {CreateAssertOptions} [opts] options bag
40+
* @returns {object} object with multiple assertion methods
41+
*/
42+
function createAssertObject(opts) {
43+
const cleanedAssertOptions = opts || {};
44+
applyDefaults(cleanedAssertOptions, {
45+
shouldLimitAssertionLogs: false,
46+
assertionLogLimit: 1e4,
47+
});
48+
1949
const assert = {
2050
failException: "AssertError",
2151

2252
fail: function fail(message) {
23-
const error = new Error(message);
53+
let msg = message;
54+
if (cleanedAssertOptions.shouldLimitAssertionLogs) {
55+
msg = message.substring(
56+
0,
57+
cleanedAssertOptions.assertionLogLimit,
58+
);
59+
}
60+
const error = new Error(msg);
2461
error.name = this.failException || assert.failException;
2562

2663
throw error;

Diff for: ‎lib/sinon/create-sandbox.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const forEach = arrayProto.forEach;
77
const push = arrayProto.push;
88

99
function prepareSandboxFromConfig(config) {
10-
const sandbox = new Sandbox();
10+
const sandbox = new Sandbox({ assertOptions: config.assertOptions });
1111

1212
if (config.useFakeServer) {
1313
if (typeof config.useFakeServer === "object") {
@@ -41,6 +41,34 @@ function exposeValue(sandbox, config, key, value) {
4141
}
4242
}
4343

44+
/**
45+
* Customize the sandbox.
46+
* This is mostly an integration feature most users will not need
47+
*
48+
* @typedef {object} SandboxConfig
49+
* @property {string[]} properties The properties of the API to expose on the sandbox. Examples: ['spy', 'fake', 'restore']
50+
* @property {(object|null)} injectInto TBD
51+
* @property {boolean} useFakeTimers whether timers are faked by default
52+
* @property {boolean} useFakeServer whether XHR's are faked and the server feature enabled by default
53+
* @property {object} [assertOptions] see CreateAssertOptions in ./assert
54+
*/
55+
// This type def is really suffering from JSDoc not being
56+
// able to reference types in other modules
57+
58+
/**
59+
* A configured sinon sandbox.
60+
*
61+
* @typedef {object} ConfiguredSinonSandboxType
62+
* @augments Sandbox
63+
* @property {string[]} injectedKeys the keys that have been injected (from config.injectInto)
64+
* @property {*} injectInto TBD
65+
* @property {*[]} args the arguments for the sandbox
66+
*/
67+
68+
/**
69+
* @param config {SandboxConfig}
70+
* @returns {Sandbox}
71+
*/
4472
function createSandbox(config) {
4573
if (!config) {
4674
return new Sandbox();

Diff for: ‎lib/sinon/sandbox.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,16 @@ function checkForValidArguments(descriptor, property, replacement) {
6969
}
7070
}
7171

72-
function Sandbox() {
72+
/**
73+
* A sinon sandbox
74+
*
75+
* @param opts
76+
* @param {object} [opts.assertOptions] see the CreateAssertOptions in ./assert
77+
* @class
78+
*/
79+
function Sandbox(opts = {}) {
7380
const sandbox = this;
81+
const assertOptions = opts.assertOptions || {};
7482
let fakeRestorers = [];
7583
let promiseLib;
7684

@@ -91,7 +99,7 @@ function Sandbox() {
9199
}
92100
}
93101

94-
sandbox.assert = sinonAssert.createAssertObject();
102+
sandbox.assert = sinonAssert.createAssertObject(assertOptions);
95103

96104
sandbox.serverPrototype = fakeServer;
97105

Diff for: ‎test/assert-test.js

+12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ describe("assert", function () {
6565
sinonAssert.failException = this.exceptionName;
6666
});
6767

68+
it("can be configured to limit the error message length", function () {
69+
const customAssert = sinonAssert.createAssertObject({
70+
shouldLimitAssertionLogs: true,
71+
assertionLogLimit: 10,
72+
});
73+
74+
assert.exception(
75+
() => customAssert.fail("1234567890--THIS SHOULD NOT SHOW--"),
76+
{ message: "1234567890" },
77+
);
78+
});
79+
6880
it("throws exception", function () {
6981
assert.exception(
7082
function () {

Diff for: ‎test/create-sandbox-test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
const createSandbox = require("../lib/sinon/create-sandbox");
4+
const { assert } = require("@sinonjs/referee");
5+
6+
describe("create-sandbox", function () {
7+
it("can be configured to limit the error message length", function () {
8+
// Arrange & Act
9+
const sb = createSandbox({
10+
assertOptions: {
11+
shouldLimitAssertionLogs: true,
12+
assertionLogLimit: 10,
13+
},
14+
});
15+
16+
// Assert
17+
assert.exception(
18+
() => sb.assert.fail("1234567890--THIS SHOULD NOT SHOW--"),
19+
{ message: "1234567890" },
20+
);
21+
});
22+
});

0 commit comments

Comments
 (0)
Please sign in to comment.