Skip to content

Commit 8002bc1

Browse files
authored
Expose strict mode functionality (#41)
1 parent 3d981b5 commit 8002bc1

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,43 @@ This module is used for writing unit tests for your applications, you can access
77
It aims to be fully compatibe with the [node.js assert module](http://nodejs.org/api/assert.html), same API and same behavior, just adding support for web browsers.
88
The API and code may contain traces of the [CommonJS Unit Testing 1.0 spec](http://wiki.commonjs.org/wiki/Unit_Testing/1.0) which they were based on, but both have evolved significantly since then.
99

10+
A `strict` and a `legacy` mode exist, while it is recommended to only use `strict mode`.
11+
12+
## Strict mode
13+
14+
When using the `strict mode`, any `assert` function will use the equality used in the strict function mode. So `assert.deepEqual()` will, for example, work the same as `assert.deepStrictEqual()`.
15+
16+
It can be accessed using:
17+
18+
```js
19+
const assert = require('assert').strict;
20+
```
21+
22+
## Legacy mode
23+
24+
> Deprecated: Use strict mode instead.
25+
26+
When accessing `assert` directly instead of using the `strict` property, the
27+
[Abstract Equality Comparison](https://tc39.github.io/ecma262/#sec-abstract-equality-comparison) will be used for any function without a
28+
"strict" in its name (e.g. `assert.deepEqual()`).
29+
30+
It can be accessed using:
31+
32+
```js
33+
const assert = require('assert');
34+
```
35+
36+
It is recommended to use the `strict mode` instead as the Abstract Equality Comparison can often have surprising results. Especially
37+
in case of `assert.deepEqual()` as the used comparison rules there are very lax.
38+
39+
E.g.
40+
41+
```js
42+
// WARNING: This does not throw an AssertionError!
43+
assert.deepEqual(/a/gi, new Date());
44+
```
45+
46+
1047
## assert.fail(actual, expected, message, operator)
1148
Throws an exception that displays the values for actual and expected separated by the provided operator.
1249

assert.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var objectAssign = require('object-assign');
4+
35
// compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js
46
// original notice:
57

@@ -483,6 +485,18 @@ assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
483485

484486
assert.ifError = function(err) { if (err) throw err; };
485487

488+
// Expose a strict only variant of assert
489+
function strict(value, message) {
490+
if (!value) fail(value, true, message, '==', strict);
491+
}
492+
assert.strict = objectAssign(strict, assert, {
493+
equal: assert.strictEqual,
494+
deepEqual: assert.deepStrictEqual,
495+
notEqual: assert.notStrictEqual,
496+
notDeepEqual: assert.notDeepStrictEqual
497+
});
498+
assert.strict.strict = assert.strict;
499+
486500
var objectKeys = Object.keys || function (obj) {
487501
var keys = [];
488502
for (var key in obj) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"main": "./assert.js",
1515
"dependencies": {
16+
"object-assign": "^4.1.1",
1617
"util": "0.10.3"
1718
},
1819
"devDependencies": {

test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,19 @@ function tests (assert, what) {
342342
assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no');
343343
}
344344
});
345+
346+
test('assert - strict mode', function () {
347+
var assertStrict = assert.strict;
348+
349+
assertStrict.throws(makeBlock(assertStrict.equal, 1, true), assertStrict.AssertionError);
350+
assertStrict.notEqual(0, false);
351+
assertStrict.throws(makeBlock(assertStrict.deepEqual, 1, true), assertStrict.AssertionError);
352+
assertStrict.notDeepEqual(0, false);
353+
assertStrict.equal(assertStrict.strict, assertStrict.strict.strict);
354+
assertStrict.equal(assertStrict.equal, assertStrict.strictEqual);
355+
assertStrict.equal(assertStrict.deepEqual, assertStrict.deepStrictEqual);
356+
assertStrict.equal(assertStrict.notEqual, assertStrict.notStrictEqual);
357+
assertStrict.equal(assertStrict.notDeepEqual, assertStrict.notDeepStrictEqual);
358+
assertStrict.equal(Object.keys(assertStrict).length, Object.keys(assert).length);
359+
});
345360
}

0 commit comments

Comments
 (0)