mardi 17 mai 2016

Why is my typeof check undefined failing my Mocha test?

This code does not pass the test

Functions:

function setTheme(theme) {
    chartTheme = theme;
}

function getTheme() {
    if (typeof chartTheme === undefined) {
        chartTheme = 'dark';
    }
    return chartTheme;
}

Test:

it('If theme is undefined, expect dark', function() {
    ChartFactory.setTheme(undefined);
    expect(ChartFactory.getTheme()).to.equal('dark');
});

enter image description here

However this does past the test, if I check for "undefined" as a string.

function getTheme() {
    if (typeof chartTheme === 'undefined') {
        chartTheme = 'dark';
    }
    return chartTheme;
}

Aucun commentaire:

Enregistrer un commentaire