Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the parse error into a variable #1404

Closed
manisshaa opened this issue Aug 11, 2020 · 2 comments · Fixed by #1951
Closed

Getting the parse error into a variable #1404

manisshaa opened this issue Aug 11, 2020 · 2 comments · Fixed by #1951
Assignees
Labels
⚡ enhancement Request for new functionality

Comments

@manisshaa
Copy link

If the feature file is not in proper gherkin format, Cucumber shows a parse error on console and aborts the tests. Following is the code that does that (this is a snippet from cucumber > lib > cli > run.js

function exitWithError(error) {
console.error(_verror.default.fullStack(error)); // eslint-disable-line no-console
process.exit(1);
}

I want to capture this parse error into a variable or a file. As a work around, I have added a line to function exitWithError to write the parse error to a file for me to later read the error for reporting purposes. But this solution requires my own version of cucumber package and then use the same. But the problem is that my changes will be overwritten once cucumber package is updated.

function exitWithError(error) {
fs.writeFileSync("./Parse_Error", error);
console.error(_verror.default.fullStack(error)); // eslint-disable-line no-console
process.exit(1);
}

Please suggest.

Note: Please know that I need to handle this scenario as we don't manually create cucumber feature files but import them from Jira cucumber tests. These tests are created by stakeholders who may make some minor mistakes in these cucumber tests which may lead to the parse error. I need to capture the parse error (in a file or variable), so that I can report it back to the jira ticket as a comment or something

@davidjgoss
Copy link
Contributor

davidjgoss commented Nov 29, 2020

The messages protocol includes a ParseError message type, which we do emit, I think we just need to refactor a bit so that the message formatter has a chance to finish before we exit. Will try and get a PR up for this soon.

@davidjgoss davidjgoss self-assigned this Jan 27, 2021
@aslakhellesoy aslakhellesoy added the ⚡ enhancement Request for new functionality label Feb 2, 2021
@davidjgoss davidjgoss removed their assignment Sep 5, 2021
@davidjgoss davidjgoss self-assigned this Mar 4, 2022
@davidjgoss
Copy link
Contributor

This change will be available in the forthcoming 8.0.0 release.

The simplest way to capture the parse errors will be to direct the message formatter output to a file, like:

npx cucumber-js --format message:cucumber-messages.ndjson

You'll then be able to see parseError messages in that file.

For more programmatic control, you could implement a custom formatter which listens to the emitted messages and then acts on the parseError ones, something like:

import {Formatter} from '@cucumber/cucumber`

class MyFormatter extends Formatter {
  constructor(options) {
    super(options)
    options.eventBroadcaster.on('envelope', (envelope) => {
      if (envelope.parseError) {
        // do something with parse error
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡ enhancement Request for new functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants