diff --git a/packages/driver/src/cypress/chai_jquery.js b/packages/driver/src/cypress/chai_jquery.js index 8cff7ffd33..ea432c6d83 100644 --- a/packages/driver/src/cypress/chai_jquery.js +++ b/packages/driver/src/cypress/chai_jquery.js @@ -13,7 +13,7 @@ const selectors = { focused: 'focused', } -const attrs = { +const accessors = { attr: 'attribute', css: 'CSS property', prop: 'property', @@ -25,6 +25,12 @@ const attrs = { // the methods on it const wrap = (ctx) => $(ctx._obj) +const maybeCastNumberToString = (num) => { + // if this is a finite number (no Infinity or NaN) + // cast to a string + return _.isFinite(num) ? `${num}` : num +} + const $chaiJquery = (chai, chaiUtils, callbacks = {}) => { const { inspect, flag } = chaiUtils @@ -111,6 +117,8 @@ const $chaiJquery = (chai, chaiUtils, callbacks = {}) => { }) chai.Assertion.addMethod('id', function (id) { + id = maybeCastNumberToString(id) + return assert( this, 'id', @@ -145,6 +153,8 @@ const $chaiJquery = (chai, chaiUtils, callbacks = {}) => { }) chai.Assertion.addMethod('text', function (text) { + text = maybeCastNumberToString(text) + assertDom( this, 'text', @@ -168,6 +178,8 @@ const $chaiJquery = (chai, chaiUtils, callbacks = {}) => { }) chai.Assertion.addMethod('value', function (value) { + value = maybeCastNumberToString(value) + assertDom( this, 'value', @@ -250,23 +262,23 @@ const $chaiJquery = (chai, chaiUtils, callbacks = {}) => { }) }) - _.each(attrs, (description, attr) => { - return chai.Assertion.addMethod(attr, function (name, val) { + _.each(accessors, (description, accessor) => { + return chai.Assertion.addMethod(accessor, function (name, val) { assertDom( this, - attr, + accessor, `expected #{this} to have ${description} #{exp}`, `expected #{this} not to have ${description} #{exp}`, name, ) - const actual = wrap(this)[attr](name) + const actual = wrap(this)[accessor](name) // when we only have 1 argument dont worry about val if (arguments.length === 1) { assert( this, - attr, + accessor, actual !== undefined, `expected #{this} to have ${description} #{exp}`, `expected #{this} not to have ${description} #{exp}`, @@ -276,7 +288,7 @@ const $chaiJquery = (chai, chaiUtils, callbacks = {}) => { // change the subject this._obj = actual } else { - // if we don't have an attribute here at all we need to + // if we don't have an accessor here at all we need to // have a different failure message let message; let negatedMessage @@ -290,9 +302,17 @@ const $chaiJquery = (chai, chaiUtils, callbacks = {}) => { negatedMessage = `expected \#{this} not to have ${description} ${inspect(name)} with the value \#{exp}, but the value was \#{act}` } + // only cast .attr() as a string + // since prop stores the native javascript type + // and we don't want to optimistically cast those + // values as a string + if (accessor === 'attr') { + val = maybeCastNumberToString(val) + } + assert( this, - attr, + accessor, (actual != null) && (actual === val), message, negatedMessage, diff --git a/packages/driver/test/cypress/fixtures/jquery.html b/packages/driver/test/cypress/fixtures/jquery.html index 9dee06f92b..47244043e6 100644 --- a/packages/driver/test/cypress/fixtures/jquery.html +++ b/packages/driver/test/cypress/fixtures/jquery.html @@ -7,7 +7,7 @@ foo
div
- +