acuminous / yadda

A BDD javascript library
412 stars 73 forks source link

expand reusable definitions #222

Closed bsideup closed 7 years ago

bsideup commented 8 years ago

Hi!

steps executed with yadda.run([]] are rendered as a single step in the report. However, expanding them would be great so, in case of failure, user will be able to see which substep failed.

cressie176 commented 8 years ago

Are you running them with mocha? If so use the step level plugin instead of the scenario level plugin.

bsideup commented 8 years ago

@cressie176 I am, but it doesn't work :(

screen shot 2016-09-14 at 6 03 23 pm
cressie176 commented 8 years ago

Sorry, I see what you mean now. I'll take a look, but suspect this will be how mocha processes describe() and it() functions.

bsideup commented 8 years ago

@cressie176 cool, thanks :) Let me know about the progress :)

bsideup commented 8 years ago

@cressie176 figured out that if we flatten steps (i.e. expand steps) before container.steps call, reporting works fine, so it something with done call I guess

bsideup commented 8 years ago

Example (I hope it's fine that I use generators and other modern features, but if it'll not be clear enough I can rewrite it):

function *flatten(steps) {
    for (const step of steps) {
        const pattern = /^Sing (.*?) (.*?) bottles$/;

        const match = step.match(pattern);
        if (match) {
            const [ , num, color ] = match;
            yield *flatten([
                `Given ${num} ${color} bottles are standing on the wall`,
                `When 1 ${color} bottle accidentally falls`,
                `Then there are ${num - 1} ${color} bottles standing on the wall`
            ]);
        }
        else {
            yield step;
        }
    }
}

new Yadda.FeatureFileSearch('./tests/features/').each(file => {
    container.featureFile(file, feature => {
        container.scenarios(feature.scenarios, scenario => {
            container.steps([...flatten(scenario.steps)], (step, done) => {
                yadda.run(step, done);
            });
        });
    });
});
cressie176 commented 8 years ago

I think what's going on is as follows...

When you call container.featureFile, yadda parses the feature and calls

describe(feature.title, () => {

When you call container.scenarios, yadda iterates over the scenarios calling

describe(scenario.title, () => {

When you call container.steps, yadda iterates over the steps calling

it(step.text, () =>{

then runs the step. Written in pure mocha this would look like

const assert = require('assert')

describe('Mocha Asynchronous Example ', () => {
    describe('10 bottles are standing on a wall', () => {
        it('Given 10 green bottles are standing on the wall', () => {
            assert(false)
        })
    })
})

When I run the pure mocha version I get the same problem

  Mocha Asynchronous Example 
    10 bottles are standing on a wall
      1) Given 10 green bottles are standing on the wall

  0 passing (11ms)
  1 failing

  1) Mocha Asynchronous Example 10 bottles are standing on a wall Given 10 green bottles are standing on the wall:

      AssertionError: false == true
      + expected - actual

      -false
      +true

      at Context.it (test.js:28:13)

The only way I can think of resolving this is to write a custom mocha reporter. Not sure why you had different behaviour with flatten.

cressie176 commented 8 years ago

On a separate note, arrow functions don't work too well in step definitions, e.g.

library.when(/some step/, (cb) => {
  cb()
}

They're fine until you want to share something between steps. Unfortunately the way to do this with yadda is to define a shared context which Yadda binds the step function to. You have to access it with this, which of course has different behaviour with arrow functions.

bsideup commented 8 years ago

@cressie176 flatten works because it will expand the step into the substeps so Mocha will receive multiple it's.

cressie176 commented 8 years ago

Hi - sorry for the delay. I've tried your example see here, but the output is the same whether I flatten or not, so maybe I'm misunderstanding something?

bsideup commented 8 years ago

@cressie176 it's

steps([...flatten(scenario.steps)], (step, done) => {
  yadda.run(step, done);
});

otherwise flatten is not being called

cressie176 commented 8 years ago

Yep - I tried with and without, to generate the flatten.txt and normal.txt files. Just happened to commit the version without the call to flatten

bsideup commented 8 years ago

ah, ok. Weird. I'll prepare an example repo, so you can play with it :)

cressie176 commented 8 years ago

Any luck?

cressie176 commented 7 years ago

Closing due to inactivity. Let me know if you want it reopened.