Search code examples
typescriptdatatablescucumbercypress

Cypress Typescript: How do I get a certain value from dataTable?


I have this data table, with a list of cause (state) and effect (message) that I want to check in the scenario.

Given user inputs "<state>" and sees "<message>" message
        | state       | message      |
        | Deactivated | error        |
        | Taken       | error        |
        | Unused      | confirmation |

I tried to use this script but it's just executing all below state instead of also executing message actions.

Given(‘user inputs {string} and sees {string} message’, (dataTable: DataTable) => {

dataTable.hashes().forEach(elem =>{
        if(elem.state == 'Deactivated') {
            <DeactivatedAction>
        } else if(elem.state == 'Taken') {
            <TakenAction>
        } else if(elem.state == 'Unused') {
            <UnusedAction>
        } else {
            throw new Error("No state defined")
        } 
    })

dataTable.hashes().forEach(elem =>{
        if(elem.message == 'error') {
            <errorAction>
        } else if(elem.message == 'confirmation') {
            <confirmationAction>
        } else {
            throw new Error("No state defined")
        } 
    })
}

The same thing happens when I change .forEach() to .map(). :/


Solution

  • If I understand correctly, you want the 2nd loop to access "message", if so it looks more like a typo - you reference elem.state instead of elem.message.

    dataTable.hashes().forEach(elem => {
      if (elem.message === 'error') {
        <errorAction>
      } else if (elem.message === 'confirmation') {
        <confirmationAction>
      } else {
        throw new Error("No state defined")
      } 
    })