Search code examples
javascriptmocha.jsappiumchaiappium-ios

Mocha conditional testing - If else always passes


I'm trying to do a conditional test on one of my elements, but mocha just skips the "if else" statements completely and just displays the output of "it("")"

Here's how I'm doing it:

    it('if test case', async function () {

        var cardProcZero = await driver.hasElementByAccessibilityId("card_proc_0"); // looks up the element

        let visivel = expect(cardProcZero).to.be.ok; // expects it to exist 
        let naoVisivel = expect(cardProcZero).to.not.be.ok; // expects it not to exist

        if (cardProcZero == naoVisivel) { // if it's not visible, it creates the element
            let adicionarProc = await driver.waitForElementByAccessibilityId("button_addProcedimento"); // adds the element
            await adicionarProc.click();
            return false;

        } else if (cardProcZero == visivel) { // if it is visible, it clicks on it
            await cardProcZero.click(); // clicks on existing element
            return true;
        }
    });

In the terminal, mocha outputs this, right after my last test case:

✓ if test case (527ms)

It doesn't even try to do the other existing "it's". It just skips it.

What exactly am I doing wrong?


Solution

  • Tried doing it without the chai.js expect, and it works.

    Here's how I did it, if anyone has the same question:

    it('if test case', async function () {
        var cardProcZeroVerify = await driver.hasElementByAccessibilityId("card_proc_0"); 
    
        if (cardProcZeroVerify === false || undefined || null) { 
            console.warn('Adicionando novo procedimento');
            let adicionarProc = await driver.waitForElementByAccessibilityId("button_addProcedimento"); 
            await adicionarProc.click();
        }
    
        else {
            console.warn('Abrindo um procedimento já existente');
            let cardProcZero = await driver.waitForElementByAccessibilityId("card_proc_0"); 
            await cardProcZero.click();
        }
    });