Skip to content

Commit

Permalink
upd lib
Browse files Browse the repository at this point in the history
  • Loading branch information
olexandr13 committed Jan 19, 2025
1 parent b942f0d commit afd7f7c
Showing 12 changed files with 200 additions and 239 deletions.
1 change: 0 additions & 1 deletion lib/adapter/playwright.d.ts
Original file line number Diff line number Diff line change
@@ -11,5 +11,4 @@ declare class PlaywrightReporter {
onEnd(result: any): Promise<void>;
#private;
}
export function initPlaywrightForStorage(): Promise<void>;
import TestomatioClient from '../client.js';
54 changes: 9 additions & 45 deletions lib/adapter/playwright.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initPlaywrightForStorage = initPlaywrightForStorage;
const picocolors_1 = __importDefault(require("picocolors"));
const crypto_1 = __importDefault(require("crypto"));
const os_1 = __importDefault(require("os"));
@@ -132,9 +108,12 @@ class PlaywrightReporter {
return path_1.default.join(this.config.outputDir || this.config.projects[0].outputDir, artifact.path);
}
if (artifact.body) {
const fileName = tmpFile();
fs_1.default.writeFileSync(fileName, artifact.body);
return fileName;
let filePath = generateTmpFilepath(artifact.name);
const extension = artifact.contentType?.split('/')[1]?.replace('jpeg', 'jpg');
if (extension)
filePath += `.${extension}`;
fs_1.default.writeFileSync(filePath, artifact.body);
return filePath;
}
return null;
}
@@ -214,9 +193,10 @@ function appendStep(step, shift = 0) {
}
return resultStep;
}
function tmpFile(prefix = 'tmp.') {
function generateTmpFilepath(filename = '') {
filename = filename || `tmp.${crypto_1.default.randomBytes(16).toString('hex')}`;
const tmpdir = os_1.default.tmpdir();
return path_1.default.join(tmpdir, prefix + crypto_1.default.randomBytes(16).toString('hex'));
return path_1.default.join(tmpdir, filename);
}
/**
* Returns filename + test title
@@ -226,20 +206,4 @@ function tmpFile(prefix = 'tmp.') {
function getTestContextName(test) {
return `${test._requireFile || ''}_${test.title}`;
}
async function initPlaywrightForStorage() {
try {
// @ts-ignore-next-line
// eslint-disable-next-line import/no-extraneous-dependencies
const { test } = await Promise.resolve().then(() => __importStar(require('@playwright/test')));
// eslint-disable-next-line no-empty-pattern
test.beforeEach(async ({}, testInfo) => {
global.testomatioTestTitle = `${testInfo.file || ''}_${testInfo.title}`;
});
}
catch (e) {
// ignore
}
}
module.exports = PlaywrightReporter;

module.exports.initPlaywrightForStorage = initPlaywrightForStorage;
77 changes: 38 additions & 39 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -282,46 +282,45 @@ class Client {
.then(() => {
if (!this.uploader.isEnabled)
return;
const filesizeStrMaxLength = 7;
if (this.uploader.successfulUploads.length) {
console.log(constants_js_1.APP_PREFIX, `🗄️ ${this.uploader.successfulUploads.length} artifacts ${process.env.TESTOMATIO_PRIVATE_ARTIFACTS ? 'privately' : picocolors_1.default.bold('publicly')} 🟢uploaded to S3 bucket`);
const filesizeStrMaxLength = 7;
if (this.uploader.successfulUploads.length) {
debug('\n', constants_js_1.APP_PREFIX, `🗄️ ${this.uploader.successfulUploads.length} artifacts uploaded to S3 bucket`);
const uploadedArtifacts = this.uploader.successfulUploads.map(file => ({
relativePath: file.path.replace(process.cwd(), ''),
link: file.link,
sizePretty: (0, filesize_1.filesize)(file.size, { round: 0 }).toString(),
}));
uploadedArtifacts.forEach(upload => {
debug(`🟢Uploaded artifact`, `${upload.relativePath},`, 'size:', `${upload.sizePretty},`, 'link:', `${upload.link}`);
});
}
if (this.uploader.failedUploads.length) {
console.log(constants_js_1.APP_PREFIX, `🗄️ ${this.uploader.failedUploads.length} artifacts 🔴${picocolors_1.default.bold('failed')} to upload`);
const failedUploads = this.uploader.failedUploads.map(file => ({
relativePath: file.path.replace(process.cwd(), ''),
sizePretty: (0, filesize_1.filesize)(file.size, { round: 0 }).toString(),
}));
const pathPadding = Math.max(...failedUploads.map(upload => upload.relativePath.length)) + 1;
failedUploads.forEach(upload => {
console.log(` ${picocolors_1.default.gray('|')} 🔴 ${upload.relativePath.padEnd(pathPadding)} ${picocolors_1.default.gray(`| ${upload.sizePretty.padStart(filesizeStrMaxLength)} |`)}`);
});
}
if (this.uploader.skippedUploads.length) {
console.log('\n', constants_js_1.APP_PREFIX, `🗄️ ${picocolors_1.default.bold(this.uploader.skippedUploads.length)} artifacts uploading 🟡${picocolors_1.default.bold('skipped')}`);
const skippedUploads = this.uploader.skippedUploads.map(file => ({
relativePath: file.path.replace(process.cwd(), ''),
sizePretty: file.size === null ? 'unknown' : (0, filesize_1.filesize)(file.size, { round: 0 }).toString(),
}));
const pathPadding = Math.max(...skippedUploads.map(upload => upload.relativePath.length)) + 1;
skippedUploads.forEach(upload => {
console.log(` ${picocolors_1.default.gray('|')} 🟡 ${upload.relativePath.padEnd(pathPadding)} ${picocolors_1.default.gray(`| ${upload.sizePretty.padStart(filesizeStrMaxLength)} |`)}`);
});
}
if (this.uploader.skippedUploads.length || this.uploader.failedUploads.length) {
const command = `TESTOMATIO=<your_api_key> TESTOMATIO_RUN=${this.runId} npx @testomatio/reporter upload-artifacts`;
console.log(constants_js_1.APP_PREFIX, `Run "${picocolors_1.default.magenta(command)}" with valid S3 credentials to upload skipped & failed artifacts`);
}
debug('\n', constants_js_1.APP_PREFIX, `🗄️ ${this.uploader.successfulUploads.length} artifacts uploaded to S3 bucket`);
const uploadedArtifacts = this.uploader.successfulUploads.map(file => ({
relativePath: file.path.replace(process.cwd(), ''),
link: file.link,
sizePretty: (0, filesize_1.filesize)(file.size, { round: 0 }).toString(),
}));
uploadedArtifacts.forEach(upload => {
debug(`🟢Uploaded artifact`, `${upload.relativePath},`, 'size:', `${upload.sizePretty},`, 'link:', `${upload.link}`);
});
}
if (this.uploader.failedUploads.length) {
console.log(constants_js_1.APP_PREFIX, `🗄️ ${this.uploader.failedUploads.length} artifacts 🔴${picocolors_1.default.bold('failed')} to upload`);
const failedUploads = this.uploader.failedUploads.map(file => ({
relativePath: file.path.replace(process.cwd(), ''),
sizePretty: (0, filesize_1.filesize)(file.size, { round: 0 }).toString(),
}));
const pathPadding = Math.max(...failedUploads.map(upload => upload.relativePath.length)) + 1;
failedUploads.forEach(upload => {
console.log(` ${picocolors_1.default.gray('|')} 🔴 ${upload.relativePath.padEnd(pathPadding)} ${picocolors_1.default.gray(`| ${upload.sizePretty.padStart(filesizeStrMaxLength)} |`)}`);
});
}
if (this.uploader.skippedUploads.length) {
console.log('\n', constants_js_1.APP_PREFIX, `🗄️ ${picocolors_1.default.bold(this.uploader.skippedUploads.length)} artifacts uploading 🟡${picocolors_1.default.bold('skipped')}`);
const skippedUploads = this.uploader.skippedUploads.map(file => ({
relativePath: file.path.replace(process.cwd(), ''),
sizePretty: file.size === null ? 'unknown' : (0, filesize_1.filesize)(file.size, { round: 0 }).toString(),
}));
const pathPadding = Math.max(...skippedUploads.map(upload => upload.relativePath.length)) + 1;
skippedUploads.forEach(upload => {
console.log(` ${picocolors_1.default.gray('|')} 🟡 ${upload.relativePath.padEnd(pathPadding)} ${picocolors_1.default.gray(`| ${upload.sizePretty.padStart(filesizeStrMaxLength)} |`)}`);
});
}
if (this.uploader.skippedUploads.length || this.uploader.failedUploads.length) {
const command = `TESTOMATIO=<your_api_key> TESTOMATIO_RUN=${this.runId} npx @testomatio/reporter upload-artifacts`;
const numberOfNotUploadedArtifacts = this.uploader.skippedUploads.length + this.uploader.failedUploads.length;
console.log(constants_js_1.APP_PREFIX, `${numberOfNotUploadedArtifacts} artifacts were not uploaded.
Run "${picocolors_1.default.magenta(command)}" with valid S3 credentials to upload skipped & failed artifacts`);
}
})
.catch(err => console.log(constants_js_1.APP_PREFIX, err));
3 changes: 3 additions & 0 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
31 changes: 17 additions & 14 deletions lib/reporter-functions.d.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
declare namespace _default {
export { saveArtifact as artifact };
export { logMessage as log };
export { addStep as step };
export { setKeyValue as keyValue };
}
export default _default;
/**
* Stores path to file as artifact and uploads it to the S3 storage
* @param {string | {path: string, type: string, name: string}} data - path to file or object with path, type and name
*/
export function saveArtifact(data: string | {
declare function saveArtifact(data: string | {
path: string;
type: string;
name: string;
}, context?: any): void;
/**
* Attach log message(s) to the test report
* @param {...any} args
* @param string
*/
export function logMessage(...args: any[]): void;
declare function logMessage(...args: any[]): void;
/**
* Similar to "log" function but marks message in report as a step
* @param {*} message
* @param {string} message
*/
export function addStep(message: any): void;
declare function addStep(message: string): void;
/**
* Add key-value pair(s) to the test report
* @param {*} keyValue
* @param {{[key: string]: string} | string} keyValue object { key: value } (multiple props allowed) or key (string)
* @param {string?} value
*/
export function setKeyValue(keyValue: any): void;
declare namespace _default {
export { saveArtifact as artifact };
export { logMessage as log };
export { addStep as step };
export { setKeyValue as keyValue };
}
export default _default;
declare function setKeyValue(keyValue: {
[key: string]: string;
} | string, value?: string | null): void;
38 changes: 17 additions & 21 deletions lib/reporter-functions.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveArtifact = saveArtifact;
exports.logMessage = logMessage;
exports.addStep = addStep;
exports.setKeyValue = setKeyValue;
const index_js_1 = require("./services/index.js");
const playwright_js_1 = require("./adapter/playwright.js");
(async () => {
if (process.env.PLAYWRIGHT_TEST_BASE_URL)
(0, playwright_js_1.initPlaywrightForStorage)();
})();
/**
* Stores path to file as artifact and uploads it to the S3 storage
* @param {string | {path: string, type: string, name: string}} data - path to file or object with path, type and name
*/
function saveArtifact(data, context = null) {
if (process.env.IS_PLAYWRIGHT)
throw new Error(`This function is not available in Playwright framework.
/Playwright supports artifacts out of the box`);
if (!data)
return;
index_js_1.services.artifacts.put(data, context);
}
/**
* Attach log message(s) to the test report
* @param {...any} args
* @param string
*/
function logMessage(...args) {
if (process.env.IS_PLAYWRIGHT)
throw new Error('This function is not available in Playwright framework');
index_js_1.services.logger._templateLiteralLog(...args);
}
/**
* Similar to "log" function but marks message in report as a step
* @param {*} message
* @param {string} message
*/
function addStep(message) {
if (process.env.IS_PLAYWRIGHT)
throw new Error('This function is not available in Playwright framework. Use playwright steps');
index_js_1.services.logger.step(message);
}
/**
* Add key-value pair(s) to the test report
* @param {*} keyValue
* @param {{[key: string]: string} | string} keyValue object { key: value } (multiple props allowed) or key (string)
* @param {string?} value
*/
function setKeyValue(keyValue) {
function setKeyValue(keyValue, value = null) {
if (process.env.IS_PLAYWRIGHT)
throw new Error('This function is not available in Playwright framework. Use test tag instead.');
if (typeof keyValue === 'string') {
keyValue = { [keyValue]: value };
}
index_js_1.services.keyValues.put(keyValue);
}
module.exports = {
@@ -46,11 +50,3 @@ module.exports = {
step: addStep,
keyValue: setKeyValue,
};

module.exports.saveArtifact = saveArtifact;

module.exports.logMessage = logMessage;

module.exports.addStep = addStep;

module.exports.setKeyValue = setKeyValue;
Loading

0 comments on commit afd7f7c

Please sign in to comment.