UNPKG

@assurka/jest

Version:

Assurka Jest Plugin

338 lines 17.1 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var node_fetch_1 = __importStar(require("node-fetch")); var git_last_commit_1 = require("git-last-commit"); var assurka_1 = require("./assurka"); var git_1 = require("./git/git"); var AssurkaCustomReporter = /** @class */ (function () { /** *Creates an instance of AssurkaCustomReporter. * @param {*} globalConfig * @param {*} options * @memberof AssurkaCustomReporter */ function AssurkaCustomReporter(globalConfig, options) { var _this = this; this.assurka = new assurka_1.Assurka(); this.alive = true; /** * Call for tests starting * @param {AggregatedResult} test - jest results * @param {ReporterOnStartOptions} options - jest invoked options */ this.onRunStart = function (test, options) { return __awaiter(_this, void 0, void 0, function () { var tokenResponse, commit, _a, _b, _c, status_1, changed, error_1, error_2; return __generator(this, function (_d) { switch (_d.label) { case 0: _d.trys.push([0, 21, , 22]); return [4 /*yield*/, this.assurka.setVariable({ name: '__ASSURKA_CONFIG__', complexValue: this.config })]; case 1: _d.sent(); if (!!this.config.token) return [3 /*break*/, 3]; return [4 /*yield*/, this.api("auth/api", 'POST', { id: this.config.projectId, secret: this.config.secret })]; case 2: tokenResponse = _d.sent(); this.config.token = tokenResponse.token; _d.label = 3; case 3: return [4 /*yield*/, this.getGitCommit(this._globalConfig.rootDir)]; case 4: commit = _d.sent(); if (!this.config.testPlanId) return [3 /*break*/, 19]; _a = this.config; return [4 /*yield*/, this.api("test-plan/" + this.config.projectId + "/get/" + this.config.testPlanId, 'GET')]; case 5: _a.testPlan = _d.sent(); _b = this.config; return [4 /*yield*/, this.api("test-param/" + this.config.testPlanId + "/list", 'GET')]; case 6: _b.testPlanParams = _d.sent(); if (!this.config.testPlanParams) return [3 /*break*/, 8]; // setup and configure the test plan variables return [4 /*yield*/, this.assurka.setVariables(this.config.testPlanParams)]; case 7: // setup and configure the test plan variables _d.sent(); _d.label = 8; case 8: if (!(test.numTotalTestSuites > 0)) return [3 /*break*/, 17]; _c = this.config; return [4 /*yield*/, this.api("test-run/" + this.config.testPlan.id + "/create", 'POST', __assign(__assign({}, test), { commit: commit, platform: 'jest' }))]; case 9: _c.testRun = _d.sent(); _d.label = 10; case 10: _d.trys.push([10, 15, , 16]); return [4 /*yield*/, git_1.gitStatus(this._globalConfig.rootDir)]; case 11: status_1 = _d.sent(); if (!status_1.remote_branch) return [3 /*break*/, 14]; return [4 /*yield*/, git_1.getChangedFiles(status_1.remote_branch)]; case 12: changed = _d.sent(); return [4 /*yield*/, this.api("test-committed/" + this.config.testRun.id + "/create", 'POST', changed['committedFiles'])]; case 13: _d.sent(); _d.label = 14; case 14: return [3 /*break*/, 16]; case 15: error_1 = _d.sent(); return [3 /*break*/, 16]; case 16: return [3 /*break*/, 18]; case 17: this.alive = false; _d.label = 18; case 18: return [3 /*break*/, 20]; case 19: this.alive = false; _d.label = 20; case 20: return [3 /*break*/, 22]; case 21: error_2 = _d.sent(); return [3 /*break*/, 22]; case 22: return [2 /*return*/]; } }); }); }; /** * Called at the start of a test spec * @param {Test} test - jest Test object */ this.onTestStart = function (test) { return __awaiter(_this, void 0, void 0, function () { var name_1, testSpec, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); name_1 = this.getSpecPath(test); if (!this.alive) return [3 /*break*/, 2]; return [4 /*yield*/, this.api("test-spec/" + this.config.testRun.id + "/create", 'POST', { name: name_1, testRunId: this.config.testRun.id })]; case 1: testSpec = _a.sent(); this.config.testSpecs[name_1] = testSpec; _a.label = 2; case 2: return [3 /*break*/, 4]; case 3: error_3 = _a.sent(); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; /** * Called on a test result * @param {Test} test - jest Test object * @param {AssertionResult} testCaseResult - jest results */ this.onTestCaseResult = function (test, testCaseResult) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { try { /* const spec = this.getSpecPath(test) const testSpec = this.config.testSpecs[spec] await this.api(`test-case/${testSpec.id}/create`, 'POST', { ...testCaseResult, testRunId: this.config.testRun.id }) */ } catch (error) { } return [2 /*return*/]; }); }); }; /** * Called when all is complete * @param {Set<Context>} contexts - jest context * @param {AggregatedResult} results - jest summarized results */ this.onRunComplete = function (contexts, results) { return __awaiter(_this, void 0, void 0, function () { var error_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 5, , 6]); if (!this.alive) return [3 /*break*/, 4]; return [4 /*yield*/, this.api("test-run/" + this.config.testPlan.id + "/update/" + this.config.testRun.id, 'PATCH', __assign(__assign({}, results), { endTime: new Date().getTime() }))]; case 1: _a.sent(); return [4 /*yield*/, this.api("test-plan/" + this.config.projectId + "/update/" + this.config.testPlanId, 'PATCH', __assign(__assign({}, results), { platform: 'jest', endTime: new Date().getTime() }))]; case 2: _a.sent(); if (!results.coverageMap) return [3 /*break*/, 4]; return [4 /*yield*/, this.api("test-run/" + this.config.testPlan.id + "/update/" + this.config.testRun.id + "/coverage", 'PATCH', results.coverageMap.toJSON())]; case 3: _a.sent(); _a.label = 4; case 4: return [3 /*break*/, 6]; case 5: error_4 = _a.sent(); return [3 /*break*/, 6]; case 6: return [2 /*return*/]; } }); }); }; /** * Called on a test spec completion * @param {Test} test - jest Test object * @param {TestResult} testResult - jest results * @param {AggregatedResult} aggregatedResult - jest summarized results */ this.onTestResult = function (test, testResult, aggregatedResult) { return __awaiter(_this, void 0, void 0, function () { var spec, testSpec, index, result, error_5; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 7, , 8]); if (!this.alive) return [3 /*break*/, 6]; spec = this.getSpecPath(test); testSpec = this.config.testSpecs[spec]; index = 0; _a.label = 1; case 1: if (!(index < testResult.testResults.length)) return [3 /*break*/, 4]; result = testResult.testResults[index]; return [4 /*yield*/, this.api("test-case/" + testSpec.id + "/create", 'POST', __assign(__assign({}, result), { position: index, testRunId: this.config.testRun.id }))]; case 2: _a.sent(); _a.label = 3; case 3: index++; return [3 /*break*/, 1]; case 4: return [4 /*yield*/, this.api("test-spec/" + this.config.testRun.id + "/update/" + testSpec.id, 'PATCH', __assign(__assign(__assign({}, testResult), testResult.perfStats), { testRunId: this.config.testRun.id }))]; case 5: _a.sent(); _a.label = 6; case 6: return [3 /*break*/, 8]; case 7: error_5 = _a.sent(); return [3 /*break*/, 8]; case 8: return [2 /*return*/]; } }); }); }; this.getLastError = function () { if (!_this.config.testPlanId) { return new Error('assurka: no test plan id has been configured'); } }; this.getSpecPath = function (test) { return test.path.replace(test.context.config.rootDir + "/", ''); }; /** *Sends results to the Assurka API * @param {string} url - the url * @param {string} method - the api method * @param {*} body - the api body * @memberof AssurkaCustomReporter */ this.api = function (url, method, body) { return __awaiter(_this, void 0, void 0, function () { var headers; return __generator(this, function (_a) { headers = new node_fetch_1.Headers({ 'Content-Type': 'application/json' }); if (this.config.token) { headers.append('Authorization', 'Bearer ' + this.config.token); } return [2 /*return*/, node_fetch_1.default(this.config.host + "/" + url, { method: method, headers: headers, body: JSON.stringify(body) }).then(function (response) { if (!response.ok) { return null; } return response.json(); })]; }); }); }; this.getGitCommit = function (path) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (res, _rej) { git_last_commit_1.getLastCommit(function (err, commit) { if (err) return res(null); return res(commit); }, { dst: path }); })]; }); }); }; this.config = options.assurka; this.config.host = this.config.host || 'https://api.assurka.io'; this.config.testPlanId = process.env.__ASSURKA_TESTPLAN_ID__ || this.config.testPlanId; this.config.testSpecs = {}; this._options = options; this._globalConfig = globalConfig; } return AssurkaCustomReporter; }()); exports.default = AssurkaCustomReporter; //# sourceMappingURL=index.js.map