approvals
Version:
Approval Tests Library - Capturing Human Intelligence
63 lines (62 loc) • 2.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MochaNamer = void 0;
exports.getMochaNamer = getMochaNamer;
const path_1 = __importDefault(require("path"));
const Namer_1 = require("../../Namer");
const Namer_2 = require("../../Core/Namer");
class MochaNamer extends Namer_1.Namer {
// @ts-ignore errors if Mocha is not used
constructor(mochaTest, overrideBasePath) {
if (!mochaTest || !mochaTest.test) {
throw new Error("Mocha test context was not supplied");
}
super("", "");
this.ctx = mochaTest.test;
this.basePath = overrideBasePath || path_1.default.dirname(this.ctx.file);
}
getFullTestName(testContext) {
let test = testContext;
let parentStack = [];
let currParent = test;
while (currParent && currParent.parent) {
parentStack.push(currParent);
currParent = currParent.parent;
}
let newTitle = "";
let parentStackReversed = parentStack.reverse();
parentStackReversed.forEach((item, index) => {
if (index !== 0) {
newTitle += ".";
}
newTitle += item.title
.split(" ")
.join("_")
.replace(/[^\w\s]/gi, "_");
});
return newTitle;
}
pathCreator(type, ext) {
if (!this.name) {
if (!this.ctx) {
throw new Error("ctx was not defined.");
}
this.name = this.getFullTestName(this.ctx);
}
return super.pathCreator(type, ext);
}
}
exports.MochaNamer = MochaNamer;
function getMochaNamer(test) {
if (!test || !test.file || !test.title) {
throw new Error(`Test context is missing required properties.\ntitle:${test.title}\nfile:${test.file}`);
}
const file = path_1.default.parse(test.file);
const testPath = file.dir;
const testFileName = file.name;
const testName = (0, Namer_2.convertToFilename)(`${testFileName}.${test.title}`);
return new Namer_1.Namer(testPath, testName);
}