UNPKG

@theia-extension-tester/mocha-runner

Version:
111 lines 4.86 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MochaRunner = void 0; const Mocha = require("mocha"); const sanitize = require("sanitize-filename"); const extension_tester_page_objects_1 = require("extension-tester-page-objects"); const repeat_1 = require("@theia-extension-tester/repeat"); class MochaRunner { constructor(browser, mochaOptions) { this.browser = browser; this.mochaOptions = mochaOptions; this.testCounter = 1; } beforeAll(mochaContext) { return __awaiter(this, void 0, void 0, function* () { mochaContext.timeout(this.browser.timeouts.pageLoadTimeout()); yield this.browser.start(); this.exitListener = this.afterAll.bind(this, mochaContext); process.on('exit', this.exitListener); process.on('SIGINT', this.exitListener); }); } afterAll(mochaContext) { var _a; return __awaiter(this, void 0, void 0, function* () { mochaContext.timeout(this.browser.timeouts.pageLoadTimeout()); process.on('uncaughtException', (e) => { if (e instanceof repeat_1.RepeatExitError) { return; } process.exitCode = (process.exitCode !== undefined) ? (process.exitCode) : (1); console.error(e); }); try { yield this.browser.quit(); } catch (_b) { } finally { process.exitCode = (_a = process.exitCode) !== null && _a !== void 0 ? _a : 0; } }); } beforeEach(mochaContext) { return __awaiter(this, void 0, void 0, function* () { }); } afterEach(mochaContext) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { mochaContext.timeout(30000); if (!((_a = mochaContext.currentTest) === null || _a === void 0 ? void 0 : _a.isPassed())) { try { yield this.browser.takeScreenshot(sanitize(`${(_b = mochaContext.currentTest) === null || _b === void 0 ? void 0 : _b.title}`, { replacement: `Test ${this.testCounter}` })); } catch (e) { if (e instanceof extension_tester_page_objects_1.error.NoSuchSessionError) { return; } console.warn(e); yield this.browser.takeScreenshot(`Test ${this.testCounter}`); } this.testCounter++; } }); } runTests(files) { return __awaiter(this, void 0, void 0, function* () { const runner = this; const mochaRunner = new Mocha(this.mochaOptions); mochaRunner.files = files; mochaRunner.fullTrace(); mochaRunner.slow(5000); return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { mochaRunner.suite.beforeAll(function () { return __awaiter(this, void 0, void 0, function* () { yield runner.beforeAll.call(runner, this); }); }); mochaRunner.suite.beforeEach(function () { return __awaiter(this, void 0, void 0, function* () { yield runner.beforeEach.call(runner, this); }); }); mochaRunner.suite.afterAll(function () { return __awaiter(this, void 0, void 0, function* () { yield runner.afterAll.call(runner, this); }); }); mochaRunner.suite.afterEach(function () { return __awaiter(this, void 0, void 0, function* () { yield runner.afterEach.call(runner, this); }); }); mochaRunner.run(resolve); })); }); } } exports.MochaRunner = MochaRunner; //# sourceMappingURL=MochaRunner.js.map