Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from DevExpress/multiple-streams
Browse files Browse the repository at this point in the history
Support multiple reporters (closes #3)
  • Loading branch information
AlexanderMoskovkin authored Dec 5, 2017
2 parents 42c8e82 + 549c207 commit d7f0127
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,32 @@ testcafe(options)

Configures the test runner to run tests in the specified browsers.

#### reporter
#### reporters

*Type*: `String`
*Type*: `String` || `Array`

*Default*: `spec`

*Details*: [Reporters](https://devexpress.github.io/testcafe/documentation/using-testcafe/common-concepts/reporters.html)

Specifies the reporter.
Specifies the reporter or an array of reporters.

Reporter can be specified by reporter name, or an object with following properties:

* `name` - name of the reporter,
* `file` - the path to a file where reporter's output will be redirected,
* `outStream` - an Writable Stream instance where reporter's output will be piped. The `file` property will be ignored if `outStream` is specified.

Examples:
```js
"reporters": "minimal"
```
```js
"reporters": { "name": "json", "file": "report.json" }
```
```js
"reporters": { "name": "xunit", "outStream": fs.createWriteStream("report.xml") }
```
#### filter

*Type*: `function(testName, fixtureName, fixturePath)`
Expand Down
33 changes: 24 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
var fs = require('fs');
var defaults = require('lodash.defaults');
var flatten = require('lodash.flatten');
var createTestCafe = require('testcafe');
var PluginError = require('gulp-util').PluginError;
var through = require('through2');

var DEFAULT_REPORTER = 'spec';

var DEFAULT_OPTS = {
browsers: [],
filter: null,
screenshotsPath: null,
takeScreenshotsOnFail: false,
reporter: 'spec',
reporter: [],
skipJsErrors: false,
quarantineMode: false,
selectorTimeout: 10000,

// NOTE: exposed for testing purposes
reportOutStream: null
selectorTimeout: 10000
};

module.exports = function gulpTestCafe (opts) {
var files = [];

opts = defaults({}, opts, DEFAULT_OPTS);

opts.reporter = flatten([opts.reporter]);

function onFile (file, enc, cb) {
if (file.isNull())
cb(null, file);
Expand All @@ -45,13 +48,25 @@ module.exports = function gulpTestCafe (opts) {

var runner = testcafe.createRunner();

return runner
runner
.src(files)
.browsers(opts.browsers)
.filter(opts.filter)
.screenshots(opts.screenshotsPath, opts.takeScreenshotsOnFail)
.reporter(opts.reporter, opts.reportOutStream)
.run(opts);
.screenshots(opts.screenshotsPath, opts.takeScreenshotsOnFail);

opts.reporter.forEach(function (reporter) {
if (typeof reporter === 'string')
runner.reporter(reporter);
else {
runner.reporter(
reporter.name || DEFAULT_REPORTER,
reporter.file ? fs.createWriteStream(reporter.file) : reporter.outStream
);
}
});

return runner.run(opts);

})
.then(function (failed) {
if (failed > 0)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"dependencies": {
"gulp-util": "^3.0.7",
"lodash.defaults": "^4.2.0",
"lodash.flatten": "^4.4.0",
"testcafe": "latest",
"through2": "^2.0.1"
},
Expand Down
43 changes: 36 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ it('Should run tests', function () {
var reportStream = createReportOutStream();

var ps = gulpTestCafe({
browsers: ['chrome', 'firefox'],
reportOutStream: reportStream
browsers: ['chrome', 'firefox'],
reporter: { outStream: reportStream }
});


Expand All @@ -44,8 +44,8 @@ it('Should fail if tests fail', function () {
var reportStream = createReportOutStream();

var ps = gulpTestCafe({
browsers: ['chrome'],
reportOutStream: reportStream
browsers: ['chrome'],
reporter: { outStream: reportStream }
});

var resultsPromise = Promise.race([
Expand All @@ -71,9 +71,8 @@ it('Should fail if configuration is incorrect', function () {
var reportStream = createReportOutStream();

var ps = gulpTestCafe({
browsers: ['chrome'],
reporter: 'unknown',
reportOutStream: reportStream
browsers: ['chrome'],
reporter: { name: 'unknown', outStream: reportStream },
});

var resultsPromise = Promise.race([
Expand All @@ -92,3 +91,33 @@ it('Should fail if configuration is incorrect', function () {

return resultsPromise;
});

it('Should use multiple reporters', function () {
var reportStream1 = createReportOutStream();
var reportStream2 = createReportOutStream();

var ps = gulpTestCafe({
browsers: ['chrome', 'firefox'],
reporter: [{ outStream: reportStream1 }, { name: 'json', outStream: reportStream2 }]
});

var resultsPromise = Promise.race([
promisifyEvent(ps, 'error'),

promisifyEvent(ps, 'done').then(function () {
expect(reportStream1.data).contains('Chrome');
expect(reportStream1.data).contains('Firefox');
expect(reportStream1.data).contains('3 passed');

expect(reportStream2.data).contains('Chrome');
expect(reportStream2.data).contains('Firefox');
expect(reportStream2.data).contains('"passed": 3');
})
]);

ps.write(vinylFile.readSync('test/fixtures/passing1.js'));
ps.write(vinylFile.readSync('test/fixtures/passing2.js'));
ps.end();

return resultsPromise;
});

0 comments on commit d7f0127

Please sign in to comment.