Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding operator attribute to assertion error #1253

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/chai/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,20 @@ module.exports = function (_chai, util) {
if (!ok) {
msg = util.getMessage(this, arguments);
var actual = util.getActual(this, arguments);
throw new AssertionError(msg, {
actual: actual
var assertionErrorObjectProperties = {
actual: actual
, expected: expected
, showDiff: showDiff
}, (config.includeStack) ? this.assert : flag(this, 'ssfi'));
};

var operator = util.getOperator(this, arguments);
if (operator) {
assertionErrorObjectProperties.operator = operator;
}

throw new AssertionError(msg,
assertionErrorObjectProperties,
(config.includeStack) ? this.assert : flag(this, 'ssfi'));
}
};

Expand Down
58 changes: 58 additions & 0 deletions lib/chai/utils/getOperator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*!
* Module dependencies
*/

var inspect = require('./inspect');
var config = require('../config');
var flag = require('./flag');

function isObjectType(obj) {
var str = inspect(obj),
type = Object.prototype.toString.call(obj);

if (config.truncateThreshold && str.length >= config.truncateThreshold) {
return (
type === '[object Function]' ||
type === '[object Array]' ||
type === '[object Object]'
);
}

return false;
}

/**
* ### .getGetOperator(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 getGetOperator
* @api public
*/

module.exports = function getOperator(obj, args) {
var negate = flag(obj, 'negate'),
expected = args[3],
msg = negate ? args[2] : args[1];

if (typeof msg === 'function') msg = msg();

msg = msg || '';
if (/\shave\s/.test(msg)) {
return undefined;
}

var isObject = isObjectType(expected);
if (/\snot\s/.test(msg)) {
return isObject ? 'notDeepStrictEqual' : 'notStrictEqual';
}

return isObject ? 'deepStrictEqual' : 'strictEqual';
};
6 changes: 6 additions & 0 deletions lib/chai/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,9 @@ exports.isProxyEnabled = require('./isProxyEnabled');
*/

exports.isNaN = require('./isNaN');

/*!
* getOperator method
*/

exports.getOperator = require('./getOperator');