Allow to skip tests asynchronously in jest-circus, closes #8604 #9944
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a proof of concept for asynchronously skipping tests as discussed in #8604. If this functionality will be considered as an addition, I will write tests and documentation.
TODO:
Summary
As described in #8604 and #7245 when using jest for e2e tests with e.g. puppeteer or detox, there are many valid use cases for wanting to abort tests programmatically based on previous test results. To enable doing that asynchronously, the API needs to add a skip function that is available during test execution and skips the test when called. Adding this to the API would also allow to easily implement various versions of bailing (see #6527) or stepped tests (see #8387).
This PR adds a
skip: () => Promise<void>
function to thetestContext
in jest-circus, so it can be called asreturn this.skip()
in the testfn
to skip the currently running test. Alternatively the skip function could be passed to the testfn
as a second argument, similar to howdone
is passed (this might be the more modern approach, as it would support arrow functions).The main point is to demonstrate that the implementation of this in jest-circus is pretty trivial and adds minimal complexity as all that skip needs to do is to dispatch the
test_skip
event and prevent the dispatch oftest_done
.Alternatively a new
canceled
state could be added to tests and atest_cancel
event could be fired, but I would advise against that, as it will increase the API surface even more.TODO:
Test plan
TODO: