lukeed/uvu

uvu does not like 'new String()'

Hexagon opened this issue · 4 comments

import { test } from 'uvu';
import * as assert from 'uvu/assert';

/*
    PASS
*/
test('Borked string using .toString()', () => {

    const 
        borkedString = new String('borked-or-not'),
        noiceString = 'borked-or-not';
    
    assert.equal(borkedString.toString(), noiceString);

});

/*  
    FAIL  "Just borked string"
    Cannot use 'in' operator to search for '0' in borked-or-not

    at dequal (file:///.../uvu-test/node_modules/dequal/dist/index.mjs:77:16)
*/
test('Just borked string', () => {
    //assert.not.throws(() => {
        const 
            borkedString = new String('borked-or-not'),
            noiceString = 'borked-or-not';
        
        assert.not.equal(borkedString, noiceString);
    //});
});

test.run();

I am aware that new String() isn't something I'm supposed to do, but I am working on a library that need to handle objects created with new String, hence i need to test it

I am also aware that dequal is what explodes, but i file it in uvu because this is where i experience the issue

wouldn't it make more sense to use "is" instead of equal here?

The assertion library shouldn't throw when comparing string and "string" anyway though :)

"equal" is traversing the keys in the object to see if each key within the object match the other object.. If the 2 objects don't have the same structure theyre not equal. It sort of like === but even further.

"is" is like doing an == comparison.

assert.is is strict equality (===)
assert.equal is a recursive equality checker for keys and constructor match.

invoking new String returns a new String insurance, which is not the same thing as a plain string.

In other words:

new String("foo") === "foo";
//=> false

it’ll only work if you do this:

assert.ok(new String("foo") == "foo")

because that’s the only way this comparison is true in raw js