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

Bring in createStatsCollector from Mocha 6 as simple compatibility fix for Mocha 6 users #3424

Merged
merged 4 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/wct-mocha/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

<!-- ## Unreleased -->
## Unreleased
* Added support for Mocha 6.0.0, which introduced a breaking change related to initialization of stats on Mocha's test-runner, preventing reporting of test results.
<!-- Add new, unreleased changes here. -->

## 1.0.0
Expand Down
7 changes: 7 additions & 0 deletions packages/wct-mocha/src/reporters/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* rights grant found at http://polymer.github.io/PATENTS.txt
*/
import * as util from '../util.js';
import {createStatsCollector} from './stats-collector.js';

// We capture console events when running tests; so make sure we have a
// reference to the original one.
Expand Down Expand Up @@ -81,6 +82,12 @@ export default class Console {
* @param runner The runner that is being reported on.
*/
constructor(runner: Mocha.IRunner) {
// Mocha 6 runner doesn't have stats at this point so we need to use
// the stats-collector from Mocha to add them before calling the base
// reporter.
if (!runner.stats) {
createStatsCollector(runner as unknown as Mocha.Runner);
}
Mocha.reporters.Base.call(this, runner);

runner.on(
Expand Down
94 changes: 94 additions & 0 deletions packages/wct-mocha/src/reporters/stats-collector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';
/**
(The MIT License)

Copyright (c) 2011-2018 JS Foundation and contributors, https://js.foundation

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* The following was extracted from
* https://github.com/mochajs/mocha/blob/master/lib/stats-collector.js
*/

/**
* Test statistics collector.
*
* @typedef {Object} StatsCollector
* @property {number} suites - integer count of suites run.
* @property {number} tests - integer count of tests run.
* @property {number} passes - integer count of passing tests.
* @property {number} pending - integer count of pending tests.
* @property {number} failures - integer count of failed tests.
* @property {Date} start - time when testing began.
* @property {Date} end - time when testing concluded.
* @property {number} duration - number of msecs that testing took.
*/

/**
* Provides stats such as test duration,
* number of tests passed / failed etc.
*
* @public
* @memberof Mocha
* @param {Runner} runner
*/
export const createStatsCollector = (runner: Mocha.Runner) => {
var stats:
Mocha.Stats = {suites: 0, tests: 0, passes: 0, pending: 0, failures: 0};

if (!runner) {
throw new TypeError('Missing runner argument');
}

runner.stats = stats;

runner.once('start', function() {
stats.start = new Date();
});

runner.on('suite', function(suite) {
suite.root || stats.suites++;
});

runner.on('pass', function() {
stats.passes++;
});

runner.on('fail', function() {
stats.failures++;
});

runner.on('pending', function() {
stats.pending++;
});

runner.on('test end', function() {
stats.tests++;
});

runner.once('end', function() {
stats.end = new Date();
// To coerce to numbers and make TS compiler happy, we use unary `+` prefix
// for date arithmetic.
stats.duration = +stats.end - +stats.start;
});
}
7 changes: 7 additions & 0 deletions packages/wct-mocha/src/reporters/title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* rights grant found at http://polymer.github.io/PATENTS.txt
*/
import * as util from '../util.js';
import {createStatsCollector} from './stats-collector.js';

const ARC_OFFSET = 0; // start at the right.
const ARC_WIDTH = 6;
Expand All @@ -21,6 +22,12 @@ const ARC_WIDTH = 6;
*/
export default class Title {
constructor(runner: Mocha.IRunner) {
// Mocha 6 runner doesn't have stats at this point so we need to use
// the stats-collector from Mocha to add them before calling the base
// reporter.
if (!runner.stats) {
createStatsCollector(runner as unknown as Mocha.Runner);
}
Mocha.reporters.Base.call(this, runner);

runner.on('test end', this.report.bind(this));
Expand Down
Loading