-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
New Feature: shared context in beforeAll and afterAll hooks #1770
New Feature: shared context in beforeAll and afterAll hooks #1770
Conversation
This is a draft failing scenario to try and describe what @charlierudolph and @davidjgoss are describing in cucumber#1393 Feedback welcome. Co-authored-by: Matt Wynne <[email protected]>
The scenario we have so far doesn't address the issue around immutability. I suggest we add another scenario showing that even if the first scenario mutates the value that was set in the |
@BlueMona thanks for getting this going, and great idea to start with the scenario. I think my only suggestion would be for the stuff that we set in
Then it's like a sibling of @mattwynne seems a good direction re immutability. |
Co-authored-by: Matt Wynne <[email protected]>
Co-authored-by: Matt Wynne <[email protected]>
Co-authored-by: Matt Wynne <[email protected]>
Co-authored-by: Matt Wynne <[email protected]>
Co-authored-by: Matt Wynne <[email protected]>
The scenario is now passing, but there are a few questions / things left to do.
|
Thanks for the update @BlueMona, this is looking good. To your points:
It's kinda the same need as
I definitely agree, and I'd even go as far as giving each
Good question, I'll get back to you! We run the test run hooks once for each worker which makes things...interesting. Testing of parallel stuff is generally in the feature tests.
Yep would be good to test this. Hopefully the cloning idea above would have the desired effect of (enough) immutability. Marking it |
@davidjgoss thanks for the quick feedback! I'm wondering if we could achieve the immutability by copying the i.e. something like
WDYT? |
@mattwynne yeah that's basically what I was suggesting, although we'd want a deep clone since doing a spread just gives you a shallow clone. |
@BlueMona I've extracted a function to run the One slightly tricky part of that merge will be sharing the |
Hello, Is it plan to merge this great feature soon ? :) Thank you |
….com:BlueMona/cucumber-js into 1393-add-world-object-reference-in-beforeall
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking fantastic, thanks for taking this on Mona. And sorry for the delayed review.
One thing I think we're missing is the correct typing of the BeforeAll
and AfterAll
functions so people can access this.testRunContext
while using TypeScript without a compilation error.
If you go to test-d/hooks.ts
and add:
// should allow test run context via this arg in test run hooks
BeforeAll(async function () {
this.testRunContext.foo = 'bar'
})
AfterAll(async function () {
this.testRunContext
})
Then that will currently fail if you run npm run types-test
. I think the place to fix it is in src/support_code_library_builder/types.ts
. We have explicit types for TestCaseHookFunction
etc there and so it would be good to follow that pattern - although we don't need the complexity with the generics for world there, so it should be easier, something like:
export type TestRunHookFunction = (
this: { testRunContext: TestRunContext }
) => any | Promise<any>
We'll also need to update some docs:
- https://github.com/cucumber/cucumber-js/blob/main/docs/support_files/api_reference.md#afteralloptions-fn - basic API doc for the functions
- https://github.com/cucumber/cucumber-js/blob/main/docs/support_files/hooks.md#beforeall--afterall - explain how to set test run context
- https://github.com/cucumber/cucumber-js/blob/main/docs/support_files/world.md - explain you can access test run context read only
I'm very happy to help out with any of this as required, just let me know. Thanks again!
this.attach = attach | ||
this.log = log | ||
this.parameters = parameters | ||
this.testRunContext = cloneDeep(testRunContext) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is part of the default World
class. When users write a custom world they typically extend this but not always. So I think it would be better to do the cloneDeep
outside of the world, probably in test_case_runner.ts::resetTestProgressData
, and then we know the world will always receive a clone.
src/runtime/parallel/worker.ts
Outdated
@@ -37,6 +37,7 @@ export default class Worker { | |||
private supportCodeLibrary: ISupportCodeLibrary | |||
private worldParameters: any | |||
private runTestRunHooks: RunsTestRunHooks | |||
private testRunContext = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine to initialize here but we should type it as TestRunContext
like in the non-parallel runtime.
src/runtime/parallel/worker.ts
Outdated
@@ -139,6 +141,7 @@ export default class Worker { | |||
skip, | |||
supportCodeLibrary: this.supportCodeLibrary, | |||
worldParameters: this.worldParameters, | |||
testRunContext: {}, // TODO: decide what this needs to be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the value here should be this.testRunContext
.
(At the moment, for parallel each worker runs all the BeforeAll
hooks in isolation. In the future, we'll allow doing them at the coordinator level as well and that will make this more complex - but we don't have to worry about that yet!)
When I run cucumber-js | ||
Then it passes | ||
|
||
Rule: testRunContext can't leak between scenarios |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should test the parallel runtime in another scenario here. We'd lose the guaranteed order so it gets less simple, but we could do something like 5 scenarios of
Scenario:
Given the value of foo is unchanged
When I change foo to {int}
And run with --parallel 2
.
So in other words every scenario checks it has an unmodified context, and then tries to modify its current one. I think that would be enough to give us confidence.
const {AfterAll, BeforeAll, Given} = require('@cucumber/cucumber') | ||
const {expect} = require('chai') | ||
|
||
BeforeAll(function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a second BeforeAll
like:
BeforeAll(function() {
this.testRunContext.otherVar = {bar: 2}
})
And then checking it's all there in the AfterAll
hook, to prove that we can incrementally build up the context across multiple BeforeAll
hooks. I've no doubt this will work without any changes, but just to document it.
Just curious if we have any updates or plan for this PR? |
@BlueMona ping me if you want to pair on finishing this off. I'm here to help if you want it. |
Let’s do it this week then!
…On Fri, Sep 1, 2023 at 7:12 PM Matt Wynne ***@***.***> wrote:
@BlueMona <https://github.com/BlueMona> ping me if you want to pair on
finishing this off. I'm here to help if you want it.
—
Reply to this email directly, view it on GitHub
<#1770 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALHLMFA6QMHT7NFI4FZ5RDXYJTVFANCNFSM5CKQKM3Q>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@BlueMona great! Can you use https://calendly.com/mattwynne to book a slot? |
any news on this PR? |
Just noting here the idea raised on the issue of, rather than adding a new
I think this would solve all the use cases nicely and this PR could be adapted to it without much work - @BlueMona @mattwynne I'm happy to pick this up if you're time-constrained, since I'm working in this area already. Also somewhat related is #2356 which I'm looking at today. |
David,
Yes, please do.
…On Thu, Dec 21, 2023 at 5:15 AM David Goss ***@***.***> wrote:
Just noting here the idea raised on the issue of, rather than adding a new
testRunContext concept, just passing in the world parameters and allowing
a BeforeAll to return a fresh world parameters if it wants to. So like:
BeforeAll(async function() {
this.token = await authenticate(this.parameters.url, this.parameters.clientId, this.parameters.clientSecret)
return {
...this.parameters,
token
}
})
I think this would solve all the use cases nicely and this PR could be
adapted to it without much work - @BlueMona <https://github.com/BlueMona>
@mattwynne <https://github.com/mattwynne> I'm happy to pick this up if
you're time-constrained, since I'm working in this area already.
Also somewhat related is #2356
<#2356> which I'm looking
at today.
—
Reply to this email directly, view it on GitHub
<#1770 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALHLMDE5Y34FGZHXLOQ6PTYKQD3NAVCNFSM5CKQKM32U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBWGU4TSNRQGM4Q>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
…ct-reference-in-beforeall
When we started this change, this new global wasn't available in Node.js yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some conflict untangling, this fitted together nicely with some other recent changes.
Many thanks @BlueMona for doing most of this!
Hi @BlueMona, Thanks for your making your first contribution to Cucumber, and welcome to the Cucumber committers team! You can now push directly to this repo and all other repos under the cucumber organization! 🍾 In return for this generous offer we hope you will:
On behalf of the Cucumber core team, |
This one's a great holiday gift. Thanks team 🖤. Happy holidays! |
🎉 nice one @BlueMona! (and @davidjgoss 😄 ) |
Description
This is a draft failing scenario to try and describe what @charlierudolph and
@davidjgoss are describing in #1393
I'd like to work on a fix but thought it would be best to get feedback on the scenario first.
Motivation & context
Fixes #1393
Type of change
Checklist: