Skip to content

Commit 1954bcf

Browse files
committed
fix: replace 'should' dependency with native assertions in cookies module
The 'should' module was listed as a devDependency but was required by lib/cookies/assertion.js at runtime. This caused 'Cannot find module should' errors for users installing supertest. This fix replaces all uses of the 'should' assertion library with native JavaScript assertion helper functions: - assertHasProperties: checks if object has specified properties (array or object) - assertNotHasProperties: checks if object does not have specified properties - assertEqual: checks strict equality - assertNotEqual: checks strict inequality The native implementations match the behavior of should.js, including the 'false negative fail' behavior when checking not.have.properties with empty arrays/objects. Fixes: #875 (comment)
1 parent 8fb7453 commit 1954bcf

File tree

1 file changed

+88
-7
lines changed

1 file changed

+88
-7
lines changed

lib/cookies/assertion.js

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,88 @@
33
'use strict';
44

55
const signature = require('cookie-signature');
6-
const should = require('should');
6+
7+
/**
8+
* Assert that an object has specific properties (supports array of keys or object)
9+
*
10+
* @param {object} obj
11+
* @param {object|array} props
12+
*/
13+
function assertHasProperties(obj, props) {
14+
if (Array.isArray(props)) {
15+
props.forEach(function (key) {
16+
if (!(key in obj)) {
17+
throw new Error('expected object to have property ' + key);
18+
}
19+
});
20+
} else {
21+
Object.keys(props).forEach(function (key) {
22+
if (!(key in obj)) {
23+
throw new Error('expected object to have property ' + key);
24+
}
25+
});
26+
}
27+
}
28+
29+
/**
30+
* Assert that an object does not have specific properties (supports array of keys or object)
31+
* When checking with empty props, throws if object exists (matches should.js behavior)
32+
*
33+
* @param {object} obj
34+
* @param {object|array} props
35+
*/
36+
function assertNotHasProperties(obj, props) {
37+
if (Array.isArray(props)) {
38+
// When empty array is passed, should.js throws 'false negative fail' if object exists
39+
if (props.length === 0) {
40+
throw new Error('expected object to not have properties (false negative fail)');
41+
}
42+
props.forEach(function (key) {
43+
if (key in obj) {
44+
throw new Error('expected object to not have property ' + key);
45+
}
46+
});
47+
} else {
48+
// When empty object is passed, should.js throws 'false negative fail' if object exists
49+
let keys = Object.keys(props);
50+
if (keys.length === 0) {
51+
throw new Error('expected object to not have properties (false negative fail)');
52+
}
53+
keys.forEach(function (key) {
54+
if (key in obj) {
55+
throw new Error('expected object to not have property ' + key);
56+
}
57+
});
58+
}
59+
}
60+
61+
/**
62+
* Assert that two values are equal
63+
*
64+
* @param {*} actual
65+
* @param {*} expected
66+
*/
67+
function assertEqual(actual, expected) {
68+
if (actual !== expected) {
69+
throw new Error(
70+
'expected ' + JSON.stringify(actual) + ' to equal ' + JSON.stringify(expected)
71+
);
72+
}
73+
}
74+
75+
/**
76+
* Assert that two values are not equal
77+
*
78+
* @param {*} actual
79+
* @param {*} expected
80+
*/
81+
function assertNotEqual(actual, expected) {
82+
if (actual === expected) {
83+
throw new Error(
84+
'expected ' + JSON.stringify(actual) + ' to not equal ' + JSON.stringify(expected)
85+
);
86+
}
87+
}
788

889
/**
990
* Build Assertion function
@@ -208,8 +289,8 @@ module.exports = function (secret, asserts) {
208289

209290
if (assert && !cookie) throw new Error('expected: ' + expect.name + ' cookie to be set');
210291

211-
if (assert) should(cookie.options).have.properties(expect.options);
212-
else if (cookie) should(cookie.options).not.have.properties(expect.options);
292+
if (assert) assertHasProperties(cookie.options, expect.options);
293+
else if (cookie) assertNotHasProperties(cookie.options, expect.options);
213294
});
214295
});
215296

@@ -362,8 +443,8 @@ module.exports = function (secret, asserts) {
362443
// check cookie values are equal
363444
if ('value' in expect) {
364445
try {
365-
if (assert) should(cookie.value).be.eql(expect.value);
366-
else should(cookie.value).not.be.eql(expect.value);
446+
if (assert) assertEqual(cookie.value, expect.value);
447+
else assertNotEqual(cookie.value, expect.value);
367448
} catch (e) {
368449
if (secret.length) {
369450
let value;
@@ -387,8 +468,8 @@ module.exports = function (secret, asserts) {
387468
? expect.options[key].toString()
388469
: expect.options[key]
389470
);
390-
if (assert) should(cookie.options[key]).be.eql(expected);
391-
else should(cookie.options[key]).not.be.eql(expected);
471+
if (assert) assertEqual(cookie.options[key], expected);
472+
else assertNotEqual(cookie.options[key], expected);
392473
});
393474
});
394475
});

0 commit comments

Comments
 (0)