eugene-sea / karma-cucumber-js

Karma adapter for running Cucumber.js features
10 stars 4 forks source link

cucumber-karma-listener.ts:60 Uncaught TypeError: Cannot read property 'stack' of undefined #5

Closed chendachao closed 8 years ago

chendachao commented 8 years ago

Let's assume we have a.feature and b.feature, if both feature have the same step Given I go to home page Then it will throw an error.

I think this is because you put all steps together and execute them once.

eugene-sea commented 8 years ago

Thanks for bug report.

If Cucumber.js finds multiple step definitions for single step then it reports this as "ambiguous step definition" error. In karma-cucumber-js this case was not correctly handled. I have fixed that. Now if you have "ambiguous step definition", then karma-cucumber-js will report the issue like this:

Step is ambiguous: Given there is an ambiguous test step : 20
test_features/steps.js : 5
test_features/steps2.js : 4

In this example "steps.js" and "steps2.js" both have step definition for "Given there is an ambiguous test step" step. This is an error. It is not Cucumber.js specific, the same error will happen for other tools from Cucumber family, e.g. SpecFlow.

If you have the same step that should behave differently depending on feature then you could use tagged hooks and tag you features accordingly. In tagged hook you set some flag on World and step definition should recognize this flag and change its behavior.

chendachao commented 8 years ago

Thanks so much, I think it works better now.

But what if we don't concatenate all the feature together, but run them separately.

Think about it: register test_features/steps.js and run it, then use another cucumber instance register test_features/steps2.js and execute it.

I think if you can fix it in that way, will make this plugin more great.

Thank you again for your kindness support.

eugene-sea commented 8 years ago

You could use multiple karma.conf.js and run them separately if you need it. However I do not recommend this. Recommended way is using of tagged hooks.

chendachao commented 8 years ago

I think you didn't get my point, here you can see, the method run all features at once, which means all the features share the same World context, but the recommended way is one feature has one World, it's better for us to run features one by one.

eugene-sea commented 8 years ago

Yes, I still do not understand the issue. karma-cucumber-js is just a plugin for Karma for Cucumber.js. Features are executed the same way as in Cucumber.js.

Nor karma-cucumber-js nor Cucumber.js force you to use the same World for each feature. For each scenario execution new instance of World is created. If you need to set World for particular feature use code like this:

__adapter__.addStepDefinitions(function (scenario) {
    scenario.World = WorldConstructor; // Constructor function for your World

    scenario.Given(/^there is a test step$/, function () { });
    ...
});

If you find any discrepancy in behavior between Cucumber.js and karma-cucumber-js then tell me about it and I will fix it. As long as behavior of Cucumber.js and karma-cucumber-js are same then there is nothing to fix. My strategy is that tests for front-end and back-end should work the same. I would not recommend to do another way.

chendachao commented 8 years ago

Well, to make backend and frontend work the same is a great idea.

To make it clear, please take a look at this demo;

Currently karma-cumber-js register all steps and then execute, but I think it's better for us to register steps in a.feature and then execute them, and then register steps in b.feature and then run them.

Thus we don't worry ambiguous steps between different features.

eugene-sea commented 8 years ago

How long have you been working with tools from Cucumber family? My impression is that it is a new tool for you. Am I wrong?

One should not organize step definitions by features. Steps definitions should be organized by entity type. Feature-coupled step definitions is an anti-pattern.

It is completely normal for single feature to reference multiple step definitions files. This is how Cucumber.js is supposed to work. karma-cucumber-js just follows it. Multiple step definitions for a single step is not supported deliberately. Please, do not reinvent the wheel. Just use best practices which were developed by years of BDD expirience. Wrong use of tools is harmful.

chendachao commented 8 years ago

Yes, you're right, I tried to work with Cucumber month ago, don't know much about it.

I think I got your point, will have a try.

Thank you so much.

eugene-sea commented 8 years ago

@chendachao you have helped me to fix "Cannot read property 'stack' of undefined" bug, so thank you to.