acuminous / yadda

A BDD javascript library
412 stars 73 forks source link

Bug: the [] empty array literal replaced with empy string when an examples table is used. Escaping examples table placeholders? #271

Closed lolmaus closed 2 years ago

lolmaus commented 2 years ago

Hi!

I have this step in the Backround section and it works as expected:

  And the app has this state:
    ---
    {
      "productSeries": "1",
      "elements": []
    }
    ---

When I add an examples table to one of scenarios, the above step crashes with the following error:

Promise rejected during "Scenario: All filters are used, selecting 1st filter": Invalid JSON:
{
  "productSeries": "1",
  "elements": 
}

Apparently, the empty array literal has been treated as an examples table placeholder and replaced with an empty string.

Questions:

  1. Is this a bug? Shouldn't Yadda ignore non-matching placeholders?
  2. How do I escape an examples table placesholder, so that I can use literal [] or [Matching Placeholder] in a step? Something like

    Then element [Element] should have text "\[Element\]"
    
    Where:
      | Element                  |
      | .my-element:nth-child(2) |

CC @simonihmig

cressie176 commented 2 years ago

Hi @lolmaus,

Is this a bug? Shouldn't Yadda ignore non-matching placeholders?

I'm struggling to reproduce. You can find the code that substitutes placeholders with values here. It builds the example placeholder name into a regular expression, e.g. \[\s*Element\s*\]/g and will replace all matches with the example value retrieved from the example table. If the regular expression doesn't match, then the placeholder shouldn't be substituted.

Can you post your full feature/scenario please so I can debug further.

How do I escape an examples table placesholder

You can't, but you can use specify custom placeholder delimiters via the leftPlaceholderChar and rightPlaceholderChar options. If you're using either the FeatureParser or FeatureFileParser directly, you can do one of the following

const featureParser = new FeatureParser({ leftPlaceholderChar: '<', rightPlaceholderChar: '>' });
const featureFileParser = new FeatureFileParser({ leftPlaceholderChar: '<', rightPlaceholderChar: '>' });

However if you're using the mocha plugin, these options will sadly be dropped. Instead you'll need to instantiate the parser manually, and pass it to the plugin when you call init.

const parser = new FeatureFileParser({ leftPlaceholderChar: '<', rightPlaceholderChar: '>' });
Yadda.plugins.mocha.StepLevelPlugin.init({ parser });
cressie176 commented 2 years ago

I made a minor change to simplify customisation with the mocha plugin. As of 2.2.0 the options are passed straight through from the plugin to the FeatureParser so you can simply do

Yadda.plugins.mocha.StepLevelPlugin.init({ leftPlaceholderChar: '<', rightPlaceholderChar: '>' });
cressie176 commented 2 years ago

Any update @lolmaus?