@cucumber/cucumber
Version:
The official JavaScript implementation of Cucumber.
126 lines • 4.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runCucumber = runCucumber;
const node_events_1 = require("node:events");
const messages_1 = require("@cucumber/messages");
const helpers_1 = require("../formatter/helpers");
const helpers_2 = require("../cli/helpers");
const paths_1 = require("../paths");
const version_1 = require("../version");
const runtime_1 = require("../runtime");
const environment_1 = require("../environment");
const formatters_1 = require("./formatters");
const support_1 = require("./support");
const gherkin_1 = require("./gherkin");
const plugins_1 = require("./plugins");
/**
* Execute a Cucumber test run and return the overall result
*
* @public
* @param options - Options for the run, obtainable via {@link loadConfiguration}
* @param environment - Project environment
* @param onMessage - Callback fired each time Cucumber emits a message
*/
async function runCucumber(options, environment = {}, onMessage) {
const mergedEnvironment = (0, environment_1.makeEnvironment)(environment);
const { cwd, stdout, stderr, env, logger } = mergedEnvironment;
logger.debug(`Running cucumber-js ${version_1.version}
Working directory: ${cwd}
Running from: ${__dirname}
`);
const newId = messages_1.IdGenerator.uuid();
const supportCoordinates = 'originalCoordinates' in options.support
? options.support.originalCoordinates
: Object.assign({
requireModules: [],
requirePaths: [],
loaders: [],
importPaths: [],
}, options.support);
const pluginManager = await (0, plugins_1.initializeForRunCucumber)({
...options,
support: supportCoordinates,
}, mergedEnvironment);
const resolvedPaths = await (0, paths_1.resolvePaths)(logger, cwd, options.sources, supportCoordinates);
pluginManager.emit('paths:resolve', resolvedPaths);
const { sourcePaths, requirePaths, importPaths } = resolvedPaths;
const supportCodeLibrary = 'originalCoordinates' in options.support
? options.support
: await (0, support_1.getSupportCodeLibrary)({
logger,
cwd,
newId,
requirePaths,
requireModules: supportCoordinates.requireModules,
importPaths,
loaders: supportCoordinates.loaders,
});
const eventBroadcaster = new node_events_1.EventEmitter();
if (onMessage) {
eventBroadcaster.on('envelope', onMessage);
}
eventBroadcaster.on('envelope', (value) => pluginManager.emit('message', value));
const eventDataCollector = new helpers_1.EventDataCollector(eventBroadcaster);
let formatterStreamError = false;
const cleanupFormatters = await (0, formatters_1.initializeFormatters)({
env,
cwd,
stdout,
stderr,
logger,
onStreamError: () => (formatterStreamError = true),
eventBroadcaster,
eventDataCollector,
configuration: options.formats,
supportCodeLibrary,
pluginManager,
});
await (0, helpers_2.emitMetaMessage)(eventBroadcaster, env);
let filteredPickles = [];
let parseErrors = [];
if (sourcePaths.length > 0) {
const gherkinResult = await (0, gherkin_1.getPicklesAndErrors)({
newId,
cwd,
sourcePaths,
coordinates: options.sources,
onEnvelope: (envelope) => eventBroadcaster.emit('envelope', envelope),
});
filteredPickles = await pluginManager.transform('pickles:filter', gherkinResult.filterablePickles);
filteredPickles = await pluginManager.transform('pickles:order', filteredPickles);
parseErrors = gherkinResult.parseErrors;
}
if (parseErrors.length) {
parseErrors.forEach((parseError) => {
logger.error(`Parse error in "${parseError.source.uri}" ${parseError.message}`);
});
await cleanupFormatters();
await pluginManager.cleanup();
return {
success: false,
support: supportCodeLibrary,
};
}
(0, helpers_2.emitSupportCodeMessages)({
eventBroadcaster,
supportCodeLibrary,
newId,
});
const runtime = await (0, runtime_1.makeRuntime)({
environment,
logger,
eventBroadcaster,
sourcedPickles: filteredPickles,
newId,
supportCodeLibrary,
options: options.runtime,
});
const success = await runtime.run();
await pluginManager.cleanup();
await cleanupFormatters();
return {
success: success && !formatterStreamError,
support: supportCodeLibrary,
};
}
//# sourceMappingURL=run_cucumber.js.map