Skip to content

Commit

Permalink
Use new API for running Cucumber
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgoss committed Jan 27, 2024
1 parent 917c261 commit e5219cd
Show file tree
Hide file tree
Showing 17 changed files with 529 additions and 7,201 deletions.
74 changes: 51 additions & 23 deletions lib/runner/test-runners/cucumber.js
@@ -1,4 +1,5 @@
const path = require('path');
const semver = require('semver');
const Runner = require('./default');
const TestSuite = require('../../testsuite');
const {Logger, isString, isDefined} = require('../../utils');
Expand All @@ -13,11 +14,9 @@ class CucumberSuite extends TestSuite {
].some(item => err.message.includes(item));
}

static createCli(settings) {
let CucumberCli;

static importCucumberModule(suffix = '') {
try {
CucumberCli = require('@cucumber/cucumber/lib/cli/index').default;
return require('@cucumber/cucumber' + suffix);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
const error = new Error('Cucumber needs to be installed as a project dependency.');
Expand All @@ -32,8 +31,6 @@ class CucumberSuite extends TestSuite {

throw err;
}

return CucumberCli;
}

static get cucumberSetupFile() {
Expand Down Expand Up @@ -61,7 +58,7 @@ class CucumberSuite extends TestSuite {
};

try {
this.createCucumberCli();
this.setupCucumberIntegration();
} catch (err) {
const {message} = err;

Expand All @@ -78,16 +75,47 @@ class CucumberSuite extends TestSuite {
}
}

createCucumberCli() {
const CucumberCli = CucumberSuite.createCli(this.settings);
const argv = this.getCliArgvForCucumber();
setupCucumberIntegration() {
this.cucumberModule = CucumberSuite.importCucumberModule();
this.integrationStrategy = this.settings.test_runner.options.integrationStrategy;
if (this.integrationStrategy) {
Logger.info(`Using Cucumber integration strategy ${this.integrationStrategy} based on explicit option.`);
} else {
// API behaviour we rely on introduced in cucumber-js 10.3.1
this.integrationStrategy = semver.satisfies(this.cucumberModule.version, '>=10.3.1') ? 'API' : 'CLI';
}

this.cucumberCli = new CucumberCli({
argv,
env: process.env,
this.cucumberOptions = this.getOptionsForCucumber();
this.cucumberEnvironment = {
cwd: process.cwd(),
stdout: process.stdout
});
stdout: process.stdout,
stderr: process.stderr,
env: process.env
};

if (this.integrationStrategy === 'API') {
this.cucumberApi = CucumberSuite.importCucumberModule('/api');
} else {
this.cucumberCli = new this.cucumberModule.Cli({
...this.cucumberEnvironment,
argv: [
process.execPath,
require.resolve('@cucumber/cucumber'),
...this.cucumberOptions.provided
]
});
}
}

async runCucumberIntegration() {
if (this.integrationStrategy === 'API') {
const {runConfiguration} = await this.cucumberApi.loadConfiguration(this.cucumberOptions, this.cucumberEnvironment);

return await this.cucumberApi.runCucumber(runConfiguration, this.cucumberEnvironment);
}

return await this.cucumberCli.run();

}

shouldUseNightwatchFormatter(options) {
Expand All @@ -97,7 +125,7 @@ class CucumberSuite extends TestSuite {
);
}

getCliArgvForCucumber() {
getOptionsForCucumber() {
const specs = this.createInitialRequires();
const {options} = this.settings.test_runner;

Expand All @@ -113,15 +141,15 @@ class CucumberSuite extends TestSuite {
options.format = [...(options.format || []), path.join(__dirname, './cucumber/nightwatch-format.js')];
}

const {feature_path = ''} = options;
const {feature_path = []} = options;
const parallelArgs = this.usingCucumberWorkers ? ['--parallel', this.usingCucumberWorkers] : [];
const additionalOptions = this.buildArgvValue(['tags', 'retry-tag-filter', 'profile', 'format', 'format-options', 'dry-run', 'fail-fast', ['retry', 'retries'], 'no-strict', 'name']);
const extraParams = ['--world-parameters', JSON.stringify({...this.argv, settings: this.settings})];

return [
process.execPath,
require.resolve('@cucumber/cucumber')
].concat(feature_path, parallelArgs, specs, additionalOptions, extraParams);
return {
profiles: this.mergeCliConfigValues('profile'),
provided: [].concat(feature_path, parallelArgs, specs, additionalOptions, extraParams)
};
}

createInitialRequires() {
Expand Down Expand Up @@ -212,11 +240,11 @@ class CucumberSuite extends TestSuite {
async runTestSuite() {
let result;
try {
result = await this.cucumberCli.run();
result = await this.runCucumberIntegration();
} catch (err) {
if (CucumberSuite.isSessionCreateError(err)) {
const {message} = err;
err.message = 'An error occurred while trying to start Cucumber CLI:';
err.message = 'An error occurred while trying to run Cucumber:';
err.detailedErr = message;
err.sessionCreate = true;
err.showTrace = false;
Expand Down

0 comments on commit e5219cd

Please sign in to comment.