Skip to content

Commit 41ae5be

Browse files
lib: add strict option to Assert
1 parent 2e638da commit 41ae5be

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

doc/api/assert.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ The `Assert` class allows creating independent assertion instances with custom o
230230
* `options` {Object}
231231
* `diff` {string} If set to `'full'`, shows the full diff in assertion errors. Defaults to `'simple'`.
232232
Accepted values: `'simple'`, `'full'`.
233+
* `strict` {boolean} If set to `true`, non-strict methods behave like their
234+
corresponding strict methods. Defaults to `true`.
233235

234236
Creates a new assertion instance. The `diff` option controls the verbosity of diffs in assertion error messages.
235237

lib/assert.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,22 @@ const NO_EXCEPTION_SENTINEL = {};
8888
* Assert options.
8989
* @typedef {object} AssertOptions
9090
* @property {'full'|'simple'} [diff='simple'] - If set to 'full', shows the full diff in assertion errors.
91+
* @property {boolean} [strict=true] - If set to true, non-strict methods behave like their corresponding
92+
* strict methods.
9193
*/
9294

9395
/**
9496
* @class Assert
9597
* @param {AssertOptions} [options] - Optional configuration for assertions.
9698
* @throws {ERR_INVALID_ARG_TYPE} If not called with `new`.
9799
*/
98-
function Assert(options = {}) {
100+
function Assert(options) {
99101
if (!new.target) {
100102
throw new ERR_INVALID_ARG_TYPE('Assert', 'constructor', Assert);
101103
}
102104

105+
options = ObjectAssign({ strict: true }, options);
106+
103107
const allowedDiffs = ['simple', 'full'];
104108
if (
105109
options.diff !== undefined &&
@@ -120,6 +124,13 @@ function Assert(options = {}) {
120124
configurable: false,
121125
writable: false,
122126
});
127+
128+
if (options.strict) {
129+
this.equal = this.strictEqual;
130+
this.deepEqual = this.deepStrictEqual;
131+
this.notEqual = this.notStrictEqual;
132+
this.notDeepEqual = this.notDeepStrictEqual;
133+
}
123134
}
124135

125136
// All of the following functions must throw an AssertionError
@@ -864,7 +875,7 @@ function strict(...args) {
864875
innerOk(strict, args.length, ...args);
865876
}
866877

867-
const assertInstance = new Assert({ diff: 'simple' });
878+
const assertInstance = new Assert({ diff: 'simple', strict: false });
868879
[
869880
'ok', 'fail', 'equal', 'notEqual', 'deepEqual', 'notDeepEqual',
870881
'deepStrictEqual', 'notDeepStrictEqual', 'strictEqual',

test/parallel/test-assert-class.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ test('Assert constructor requires new', () => {
2424
);
2525
});
2626

27-
test('Assert class basic instance', () => {
28-
const assertInstance = new Assert({ diff: undefined });
27+
test('Assert class non strict', () => {
28+
const assertInstance = new Assert({ diff: undefined, strict: false });
2929

3030
assertInstance.ok(assert.AssertionError.prototype instanceof Error,
3131
'assert.AssertionError instanceof Error');
@@ -44,6 +44,8 @@ test('Assert class basic instance', () => {
4444
}
4545
);
4646
assertInstance.equal(undefined, undefined);
47+
assertInstance.equal(null, undefined);
48+
assertInstance.equal(2, '2');
4749
assertInstance.notEqual(true, false);
4850
assertInstance.throws(
4951
() => assertInstance.deepEqual(/a/),
@@ -117,6 +119,15 @@ test('Assert class basic instance', () => {
117119
/* eslint-enable no-restricted-syntax */
118120
});
119121

122+
test('Assert class strict', () => {
123+
const assertInstance = new Assert();
124+
125+
assertInstance.equal(assertInstance.equal, assertInstance.strictEqual);
126+
assertInstance.equal(assertInstance.deepEqual, assertInstance.deepStrictEqual);
127+
assertInstance.equal(assertInstance.notEqual, assertInstance.notStrictEqual);
128+
assertInstance.equal(assertInstance.notDeepEqual, assertInstance.notDeepStrictEqual);
129+
});
130+
120131
test('Assert class with invalid diff option', () => {
121132
assert.throws(
122133
() => new Assert({ diff: 'invalid' }),
@@ -128,8 +139,8 @@ test('Assert class with invalid diff option', () => {
128139
);
129140
});
130141

131-
test('Assert class with full diff', () => {
132-
const assertInstance = new Assert({ diff: 'full' });
142+
test('Assert class non strict with full diff', () => {
143+
const assertInstance = new Assert({ diff: 'full', strict: false });
133144

134145
const longStringOfAs = 'A'.repeat(1025);
135146
const longStringOFBs = 'B'.repeat(1025);
@@ -173,8 +184,8 @@ test('Assert class with full diff', () => {
173184
});
174185
});
175186

176-
test('Assert class with simple diff', () => {
177-
const assertInstance = new Assert({ diff: 'simple' });
187+
test('Assert class non strict with simple diff', () => {
188+
const assertInstance = new Assert({ diff: 'simple', strict: false });
178189

179190
const longStringOfAs = 'A'.repeat(1025);
180191
const longStringOFBs = 'B'.repeat(1025);

0 commit comments

Comments
 (0)