From 18988fdf4c89bed1359db2e1a6bb7c4cf1b4de42 Mon Sep 17 00:00:00 2001 From: David Goss Date: Wed, 8 Jan 2025 23:34:17 +0000 Subject: [PATCH] Enable world proxy in parameter transformers (#2465) --- CHANGELOG.md | 3 +++ features/scope_proxies.feature | 23 ++++++++++++++++++++--- src/runtime/step_runner.ts | 10 ++++++---- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a83f6735d..4c2ef16ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber - `Hook` message now includes `type` ([#2463](https://github.com/cucumber/cucumber-js/pull/2463)) - `TestRunStarted` message now includes `id`; `TestCase` and `TestRunFinished` messages reference it in `testRunStartedId` ([#2463](https://github.com/cucumber/cucumber-js/pull/2463)) +### Fixed +- Enable world proxy in parameter transformers ([#2465](https://github.com/cucumber/cucumber-js/pull/2465)) + ## [11.1.1] - 2024-12-11 ### Fixed - Correctly report error in publish plugin ([#2454](https://github.com/cucumber/cucumber-js/pull/2454)) diff --git a/features/scope_proxies.feature b/features/scope_proxies.feature index 36dc7da43..1b5378e82 100644 --- a/features/scope_proxies.feature +++ b/features/scope_proxies.feature @@ -5,7 +5,7 @@ Feature: Scope proxies """ Feature: some feature Scenario: some scenario - Given a step + Given somebody called Lucy """ And a file named "features/support/world.js" with: """ @@ -28,11 +28,28 @@ Feature: Scope proxies Scenario: world and context can be used from appropriate scopes Given a file named "features/step_definitions/cucumber_steps.js" with: """ - const {BeforeAll,Given,BeforeStep,Before,world,context} = require('@cucumber/cucumber') + const {defineParameterType,BeforeAll,Given,BeforeStep,Before,world,context} = require('@cucumber/cucumber') const assert = require('node:assert/strict') + class Person { + #name; + + constructor(name) { + this.#name = name + } + } + + defineParameterType({ + name: 'person', + regexp: /\w+/, + transformer: name => { + assert(world.isWorld()) + return new Person(name); + } + }) + BeforeAll(() => assert.equal(context.parameters.a, 1)) - Given('a step', () => assert(world.isWorld())) + Given('somebody called {person}', (person) => assert(world.isWorld())) BeforeStep(() => assert(world.isWorld())) Before(() => assert(world.isWorld())) """ diff --git a/src/runtime/step_runner.ts b/src/runtime/step_runner.ts index 4de2f6c9b..428e03905 100644 --- a/src/runtime/step_runner.ts +++ b/src/runtime/step_runner.ts @@ -32,10 +32,12 @@ export async function run({ let error: any, result: any, invocationData: IGetInvocationDataResponse try { - invocationData = await stepDefinition.getInvocationParameters({ - hookParameter, - step, - world, + await runInTestCaseScope({ world }, async () => { + invocationData = await stepDefinition.getInvocationParameters({ + hookParameter, + step, + world, + }) }) } catch (err) { error = err