approvals
Version:
Approval Tests Library - Capturing Human Intelligence
83 lines (82 loc) • 3.43 kB
JavaScript
;
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.beforeEachVerifierBase = beforeEachVerifierBase;
const _ = __importStar(require("lodash"));
const fs = __importStar(require("fs"));
const cfg = __importStar(require("../config"));
const StringWriter_1 = require("../StringWriter");
const FileApprover_1 = require("../FileApprover");
const ReporterFactory_1 = require("../Reporting/ReporterFactory");
const aUtils = __importStar(require("../AUtils"));
function beforeEachLoaderFunction(Namer, dirName, that) {
// Add methods to the test context
that.approvals = { getCurrentReporters };
that.verify = verify;
that.verifyAsJSON = verifyAsJSON;
function getCurrentReporters(options) {
options = options || cfg.currentConfig();
const reporterCandidates = ReporterFactory_1.ReporterFactory.loadAllReporters(options.reporters);
return reporterCandidates;
}
function verify(data, overrideOptions) {
// @ts-ignore
const context = this;
const namer = new Namer(context, dirName);
const newOptions = _.defaults(overrideOptions || {}, cfg.currentConfig());
const reporterFactory = function () {
return { getCurrentReporters }.getCurrentReporters(newOptions);
};
const writer = new StringWriter_1.StringWriter(newOptions, data);
FileApprover_1.FileApprover.verify(namer, writer, reporterFactory);
}
function verifyAsJSON(data, overrideOptions) {
// @ts-ignore
this.verify(aUtils.stringifyKeysInOrder(data), overrideOptions);
}
}
function beforeEachVerifierBase(Namer, usageSample, dirName) {
if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName);
}
// Make sure it's a valid directory
const stats = fs.lstatSync(dirName);
if (!stats.isDirectory()) {
throw new Error(`Invalid directory [${dirName}]. Try using the following syntax. > ${usageSample}`);
}
beforeEach(function () {
return beforeEachLoaderFunction(Namer, dirName, this);
});
}