UNPKG

2.21 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Provides a factory function for a {@link StatsCollector} object.
5 * @module
6 */
7
8var constants = require('./runner').constants;
9var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
10var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
11var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN;
12var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
13var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
14var EVENT_RUN_END = constants.EVENT_RUN_END;
15var EVENT_TEST_END = constants.EVENT_TEST_END;
16
17/**
18 * Test statistics collector.
19 *
20 * @public
21 * @typedef {Object} StatsCollector
22 * @property {number} suites - integer count of suites run.
23 * @property {number} tests - integer count of tests run.
24 * @property {number} passes - integer count of passing tests.
25 * @property {number} pending - integer count of pending tests.
26 * @property {number} failures - integer count of failed tests.
27 * @property {Date} start - time when testing began.
28 * @property {Date} end - time when testing concluded.
29 * @property {number} duration - number of msecs that testing took.
30 */
31
32var Date = global.Date;
33
34/**
35 * Provides stats such as test duration, number of tests passed / failed etc., by listening for events emitted by `runner`.
36 *
37 * @private
38 * @param {Runner} runner - Runner instance
39 * @throws {TypeError} If falsy `runner`
40 */
41function createStatsCollector(runner) {
42 /**
43 * @type StatsCollector
44 */
45 var stats = {
46 suites: 0,
47 tests: 0,
48 passes: 0,
49 pending: 0,
50 failures: 0
51 };
52
53 if (!runner) {
54 throw new TypeError('Missing runner argument');
55 }
56
57 runner.stats = stats;
58
59 runner.once(EVENT_RUN_BEGIN, function() {
60 stats.start = new Date();
61 });
62 runner.on(EVENT_SUITE_BEGIN, function(suite) {
63 suite.root || stats.suites++;
64 });
65 runner.on(EVENT_TEST_PASS, function() {
66 stats.passes++;
67 });
68 runner.on(EVENT_TEST_FAIL, function() {
69 stats.failures++;
70 });
71 runner.on(EVENT_TEST_PENDING, function() {
72 stats.pending++;
73 });
74 runner.on(EVENT_TEST_END, function() {
75 stats.tests++;
76 });
77 runner.once(EVENT_RUN_END, function() {
78 stats.end = new Date();
79 stats.duration = stats.end - stats.start;
80 });
81}
82
83module.exports = createStatsCollector;