jest-cucumber
jest-cucumber copied to clipboard
Support done callback in addition to promises
Given, When, Then can't return promises or take a done callback.
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?
Hi Bencompton,
Sorry - yes - you're right it was my misunderstanding. Thanks for your reply. I'll close the issue.
Nic.
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.
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.
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.
Being consistent with Jest seems reasonable and is certainly worth consideration. Let's leave this open, then.
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 instantiaterocket
, 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, 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.
any news about this feature ?
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.
More detailed example in the ticket https://github.com/bencompton/jest-cucumber/issues/159
@bencompton