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

QUnit DeepEqual using two strings #895

Closed
joeJazdzewski opened this Issue Nov 26, 2015 · 2 comments

Comments

3 participants
@joeJazdzewski

I was testing some code using qunit my test kept failing so I tested

var str = new String("hey there");
var s = new String("hey there");
assert.deepEqual(s, str);
assert.equal(s, str);

both equal trys failed

@platinumazure

This comment has been minimized.

Show comment
Hide comment
@platinumazure

platinumazure Dec 1, 2015

Contributor

new String will result in the types actually being objects, not strings. So I would expect assert.equal to fail since str == s is false. As for deepEqual failing, my guess is your implementation stores an ID (or hash code, or...) within the string object, and so deepEqual will fail as well when it compares property-to-property.

Try using string literals rather than objects. For example, if you have a function that returns "hey there", the correct assertion would be:

var result = myHeyThereFunction();
assert.equal(result, "hey there", "Got 'hey there' back!");
// Or better:
assert.strictEqual(result, "hey there", "Got 'hey there' back!");
Contributor

platinumazure commented Dec 1, 2015

new String will result in the types actually being objects, not strings. So I would expect assert.equal to fail since str == s is false. As for deepEqual failing, my guess is your implementation stores an ID (or hash code, or...) within the string object, and so deepEqual will fail as well when it compares property-to-property.

Try using string literals rather than objects. For example, if you have a function that returns "hey there", the correct assertion would be:

var result = myHeyThereFunction();
assert.equal(result, "hey there", "Got 'hey there' back!");
// Or better:
assert.strictEqual(result, "hey there", "Got 'hey there' back!");
@leobalter

This comment has been minimized.

Show comment
Hide comment
@leobalter

leobalter Dec 2, 2015

Member

I can't see a good reason to instantiate string objects using the new String form. If you really need to stick with it, keep in mind different object instances will not pass on the simple equal comparison, if what you want to compare is the value, you can catch its value instead.

var s = new String( "hey there" )
s; // a String typed object, with a collection of each character
typeof s; // "object"

s.toString(); // the value of this object when converted to a simple regular string.
typeof s.toString(); // "string"

Both in == (equivalent to assert.equal) and === (equivalent to assert.strictEqual) you will only have true returning if both objects share the same reference.

What we can still check is the deepEqual assertion, as it should check for the object props.

  assert.deepEqual(new String("foo"), new String("foo")); // fails
  assert.deepEqual(new String(), new String()); // fails
  assert.deepEqual(new Object(), new Object()); // pass
  assert.deepEqual({foo: "bar"}, {foo: "bar"}); // pass
Member

leobalter commented Dec 2, 2015

I can't see a good reason to instantiate string objects using the new String form. If you really need to stick with it, keep in mind different object instances will not pass on the simple equal comparison, if what you want to compare is the value, you can catch its value instead.

var s = new String( "hey there" )
s; // a String typed object, with a collection of each character
typeof s; // "object"

s.toString(); // the value of this object when converted to a simple regular string.
typeof s.toString(); // "string"

Both in == (equivalent to assert.equal) and === (equivalent to assert.strictEqual) you will only have true returning if both objects share the same reference.

What we can still check is the deepEqual assertion, as it should check for the object props.

  assert.deepEqual(new String("foo"), new String("foo")); // fails
  assert.deepEqual(new String(), new String()); // fails
  assert.deepEqual(new Object(), new Object()); // pass
  assert.deepEqual({foo: "bar"}, {foo: "bar"}); // pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment