@zebrunner/javascript-agent-mocha
Version:
Zebrunner Agent for Mocha
63 lines (48 loc) • 1.73 kB
JavaScript
const path = require('path');
const fs = require('fs');
const mime = require('mime-types');
const isBlankString = (value) => !value || value.trim().length === 0;
const isEmptyObject = (value) => Object.keys(value).length === 0 || Object.values(value).filter((prop) => prop).length === 0;
const isNotEmptyArray = (value) => Array.isArray(value) && value.length !== 0;
const isBuffer = (value) => Buffer.isBuffer(value);
const isFunction = (value) => typeof value === 'function';
const isCurrentTestPresent = (test) => !!(test && test.type === 'test');
const getSpecPath = (filePath) => {
if (filePath) {
const testFileDir = path
.parse(path.normalize(path.relative(process.cwd(), filePath)))
.dir.replace(new RegExp('\\'.concat(path.sep), 'g'), '/');
const testFile = path.parse(filePath);
return `${testFileDir}/${testFile.name}${testFile.ext}`;
}
};
const getTestTitle = (titlePath) => titlePath.slice(1).join(' ');
const getBase64Encode = (file) => {
if (fs.existsSync(file)) {
const buffer = fs.readFileSync(file);
return Buffer.from(buffer).toString('base64');
}
};
const getMimeTypeFromPath = (file) => {
if (fs.existsSync(file)) {
return mime.lookup(file);
}
};
/**
* Generates boundary parameter for Multipart request.
* Should be an arbitrary value that not exceeds 70 bytes in length and consists only of 7-bit US-ASCII characters.
*/
const generateMultipartBoundary = () => Math.floor(10000000 + Math.random() * 90000000);
module.exports = {
isBlankString,
isEmptyObject,
isNotEmptyArray,
isBuffer,
isFunction,
isCurrentTestPresent,
getSpecPath,
getTestTitle,
getBase64Encode,
getMimeTypeFromPath,
generateMultipartBoundary,
};