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

fix(allure-cypress): missing tests and fixtures when an error occurs in a hook #1123

Merged
merged 13 commits into from
Aug 29, 2024

Conversation

delatrie
Copy link
Collaborator

@delatrie delatrie commented Aug 28, 2024

The changes

Reporting of failed hooks

If a hook fails, Mocha won't emit the hook end event. That caused the corresponding fixture to hang in the state until the process finishes instead of being written on disk.

Now, we're detecting those cases, and issuing the cypress_hook_end message in the Mocha's fail event if it corresponds to a hook error.

Note

Spec-level after hooks are handled differently. See below.

Reporting of spec-level failed after hooks

In beta 10 we support spec-level after hooks by patching the after function to introduce two hooks instead of one. The first hook is the original user-defined one, while the second, the Allure-specific one, is responsible for flushing the messages of the original hook to Node.js.

That works just fine until the user-defined hook fails. In that case, Mocha skips its Allure-specific counterpart, which essentially precludes the hook from being reported.

With this PR, the mechanism is changed. Now, instead of inserting extra hooks, we wrap the original hook implementation function. The wrapper calls the original hook implementation, then:

  • if the hook fails, it reports all the messages to complete the spec (remember, we're talking about a spec-level after hook; if it fails, other such hooks will be skipped making the failed hook the last piece of user-defined code being executed and the last place capable of calling a Cypress task).
  • if the hook succeeds, it completes the spec only if it was the last spec-level after hook in the spec file.

All three types of functions are supported:

  • Synchronous: after(() => { /* ... */ });
  • Promise-based: after(async () => { /* ... */ });
  • Callback-based:after((done) => { /* the code must eventually call done */ });

That technique also works better with cypress open. Previously we tried to complete the spec in Mocha's end event, which, TBH, never worked in the first place.

With cypress run we still rely on Cypress'es after:spec event because it allows us to attach the video.

Reporting of tests skipped because of a failed hook

If a hook fails, Mocha skips the remaining tests from the suite where the hook has been defined. This PR includes them (except those not in the test plan, if any) in the report with the status that depends on the failed hook type:

  • broken for before hooks.
  • skipped for beforeEach and afterEach hooks.

Note

There are no skipped tests in case of a failed after hook.

Fixes #1072

Open issues

There is a problem with failed after-fixture visibility. See #1122 for more details.

Other minor changes

  • Add language specs to examples embedded into JS doc comments
  • Some unused code was removed

Checklist

@delatrie delatrie added the type:bug Something isn't working label Aug 28, 2024
@delatrie delatrie force-pushed the issue1072-cypress-failed-hooks branch from c507b1b to 016c4f5 Compare August 28, 2024 04:40
@delatrie delatrie force-pushed the issue1072-cypress-failed-hooks branch from 016c4f5 to b6a48b6 Compare August 28, 2024 05:15
@delatrie delatrie marked this pull request as ready for review August 28, 2024 05:16
@baev baev requested a review from epszaw August 28, 2024 10:25
@baev baev merged commit 0c4d92a into main Aug 29, 2024
10 checks passed
@baev baev deleted the issue1072-cypress-failed-hooks branch August 29, 2024 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

allure-cypress test failures in before & beforeEach blocks are not reported
2 participants