Custom messages output in Cucumber.js reports
Cucumber.js is definitely one of the most popular libraries for e2e testing of frontend applications.
But as popular as it is, its documentation is unbearable.
What’s the matter? Cucumber creates reports after running tests, and the reports can be formatted by third-party plugins. For this purpose, the message scheme is typified in a separate repository https://github.com/cucumber/messages. You can create your own plugin that will show a convenient UI with test results.
Also, Cucumber can be run programmatically directly from JS, not from the command line. In this case, messages will fall into the callback call.
const { runConfiguration } = await loadConfiguration();
const { success } = await runCucumber( runConfiguration, undefined, (message) => { console.log("onMessage:", message); });The question is, how do you send your own messages to this message thread?
Ideally, I would like to send a message in step definition and have it appear in all reports and in the callback.
And by default there are only messages defined by the scheme.
There is no way to extend the message schema in Cucumber (at least I haven’t found any), but you can add an attachment to the message with the completed step.
It turns out that the Default World constructor passes the attach method, which allows you to send attachments directly from steps. This method can be attached to your Custom World class and used inside step definitions.
class CustomWorld extends World { constructor(options) { super(options) this.attach = options.attach }}And then these attachments will appear in any native Cucumber reports.
As always, this is not in the docs on the main site, but hidden somewhere in the depths of their repository https://github.com/cucumber/cucumber-js/blob/main/docs/support_files/attachments.md