Skip to content

Commit 23ff64a

Browse files
authored
fix: use explicit status field to determine (not) running status (#2386)
1 parent 5a4f129 commit 23ff64a

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber.
99

1010
## [Unreleased]
11+
### Changed
12+
- Use explicit status to check if Cucumber is running when registering support code ([#2386](https://github.com/cucumber/cucumber-js/pull/2386))
1113

1214
## [10.3.1] - 2024-01-16
1315
### Changed

Diff for: features/invalid_installation.feature

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Feature: Invalid installations
1919
Then it fails
2020
And the error output contains the text:
2121
"""
22-
You're calling functions (e.g. "When") on an instance of Cucumber that isn't running.
23-
This means you have an invalid installation, mostly likely due to:
22+
You're calling functions (e.g. "When") on an instance of Cucumber that isn't running (status: PENDING).
23+
This means you may have an invalid installation, potentially due to:
2424
- Cucumber being installed globally
2525
- A project structure where your support code is depending on a different instance of Cucumber
2626
Either way, you'll need to address this in order for Cucumber to work.

Diff for: src/support_code_library_builder/index.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import TestStepHookDefinition from '../models/test_step_hook_definition'
1010
import TestRunHookDefinition from '../models/test_run_hook_definition'
1111
import StepDefinition from '../models/step_definition'
1212
import { formatLocation } from '../formatter/helpers'
13-
import { doesHaveValue, doesNotHaveValue } from '../value_checker'
13+
import { doesHaveValue } from '../value_checker'
1414
import { ICanonicalSupportCodeIds } from '../runtime/parallel/command_types'
1515
import { GherkinStepKeyword } from '../models/gherkin_step_keyword'
1616
import validateArguments from './validate_arguments'
@@ -65,9 +65,10 @@ interface ITestRunHookDefinitionConfig {
6565
uri: string
6666
}
6767

68+
type LibraryStatus = 'PENDING' | 'OPEN' | 'FINALIZED'
69+
6870
export class SupportCodeLibraryBuilder {
6971
public readonly methods: IDefineSupportCodeMethods
70-
7172
private originalCoordinates: ISupportCodeCoordinates
7273
private afterTestCaseHookDefinitionConfigs: ITestCaseHookDefinitionConfig[]
7374
private afterTestRunHookDefinitionConfigs: ITestRunHookDefinitionConfig[]
@@ -83,6 +84,7 @@ export class SupportCodeLibraryBuilder {
8384
private stepDefinitionConfigs: IStepDefinitionConfig[]
8485
private World: any
8586
private parallelCanAssign: ParallelAssignmentValidator
87+
private status: LibraryStatus = 'PENDING'
8688

8789
constructor() {
8890
const methods: IDefineSupportCodeMethods = {
@@ -123,11 +125,11 @@ export class SupportCodeLibraryBuilder {
123125
When: this.defineStep('When', () => this.stepDefinitionConfigs),
124126
}
125127
const checkInstall = (method: string) => {
126-
if (doesNotHaveValue(this.cwd)) {
128+
if (this.status === 'PENDING') {
127129
throw new Error(
128130
`
129-
You're calling functions (e.g. "${method}") on an instance of Cucumber that isn't running.
130-
This means you have an invalid installation, mostly likely due to:
131+
You're calling functions (e.g. "${method}") on an instance of Cucumber that isn't running (status: ${this.status}).
132+
This means you may have an invalid installation, potentially due to:
131133
- Cucumber being installed globally
132134
- A project structure where your support code is depending on a different instance of Cucumber
133135
Either way, you'll need to address this in order for Cucumber to work.
@@ -414,6 +416,7 @@ export class SupportCodeLibraryBuilder {
414416
}
415417

416418
finalize(canonicalIds?: ICanonicalSupportCodeIds): SupportCodeLibrary {
419+
this.status = 'FINALIZED'
417420
const stepDefinitionsResult = this.buildStepDefinitions(
418421
canonicalIds?.stepDefinitionIds
419422
)
@@ -472,6 +475,7 @@ export class SupportCodeLibraryBuilder {
472475
this.stepDefinitionConfigs = []
473476
this.parallelCanAssign = () => true
474477
this.World = World
478+
this.status = 'OPEN'
475479
}
476480
}
477481

Diff for: src/support_code_library_builder/index_spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fail } from 'node:assert'
12
import { describe, it } from 'mocha'
23
import { expect } from 'chai'
34
import sinon from 'sinon'
@@ -9,6 +10,18 @@ import supportCodeLibraryBuilder from './'
910
const { uuid } = IdGenerator
1011

1112
describe('supportCodeLibraryBuilder', () => {
13+
it('should throw if not been reset yet', () => {
14+
try {
15+
// @ts-expect-error mutating private member
16+
supportCodeLibraryBuilder.status = 'PENDING'
17+
supportCodeLibraryBuilder.methods.Given('some context', () => {})
18+
fail()
19+
} catch (e) {
20+
expect(e.message).to.contain('calling functions (e.g. "Given")')
21+
expect(e.message).to.contain('status: PENDING')
22+
}
23+
})
24+
1225
describe('no support code fns', () => {
1326
it('returns the default options', function () {
1427
// Arrange

0 commit comments

Comments
 (0)