Skip to content
Advertisement

Write assertion subject to whether key has value

I’m trying to come up with a method that would allow me to create a it block that confirms whether my configuration file has a specific attribute. The object has in it:

exports.config = {
    services: [
        ['sauce', {
            sauceConnect: true,
        }]
    ],
}

I would like to have an it block that would confirm whether this value is true and if it isn’t, then it should fail.

I have tried a couple approaches like if (sauceConnect in services) etc but it isn’t an approach that is working. The test and the configuration file are in separated documents and for the life of me I can’t work out a good enough test.

I’d appreciate any help or answers here.

Advertisement

Answer

On the assumption that this is how you want your config to look like, you’ll have to loop through it and find the match:

const exportedConfig = {
    services: [
        ['sauce', {
            sauceConnect: true,
        }],
        ['key', {
          data: "value",
        }]
    ],
}

// [1] refers to the 2nd element in the array, aka the value
// some looks for ANY match and if a match occurs it returns true.
console.log(exportedConfig.services.some(element => element[1].sauceConnect == true ))

Though, a recommendation would be to format your config as so:

const exportedConfig = {
    services: {
        sauce: {
            sauceConnect: true,
        },
        key: {
          data: "value",
        }
    },
}
``
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement