Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new API for running Cucumber #3995

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -14,11 +15,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 @@ -33,8 +32,6 @@ class CucumberSuite extends TestSuite {

throw err;
}

return CucumberCli;
}

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

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

Expand All @@ -79,16 +76,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 @@ -98,7 +126,7 @@ class CucumberSuite extends TestSuite {
);
}

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

Expand All @@ -114,15 +142,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 @@ -213,11 +241,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