UNPKG

2.19 kBJavaScriptView Raw
1"use strict";
2/**
3 * @module Core
4 */
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Runner = void 0;
7/*
8 * japa
9 *
10 * (c) Harminder Virk <virk@adonisjs.com>
11 *
12 * For the full copyright and license information, please view the LICENSE
13 * file that was distributed with this source code.
14*/
15const Contracts_1 = require("../Contracts");
16const Emitter_1 = require("../Emitter");
17/**
18 * Runner class is used for defining global properties
19 * and run all the tests inside the group.
20 */
21class Runner {
22 constructor(_groups, _options) {
23 this._groups = _groups;
24 this._options = _options;
25 }
26 /**
27 * Returns a boolean telling if any of the groups or it's tests
28 * has errors.
29 */
30 get hasErrors() {
31 return !!this._groups.find((group) => group.hasErrors);
32 }
33 /**
34 * Define a set of test groups to use
35 */
36 useGroups(groups) {
37 this._groups = groups;
38 return this;
39 }
40 /**
41 * Define custom reporter
42 */
43 reporter(fn) {
44 if (typeof (fn) !== 'function') {
45 throw new Error('"runner.reporter" expects callback to be a valid function');
46 }
47 this._reporterFn = fn;
48 return this;
49 }
50 /**
51 * Run all the tests inside the registered groups
52 */
53 async run() {
54 if (typeof (this._reporterFn) !== 'function') {
55 throw new Error('Make sure to define tests reporter as a function');
56 }
57 /**
58 * Give emitter instance to the reporter
59 */
60 this._reporterFn(Emitter_1.emitter, this._options);
61 /**
62 * Emit the started event
63 */
64 Emitter_1.emitter.emit(Contracts_1.IEvents.STARTED);
65 /**
66 * Run all the tests
67 */
68 for (let group of this._groups) {
69 await group.run();
70 /**
71 * Break when bail is true and group has errors
72 */
73 if (this._options.bail && group.hasErrors) {
74 break;
75 }
76 }
77 /**
78 * Emit completed event
79 */
80 Emitter_1.emitter.emit(Contracts_1.IEvents.COMPLETED);
81 }
82}
83exports.Runner = Runner;