From 949b98f402bc319bcf43079d82d00801ee67ecb5 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Sat, 22 Oct 2022 08:48:38 -0700 Subject: [PATCH] Allow instantiated reporters to be provided in the configuration This supports more complex scenarios than the --reporter= CLI flag (multiple reporters, reporters that need configuration, reporters that aren't default exports, etc) without pushing the complexity of all of those scenarios into Jasmine or requiring the user to move to programmatic usage. See #77 and #159. --- lib/jasmine.js | 18 ++++++++++++++++++ spec/jasmine_spec.js | 11 +++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/jasmine.js b/lib/jasmine.js index 1ac5c4cc..3a90ad5a 100644 --- a/lib/jasmine.js +++ b/lib/jasmine.js @@ -384,6 +384,24 @@ class Jasmine { if(config.spec_files) { this.addMatchingSpecFiles(config.spec_files); } + + /** + * An array of reporters. Each object in the array will be passed to + * {@link Jasmine#addReporter|addReporter}. + * + * This provides a middle ground between the --reporter= CLI option and full + * programmatic usage. Note that because reporters are objects with methods, + * this option can only be used in JavaScript config files + * (e.g `spec/support/jasmine.js`), not JSON. + * @name Configuration#reporters + * @type Reporter[] | undefined + * @see custom_reporter + */ + if (config.reporters) { + for (const r of config.reporters) { + this.addReporter(r); + } + } } addRequires(requires) { diff --git a/spec/jasmine_spec.js b/spec/jasmine_spec.js index efd697f9..49440e3b 100644 --- a/spec/jasmine_spec.js +++ b/spec/jasmine_spec.js @@ -385,6 +385,17 @@ describe('Jasmine', function() { expect(this.fixtureJasmine.alwaysListPendingSpecs_).toBeTrue(); }); + + it('adds specified reporters', function() { + const reporter1 = {id: 'reporter1'}; + const reporter2 = {id: 'reporter2'}; + this.configObject.reporters = [reporter1, reporter2]; + + this.fixtureJasmine.loadConfig(this.configObject); + + expect(this.fixtureJasmine.env.addReporter).toHaveBeenCalledWith(reporter1); + expect(this.fixtureJasmine.env.addReporter).toHaveBeenCalledWith(reporter2); + }); }); describe('from a file', function() {