Skip to content

Commit

Permalink
cli: warn when untested node version (#1959)
Browse files Browse the repository at this point in the history
* warn if node version not tested

* add note to workflows file

* update CHANGELOG.md
  • Loading branch information
davidjgoss committed Mar 25, 2022
1 parent 8e53bab commit ba0c700
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Expand Up @@ -19,6 +19,7 @@ jobs:
os:
- ubuntu-latest
- windows-latest
# these versions must be kept in sync with enginesTested.node in package.json
node-version: [12.x, 14.x, 16.x, 17.x]
fail-fast: false

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,9 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO
### Fixed
- Allow `file://` URLs to be used as formatter/snippet paths in options ([#1963](https://github.com/cucumber/cucumber-js/pull/1963) [#1920](https://github.com/cucumber/cucumber-js/issues/1920))

### Changed
- Emit a warning when using a Node.js version that's untested with Cucumber ([#1959](https://github.com/cucumber/cucumber-js/pull/1959))

## [8.0.0-rc.3] - 2022-03-21
### Added
- Add support for Node.js 17
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -190,6 +190,9 @@
"engines": {
"node": "12 || 14 || >=16"
},
"enginesTested": {
"node": "12 || 14 || 16 || 17"
},
"dependencies": {
"@cspotcode/source-map-support": "^0.7.0",
"@cucumber/ci-environment": "9.0.0",
Expand Down
12 changes: 8 additions & 4 deletions src/cli/run.ts
Expand Up @@ -16,10 +16,14 @@ function displayPublishAdvertisementBanner(): void {
}

export default async function run(): Promise<void> {
validateNodeEngineVersion(process.version, (error) => {
console.error(error)
process.exit(1)
})
validateNodeEngineVersion(
process.version,
(error) => {
console.error(error)
process.exit(1)
},
console.warn
)

const cli = new Cli({
argv: process.argv,
Expand Down
13 changes: 10 additions & 3 deletions src/cli/validate_node_engine_version.ts
Expand Up @@ -4,6 +4,7 @@ import semver from 'semver'

type PackageJSON = {
engines: { node: string }
enginesTested: { node: string }
}

const readActualPackageJSON: () => PackageJSON = () =>
Expand All @@ -16,12 +17,18 @@ const readActualPackageJSON: () => PackageJSON = () =>
export function validateNodeEngineVersion(
currentVersion: string,
onError: (message: string) => void,
onWarning: (message: string) => void,
readPackageJSON: () => PackageJSON = readActualPackageJSON
): void {
const requiredVersion = readPackageJSON().engines.node
if (!semver.satisfies(currentVersion, requiredVersion)) {
const requiredVersions = readPackageJSON().engines.node
const testedVersions = readPackageJSON().enginesTested.node
if (!semver.satisfies(currentVersion, requiredVersions)) {
onError(
`Cucumber can only run on Node.js versions ${requiredVersion}. This Node.js version is ${currentVersion}`
`Cucumber can only run on Node.js versions ${requiredVersions}. This Node.js version is ${currentVersion}`
)
} else if (!semver.satisfies(currentVersion, testedVersions)) {
onWarning(
`This Node.js version (${currentVersion}) has not been tested with this version of Cucumber; it should work normally, but please raise an issue if you see anything unexpected.`
)
}
}
45 changes: 24 additions & 21 deletions src/cli/validate_node_engine_version_spec.ts
Expand Up @@ -3,66 +3,69 @@ import * as sinon from 'sinon'
import { validateNodeEngineVersion } from './validate_node_engine_version'

describe(validateNodeEngineVersion.name, () => {
const readPackageJSON = () => ({
engines: {
node: '12 || 14 || >=16',
},
enginesTested: {
node: '12 || 14 || 16 || 17',
},
})

it('calls the onError callback when the version is lower than any of our supported versions', () => {
// Arrange
const errorSpy = sinon.spy()
const warningSpy = sinon.spy()

// Act
validateNodeEngineVersion('v11.1.2', errorSpy, () => ({
engines: {
node: '12 || 14 || >=16',
},
}))
validateNodeEngineVersion('v11.1.2', errorSpy, warningSpy, readPackageJSON)

// Assert
expect(errorSpy).to.have.been.calledOnceWith(
'Cucumber can only run on Node.js versions 12 || 14 || >=16. This Node.js version is v11.1.2'
)
expect(warningSpy).not.to.have.been.called()
})

it('calls the onError callback when the version is between our supported versions', () => {
// Arrange
const errorSpy = sinon.spy()
const warningSpy = sinon.spy()

validateNodeEngineVersion('v13.1.2', errorSpy, () => ({
engines: {
node: '12 || 14 || >=16',
},
}))
validateNodeEngineVersion('v13.1.2', errorSpy, warningSpy, readPackageJSON)

// Assert
expect(errorSpy).to.have.been.calledOnceWith(
'Cucumber can only run on Node.js versions 12 || 14 || >=16. This Node.js version is v13.1.2'
)
expect(warningSpy).not.to.have.been.called()
})

it('does not call the onError when the version is one of our supported versions', () => {
it('does not call onError or onWarning when the version is one of our supported versions', () => {
// Arrange
const errorSpy = sinon.spy()
const warningSpy = sinon.spy()

// Act
validateNodeEngineVersion('v17.1.2', errorSpy, () => ({
engines: {
node: '12 || 14 || >=16',
},
}))
validateNodeEngineVersion('v17.1.2', errorSpy, warningSpy, readPackageJSON)

// Assert
expect(errorSpy).not.to.have.been.called()
expect(warningSpy).not.to.have.been.called()
})

it('does not call onError when the version is a version that isnt out yet at time of release', () => {
// Arrange
const errorSpy = sinon.spy()
const warningSpy = sinon.spy()

// Act
validateNodeEngineVersion('v18.0.0', errorSpy, () => ({
engines: {
node: '12 || 14 || >=16',
},
}))
validateNodeEngineVersion('v18.0.0', errorSpy, warningSpy, readPackageJSON)

// Assert
expect(errorSpy).not.to.have.been.called()
expect(warningSpy).to.have.been.calledOnceWith(
`This Node.js version (v18.0.0) has not been tested with this version of Cucumber; it should work normally, but please raise an issue if you see anything unexpected.`
)
})
})

0 comments on commit ba0c700

Please sign in to comment.