jest-cucumber icon indicating copy to clipboard operation
jest-cucumber copied to clipboard

Support done callback in addition to promises

Open nicopolyptic opened this issue 6 years ago • 13 comments

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

nicopolyptic avatar Jul 23 '18 19:07 nicopolyptic

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?

bencompton avatar Jul 24 '18 12:07 bencompton

Hi Bencompton,

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

Nic.

nicopolyptic avatar Jul 24 '18 13:07 nicopolyptic

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.

bencompton avatar Jul 24 '18 14:07 bencompton

Do/will steps support the done parameter?

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

robinelvin avatar Sep 26 '18 09:09 robinelvin

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.

bencompton avatar Sep 26 '18 17:09 bencompton

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.

robinelvin avatar Sep 27 '18 09:09 robinelvin

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

bencompton avatar Sep 27 '18 16:09 bencompton

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.

markj9 avatar Nov 30 '18 15:11 markj9

@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.

bencompton avatar Nov 30 '18 15:11 bencompton

any news about this feature ?

osman-mohamad avatar Jan 12 '20 14:01 osman-mohamad

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 avatar Aug 24 '22 09:08 nitinmagdumvista

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

nitinmagdumvista avatar Aug 24 '22 09:08 nitinmagdumvista

@bencompton

nitinmagdumvista avatar Aug 24 '22 09:08 nitinmagdumvista