UNPKG

5.24 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _mockedApi = require('mocked-api');
10
11var _mockedApi2 = _interopRequireDefault(_mockedApi);
12
13var _terminate = require('terminate');
14
15var _terminate2 = _interopRequireDefault(_terminate);
16
17var _chai = require('chai');
18
19var _chai2 = _interopRequireDefault(_chai);
20
21var _configHelper = require('./utils/config-helper');
22
23var _target = require('./target');
24
25var _target2 = _interopRequireDefault(_target);
26
27var _browser = require('./browser');
28
29var _browser2 = _interopRequireDefault(_browser);
30
31var _tests = require('./tests');
32
33var _tests2 = _interopRequireDefault(_tests);
34
35function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
36
37function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
38
39var readLine = require('readline').createInterface({
40 input: process.stdin,
41 output: process.stdout
42});
43
44var GreenLight = function () {
45 function GreenLight() {
46 var _this = this;
47
48 _classCallCheck(this, GreenLight);
49
50 this.api = null;
51 this.API = _mockedApi2.default;
52
53 this.target = null;
54 this.Target = _target2.default;
55
56 this.browser = null;
57 this.Browser = _browser2.default;
58
59 this.tests = null;
60 this.Tests = _tests2.default;
61
62 this.expect = _chai2.default.expect;
63
64 this.pause = function (test) {
65 console.log('Paused at: ' + _this.browser.url);
66 var originalTimeout = test.timeout;
67 test.timeout(null);
68 return new Promise(function (resolve, reject) {
69 readLine.question('Press enter to continue.', function () {
70 test.timeout(originalTimeout);
71 readLine.close();
72 reject(new Error('Test is considered invalid because it was paused.'));
73 });
74 });
75 };
76 }
77
78 _createClass(GreenLight, [{
79 key: 'init',
80 value: function init() {
81 return Promise.resolve();
82 }
83 }, {
84 key: 'done',
85 value: function done(exitCode) {
86 return this.clean().then(function () {
87 process.exit(exitCode);
88 });
89 }
90 }, {
91 key: 'fail',
92 value: function fail(err) {
93 return this.clean().then(function () {
94 console.error(err.stack || err);
95 process.exit(1);
96 });
97 }
98 }, {
99 key: 'clean',
100 value: function clean() {
101 return new Promise(function (resolve, reject) {
102 (0, _terminate2.default)(process.pid, function (err) {
103 if (err) {
104 throw err;
105 }
106 resolve();
107 });
108 });
109 }
110
111 /*
112 * API
113 */
114
115 }, {
116 key: 'runAPI',
117 value: function runAPI(config) {
118 config = (0, _configHelper.parseConfigFor)('api', config);
119
120 if (config.verbose) {
121 console.log('Initializing API...');
122 }
123
124 var api = this.API.setup(config);
125
126 if (!config.name) {
127 // handy shortcut:
128 this.api = api;
129 } else {
130 // should be referenced with GreenLight.API.getByName()
131 }
132
133 return api.start().then(function () {
134 if (config.verbose) {
135 console.log('API ready');
136 }
137 });
138 }
139
140 /*
141 * Target
142 */
143
144 }, {
145 key: 'runTarget',
146 value: function runTarget(config) {
147 config = (0, _configHelper.parseConfigFor)('target', config);
148
149 if (config.verbose) {
150 console.log('Initializing target...');
151 }
152
153 this.target = new _target2.default(config);
154 return this.target.start().then(function () {
155 if (config.verbose) {
156 console.log('Target ready');
157 }
158 });
159 }
160
161 /*
162 * Browser
163 */
164
165 }, {
166 key: 'runBrowser',
167 value: function runBrowser(config) {
168 config = (0, _configHelper.parseConfigFor)('browser', config);
169
170 if (config.verbose) {
171 console.log('Initializing browser...');
172 }
173
174 this.browser = new _browser2.default(config);
175 return this.browser.start().then(function () {
176 if (config.verbose) {
177 console.log('Browser ready');
178 }
179 });
180 }
181
182 /*
183 * Mocha
184 */
185
186 }, {
187 key: 'runTests',
188 value: function runTests(config) {
189 config = (0, _configHelper.parseConfigFor)('tests', config);
190
191 if (config.verbose) {
192 console.log('Initializing tests...');
193 }
194
195 this.tests = new _tests2.default(config);
196 return this.tests.start().then(function (failures) {
197 if (config.verbose) {
198 console.log('Tests ready');
199 }
200 return failures;
201 });
202 }
203 }]);
204
205 return GreenLight;
206}();
207
208exports.default = GreenLight;
209
210
211module.exports = new GreenLight();
\No newline at end of file