bencompton / jest-cucumber

Execute Gherkin scenarios in Jest
Apache License 2.0
655 stars 115 forks source link

Support done callback in addition to promises #16

Closed nicopolyptic closed 4 months ago

nicopolyptic commented 6 years ago

Given, When, Then can't return promises or take a done callback.

bencompton commented 6 years ago

Hi @nicopolyptic. Promises are supported, but callbacks are not. Here is an example that I just tried that works as expected:

        ...
        given('I am Elon Musk attempting to launch a rocket into space', () => {
            return new Promise((resolve) => {
                setTimeout(() => {
                    rocket = new Rocket();
                    resolve();
                }, 1000);
            });
        });
        ...

This test passes as it is written above. When I remove the call to resolve, the test times out and fails. When I remove the call to instantiate rocket, the test also fails. In addition, I use this library in my own project, which has a lot of complex asynchronous logic, and have never discovered any promise-related bugs in jest-cucumber.

Would you mind providing an example of some code with a promise that isn't working as expected for you?

nicopolyptic commented 6 years ago

Hi Bencompton,

Sorry - yes - you're right it was my misunderstanding. Thanks for your reply. I'll close the issue.

Nic.

bencompton commented 6 years ago

Thanks for bringing this to light, @nicopolyptic. I re-named your issue and am re-opening it because asynchronous use cases should really have documentation and examples.

robinelvin commented 5 years ago

Do/will steps support the done parameter?

https://jestjs.io/docs/en/asynchronous#callbacks

bencompton commented 5 years ago

A done callback is not currently supported, only promises (or async/await). Do you think a done callback is really needed? Currently, code that requires a callback can be accommodated by wrapping the code in a promise and using the Promise's resolve function as the callback.

robinelvin commented 5 years ago

No, I don't think it's needed but it would make it consistent with Jest. Wrapping the code in a Promise was exactly what I have been doing but it would be neater to use done() in some situations. This cropped up from converting existing tests that use done to jest-cucumber. So now I know I can continue using promises instead.

bencompton commented 5 years ago

Being consistent with Jest seems reasonable and is certainly worth consideration. Let's leave this open, then.

markj9 commented 5 years ago

Hi @nicopolyptic. Promises are supported, but callbacks are not. Here is an example that I just tried that works as expected:

        ...
        given('I am Elon Musk attempting to launch a rocket into space', () => {
            return new Promise((resolve) => {
                setTimeout(() => {
                    rocket = new Rocket();
                    resolve();
                }, 1000);
            });
        });
        ...

This test passes as it is written above. When I remove the call to resolve, the test times out and fails. When I remove the call to instantiate rocket, the test also fails. In addition, I use this library in my own project, which has a lot of complex asynchronous logic, and have never discovered any promise-related bugs in jest-cucumber.

Would you mind providing an example of some code with a promise that isn't working as expected for you?

Are you using the promise returned from this Given block in a When or Then block? I'm trying to assert state in a Then block that follows a When block that is calling an async function that I need to complete before I assert.

bencompton commented 5 years ago

@markj9, when a promise is returned from a step (or async/await is used), jest-cucumber will wait until the promise is resolved before continuing to the next step.

osman-mohamad commented 4 years ago

any news about this feature ?

nitinmagdumvista commented 2 years ago

Hello I am using jest-cucumber I have a async function in When block and based on the result of that in want to do asset in Then block how to stop executing Then block till the When block executes successfully.

nitinmagdumvista commented 2 years ago

More detailed example in the ticket https://github.com/bencompton/jest-cucumber/issues/159

nitinmagdumvista commented 2 years ago

@bencompton

github-actions[bot] commented 4 months ago

:tada: This issue has been resolved in version 4.4.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: