-
-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding operator attribute to assertion error (#1257)
* feat: adding operator attribute to assertion error You can set the operator as mentioned below. flag(this, 'operator', MYOPERATOR). for example, flag(obj, 'operator', 'notEqual')
- Loading branch information
1 parent
1958341
commit 8dc92d8
Showing
5 changed files
with
363 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
var type = require('type-detect'); | ||
|
||
var flag = require('./flag'); | ||
|
||
function isObjectType(obj) { | ||
var objectType = type(obj); | ||
var objectTypes = ['Array', 'Object', 'function']; | ||
|
||
return objectTypes.indexOf(objectType) !== -1; | ||
} | ||
|
||
/** | ||
* ### .getOperator(message) | ||
* | ||
* Extract the operator from error message. | ||
* Operator defined is based on below link | ||
* https://nodejs.org/api/assert.html#assert_assert. | ||
* | ||
* Returns the `operator` or `undefined` value for an Assertion. | ||
* | ||
* @param {Object} object (constructed Assertion) | ||
* @param {Arguments} chai.Assertion.prototype.assert arguments | ||
* @namespace Utils | ||
* @name getOperator | ||
* @api public | ||
*/ | ||
|
||
module.exports = function getOperator(obj, args) { | ||
var operator = flag(obj, 'operator'); | ||
var negate = flag(obj, 'negate'); | ||
var expected = args[3]; | ||
var msg = negate ? args[2] : args[1]; | ||
|
||
if (operator) { | ||
return operator; | ||
} | ||
|
||
if (typeof msg === 'function') msg = msg(); | ||
|
||
msg = msg || ''; | ||
if (!msg) { | ||
return undefined; | ||
} | ||
|
||
if (/\shave\s/.test(msg)) { | ||
return undefined; | ||
} | ||
|
||
var isObject = isObjectType(expected); | ||
if (/\snot\s/.test(msg)) { | ||
return isObject ? 'notDeepStrictEqual' : 'notStrictEqual'; | ||
} | ||
|
||
return isObject ? 'deepStrictEqual' : 'strictEqual'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters