Jest Cucumber supports returning promises or using async/await from steps that have asynchronous operations:
defineFeature(feature, test => {
test('Adding a todo', ({ given, when, then }) => {
...
when('I save my changes', async () => {
await todo.saveChanges();
console.log('Changes saved!');
});
...
});
});
defineFeature(feature, test => {
test('Adding a todo', ({ given, when, then }) => {
...
when('I save my changes', () => {
return todo
.saveChanges()
.then(() => console.log('Changes saved'));
});
...
});
});
Jest Cucumber also supports callbacks for asynchronous tasks. If an additional callback parameter is defined on the step function, it will wait for that callback to be called to continue with the next step:
defineFeature(feature, test => {
test('Adding a todo', ({ given, when, then }) => {
...
when('I save my changes', done => {
todo.saveChanges(() => {
console.log('Changes saved');
done(); // When the callback is called, the step will end
});
});
...
});
});
The callback should always be the last parameter:
defineFeature(feature, test => {
test('Adding a todo', ({ given, when, then }) => {
...
when(/I save my changes with name (\w+)/, (name, done) => {
todo.saveChanges(() => {
console.log(`Changes saved with name ${name}`);
done();
});
});
...
});
});
Like in jest, if an argument is passed to the callback function, it will fail the test:
defineFeature(feature, test => {
test('Adding a todo', ({ given, when, then }) => {
...
when('I save my changes', done => {
todo.saveChanges(error => {
if (error) {
done(error); // the test will fail if called with an argument
} else {
console.log(`Changes saved`);
done(); // the test will succeed if no argument is passed
}
});
});
...
});
});
You can also control how long Jest will wait for asynchronous operations before failing your test by passing a timeout (milliseconds) into your test:
defineFeature(feature, test => {
test('Adding a todo', ({ given, when, then }) => {
...
when('I save my changes', async () => {
await todo.saveChanges();
console.log('Changes saved!');
});
...
}, 1000);
});
In addition, you can set a default timeout for all of your asynchronous tests in your Jest configuration.