Add world and context proxies for use in arrow functions #2402
+309
−20
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.
🤔 What's changed?
This PR adds two new public exports:
world
which you can reference from a step definition or hook to get the Worldcontext
which you can reference from aBeforeAll
orAfterAll
hook to get the context containing the world parametersI've added this to the docs, but assuming this is successful with early adopters I will raise a follow up PR to switch all our code examples over to use it too, and mark it stable (currently it's beta).
⚡️ What's your motivation?
This provides a solution for the long-standing conflict between wanting to access the "World" instance which has always been bound to
this
, and wanting to use modern JavaScript syntax with arrow functions that don't honour such a binding.You can now write step definitions with arrow functions and use the World like this:
Fixes #2004.
🏷️ What kind of change is this?
♻️ Anything particular you want feedback on?
The exported
world
object is aProxy
which dynamically forwards to the World for the currently-executing test case, which in turn is provided by a Node.jsAsyncLocalStorage
which works very similarly to React context in that we can have a piece of state scoped to the running of some code, and reachable from within that code without passing directly.There are a couple of things to bear in mind with this approach:
AsyncLocalStorage
here is behind a generic facade and does not leak externally.📋 Checklist:
This text was originally generated from a template, then edited by hand. You can modify the template here.