sindresorhus/is

is(value) is not slightly tested #7

gioragutt posted onGitHub

There are no tests that is(value) works correctly.

This makes it hard to make changes for it, f.e replace all the primitive if checks with

if (is.primitive(value)) {
   return typeof value;
}

I had an idea regarding the testType method. Currently, it accepts an optional argument that is an array (excludes).

I played with the method a bit, and it looks something like this:

const testTypeName = (t, fixture, typename) => {
    t.is(m(fixture), typename, `Type name of ${util.inspect(fixture)} is not ${typename}`);
};

// This ensure a certain method matches only the types
// it's supposed to and none of the other methods' types
const testType = (t, type, {exclude, typename} = {}) => {
    for (const [key, value] of types) {
        // TODO: Automatically exclude value types in other tests that we have in the current one.
        // Could reduce the use of `exclude`.
        if (exclude && exclude.indexOf(key) !== -1) {
            continue;
        }

        const assert = key === type ? t.true.bind(t) : t.false.bind(t);
        const is = m[type];
        const fixtures = Array.isArray(value) ? value : [value];

        for (const fixture of fixtures) {
            assert(is(fixture), `Value ${util.inspect(fixture)} is not ${type}`);

            if (typename) {
                testTypeName(t, fixture, typename);
            }
        }
    }
};

basically the third parameter becomes an object with which we can pass optional parameters.

tests with excludes now look like:

test('is.undefined', t => {
    testType(t, 'undefined', {
        exclude: ['nullOrUndefined'],
        typename: 'undefined'
    });
});

test('is.null', t => {
    testType(t, 'null', {
        exclude: ['nullOrUndefined'],
        typename: 'null'
    });
});

See that I can easily pass data(typename) for checking is(null) === 'null'), and just not pass it if I don't want to check it.

This also allows the tests to be a little more verbose (you explicitly say that you exclude those types from the test).


Fund this Issue

$30.00
Rewarded

Rewarded pull request

Recent activities

mrhen was rewarded by sindresorhus for sindresorhus/is# 7
almost 6 years ago
mrhen submitted an output to  sindresorhus/ is# 7
almost 6 years ago