UNPKG

@serenity-js/jasmine

Version:

Serenity/JS test runner adapter for Jasmine, enabling the use of the Screenplay Pattern in Jasmine-based test suites and leveraging Serenity/JS reporting capabilities

120 lines 4.14 kB
import { ExecutionIgnored } from '@serenity-js/core/lib/model/index.js'; import reporter from '../index.js'; import { AcceptingSpecFilter, CustomFunctionSpecFilter, GrepSpecFilter, InvertedGrepSpecFilter } from './filters/index.js'; /** * Allows for programmatic execution of Jasmine test scenarios, * using [`SerenityReporterForJasmine`](https://serenity-js.org/api/jasmine/function/default/) to report progress. * * ## Learn more * - [`TestRunnerAdapter`](https://serenity-js.org/api/core-adapter/interface/TestRunnerAdapter/) * * @group Integration */ export class JasmineAdapter { config; loader; runner; totalScenarios = 0; static defaultConfig = { /* * Serenity/JS doesn't use Jasmine's assertions, so this mechanism can be disabled */ oneFailurePerSpec: true, /* * A spec should stop execution as soon as there's a hook or spec failure * See https://github.com/angular/protractor/issues/3234 */ stopSpecOnExpectationFailure: true, /* * Default to not executing tests at random. * See https://github.com/angular/protractor/blob/4f74a4ec753c97adfe955fe468a39286a0a55837/lib/frameworks/jasmine.js#L76 */ random: false, }; constructor(config, loader) { this.config = config; this.loader = loader; } /** * Scenario success threshold for this test runner. */ successThreshold() { return ExecutionIgnored; } /** * Loads test scenarios. * * @param pathsToScenarios */ async load(pathsToScenarios) { const JasmineRunner = this.loader.require('jasmine'); this.runner = new JasmineRunner({ projectBaseDir: '' }); if (this.config.defaultTimeoutInterval) { this.runner.jasmine.DEFAULT_TIMEOUT_INTERVAL = this.config.defaultTimeoutInterval; } this.runner.clearReporters(); this.runner.loadConfig(Object.assign(JasmineAdapter.defaultConfig, this.config)); const reporterConfig = this.config.specDir ? { specDirectory: this.config.specDir } : {}; this.runner.addReporter(reporter(reporterConfig, this.runner.jasmine)); if (this.config.reporters) { this.config.reporters.forEach(reporter => { this.runner.addReporter(reporter); }); } this.runner.configureDefaultReporter(this.config); this.runner.loadRequires(); this.runner.loadHelpers(); this.configureSpecFilter(); await this.loadSpecs(pathsToScenarios); this.countScenarios(this.runner.env.topSuite()); } configureSpecFilter() { /* * Configure spec filter */ this.runner.env.configure({ specFilter: spec => this.specFilter().matches(spec.getFullName()), }); } async loadSpecs(pathsToScenarios) { this.runner.specDir = ''; this.runner.specFiles = []; this.runner.addMatchingSpecFiles(pathsToScenarios); await this.runner.loadSpecs(); } specFilter() { if (this.config.specFilter) { return new CustomFunctionSpecFilter(this.config.specFilter); } if (this.config.grep) { return this.config.invertGrep ? new InvertedGrepSpecFilter(this.config.grep) : new GrepSpecFilter(this.config.grep); } return new AcceptingSpecFilter(); } /** * Returns the number of loaded scenarios */ scenarioCount() { return this.totalScenarios; } countScenarios(suite) { suite.children?.forEach((child) => { if (Array.isArray(child.children)) { return this.countScenarios(child); } if (this.specFilter().matches(child.getFullName())) { this.totalScenarios++; } }); } /** * Runs loaded test scenarios. */ async run() { this.runner.exitOnCompletion = false; await this.runner.execute(); } } //# sourceMappingURL=JasmineAdapter.js.map