UNPKG

4.5 kBJavaScriptView Raw
1"use strict";
2/**
3 * @module Core
4 */
5var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
8}) : (function(o, m, k, k2) {
9 if (k2 === undefined) k2 = k;
10 o[k2] = m[k];
11}));
12var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
13 Object.defineProperty(o, "default", { enumerable: true, value: v });
14}) : function(o, v) {
15 o["default"] = v;
16});
17var __importStar = (this && this.__importStar) || function (mod) {
18 if (mod && mod.__esModule) return mod;
19 var result = {};
20 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
21 __setModuleDefault(result, mod);
22 return result;
23};
24var __importDefault = (this && this.__importDefault) || function (mod) {
25 return (mod && mod.__esModule) ? mod : { "default": mod };
26};
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.isCoreException = exports.TestsStore = void 0;
29/*
30 * japa
31 *
32 * (c) Harminder Virk <virk@adonisjs.com>
33 *
34 * For the full copyright and license information, please view the LICENSE
35 * file that was distributed with this source code.
36*/
37const time_span_1 = __importDefault(require("time-span"));
38const Contracts_1 = require("../Contracts");
39const exceptions = __importStar(require("../Exceptions"));
40/**
41 * Tests store class records the tests being executed and
42 * returns a report to be used by the reporters.
43 */
44class TestsStore {
45 constructor() {
46 this._store = {
47 passedCount: 0,
48 skippedCount: 0,
49 failedCount: 0,
50 todoCount: 0,
51 regressionCount: 0,
52 total: 0,
53 groups: [],
54 duration: 0,
55 };
56 }
57 /**
58 * Returns the currently active group
59 */
60 get activeGroup() {
61 return this._store.groups[this._store.groups.length - 1];
62 }
63 /**
64 * Record the group
65 */
66 recordGroup(group) {
67 this.open();
68 this._store.groups.push({ title: group.title, failedTests: [], failedHooks: [] });
69 }
70 /**
71 * End the group and record failed hooks
72 */
73 endGroup(group) {
74 if (group.status === Contracts_1.IGroupStatus.FAILED && this.activeGroup.title === group.title) {
75 const error = group.error;
76 this.activeGroup.failedHooks.push({ title: error.fnName || error.lifecycle, error });
77 }
78 }
79 /**
80 * Record the test
81 */
82 recordTest(test) {
83 this.open();
84 /**
85 * Store reference to test and the group when test has been
86 * failed
87 */
88 if (test.status === Contracts_1.ITestStatus.FAILED) {
89 this.activeGroup.failedTests.push({ title: test.title, error: test.error });
90 }
91 /**
92 * Increment the total counter
93 */
94 this._store.total++;
95 /**
96 * Increment individual test statuses counters
97 */
98 switch (test.status) {
99 case Contracts_1.ITestStatus.FAILED:
100 this._store.failedCount++;
101 break;
102 case Contracts_1.ITestStatus.PASSED:
103 this._store.passedCount++;
104 break;
105 case Contracts_1.ITestStatus.SKIPPED:
106 this._store.skippedCount++;
107 break;
108 case Contracts_1.ITestStatus.TODO:
109 this._store.todoCount++;
110 break;
111 }
112 /**
113 * Increment the regression counter
114 */
115 if (test.regression) {
116 this._store.regressionCount++;
117 }
118 }
119 /**
120 * Open store
121 */
122 open() {
123 if (!this._processStart) {
124 this._processStart = (0, time_span_1.default)();
125 }
126 }
127 /**
128 * Close store
129 */
130 close() {
131 if (!this._store.duration) {
132 this._store.duration = this._processStart.rounded();
133 }
134 }
135 /**
136 * Return store report
137 */
138 getReport() {
139 this.close();
140 return this._store;
141 }
142}
143exports.TestsStore = TestsStore;
144/**
145 * Returns a boolean telling if exception is part of core exceptions or
146 * not.
147 */
148function isCoreException(error) {
149 return !!Object.keys(exceptions).find((e) => error instanceof exceptions[e]);
150}
151exports.isCoreException = isCoreException;