Skip to content

Commit

Permalink
fix: use explicit status field to determine (not) running status (#2386)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgoss committed Mar 27, 2024
1 parent 5a4f129 commit 23ff64a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber.

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

## [10.3.1] - 2024-01-16
### Changed
Expand Down
4 changes: 2 additions & 2 deletions features/invalid_installation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Feature: Invalid installations
Then it fails
And the error output contains the text:
"""
You're calling functions (e.g. "When") on an instance of Cucumber that isn't running.
This means you have an invalid installation, mostly likely due to:
You're calling functions (e.g. "When") on an instance of Cucumber that isn't running (status: PENDING).
This means you may have an invalid installation, potentially due to:
- Cucumber being installed globally
- A project structure where your support code is depending on a different instance of Cucumber
Either way, you'll need to address this in order for Cucumber to work.
Expand Down
14 changes: 9 additions & 5 deletions src/support_code_library_builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import TestStepHookDefinition from '../models/test_step_hook_definition'
import TestRunHookDefinition from '../models/test_run_hook_definition'
import StepDefinition from '../models/step_definition'
import { formatLocation } from '../formatter/helpers'
import { doesHaveValue, doesNotHaveValue } from '../value_checker'
import { doesHaveValue } from '../value_checker'
import { ICanonicalSupportCodeIds } from '../runtime/parallel/command_types'
import { GherkinStepKeyword } from '../models/gherkin_step_keyword'
import validateArguments from './validate_arguments'
Expand Down Expand Up @@ -65,9 +65,10 @@ interface ITestRunHookDefinitionConfig {
uri: string
}

type LibraryStatus = 'PENDING' | 'OPEN' | 'FINALIZED'

export class SupportCodeLibraryBuilder {
public readonly methods: IDefineSupportCodeMethods

private originalCoordinates: ISupportCodeCoordinates
private afterTestCaseHookDefinitionConfigs: ITestCaseHookDefinitionConfig[]
private afterTestRunHookDefinitionConfigs: ITestRunHookDefinitionConfig[]
Expand All @@ -83,6 +84,7 @@ export class SupportCodeLibraryBuilder {
private stepDefinitionConfigs: IStepDefinitionConfig[]
private World: any
private parallelCanAssign: ParallelAssignmentValidator
private status: LibraryStatus = 'PENDING'

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

finalize(canonicalIds?: ICanonicalSupportCodeIds): SupportCodeLibrary {
this.status = 'FINALIZED'
const stepDefinitionsResult = this.buildStepDefinitions(
canonicalIds?.stepDefinitionIds
)
Expand Down Expand Up @@ -472,6 +475,7 @@ export class SupportCodeLibraryBuilder {
this.stepDefinitionConfigs = []
this.parallelCanAssign = () => true
this.World = World
this.status = 'OPEN'
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/support_code_library_builder/index_spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fail } from 'node:assert'
import { describe, it } from 'mocha'
import { expect } from 'chai'
import sinon from 'sinon'
Expand All @@ -9,6 +10,18 @@ import supportCodeLibraryBuilder from './'
const { uuid } = IdGenerator

describe('supportCodeLibraryBuilder', () => {
it('should throw if not been reset yet', () => {
try {
// @ts-expect-error mutating private member
supportCodeLibraryBuilder.status = 'PENDING'
supportCodeLibraryBuilder.methods.Given('some context', () => {})
fail()
} catch (e) {
expect(e.message).to.contain('calling functions (e.g. "Given")')
expect(e.message).to.contain('status: PENDING')
}
})

describe('no support code fns', () => {
it('returns the default options', function () {
// Arrange
Expand Down

0 comments on commit 23ff64a

Please sign in to comment.