@chainsafe/eth2.0-spec-test-util
Version:
Spec test suite generator from yaml test files
89 lines (72 loc) • 3.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.describeMultiSpec = describeMultiSpec;
var _fs = require("fs");
var _chai = require("chai");
var _v8ProfilerNext = _interopRequireDefault(require("v8-profiler-next"));
var _mocha = require("mocha");
var _nodejs = require("@chainsafe/eth2.0-utils/lib/nodejs");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unused-vars */
const env = process.env;
/**
* Run yaml Eth2.0 bulk spec tests (m) for a certain function
* Compares actual vs expected for all test cases
* @param {string} testYamlPath - path to yaml spec test
* @param {Function} testFunc - function to use to generate output
* @param {Function} getInput - function to convert test case into input array
* @param {Function} getExpected - function to convert test case into a
* comparable expected output
* @param {Function} getActual - function to convert function output into
* comparable actual output
* @param {Function} shouldError - function to convert test case into a
* boolean, if the case should result in an error
* @param {Function} shouldSkip - function to convert test case into a boolean,
* if the case should be skipped
* @param {Function} expectFunc - function to run expectations against expected
* and actual output
* @param timeout - how long to wait before marking tests as failed (default 2000ms). Set to 0 to wait infinitely
*/
function describeMultiSpec(testYamlPath, testFunc, getInput, getExpected, getActual, shouldError = (testCase, index) => false, shouldSkip = (testCase, index) => false, expectFunc = (testCase, expect, expected, actual) => expect(actual).to.be.equal(expected), timeout = 2000) {
const testSpec = (0, _nodejs.loadYamlFile)(testYamlPath);
const testSuiteName = "".concat(testSpec.runner, " - ").concat(testSpec.handler, " - ").concat(testSpec.title, " - ").concat(testSpec.config);
(0, _mocha.describe)(testSuiteName, function () {
this.timeout(timeout);
testSpec.testCases.forEach((testCase, index) => {
if (shouldSkip(testCase, index)) {
return;
}
const description = index + (testCase.description ? " - " + testCase.description : "");
(0, _mocha.it)(description, function () {
const inputs = getInput(testCase);
if (shouldError(testCase, index)) {
(0, _chai.expect)(testFunc.bind(null, ...inputs)).to.throw();
} else {
const profileId = "".concat(description, "-").concat(Date.now(), ".profile");
if (env.GEN_PROFILE_DIR) {
_v8ProfilerNext.default.startProfiling(profileId);
}
const result = testFunc(...inputs);
if (env.GEN_PROFILE_DIR) {
const profile = _v8ProfilerNext.default.stopProfiling(profileId);
const directory = env.GEN_PROFILE_DIR || __dirname;
profile.export((error, result) => {
if (error) {
return;
}
(0, _fs.writeFile)("".concat(directory, "/").concat(profileId), result, () => {
profile.delete();
});
});
}
const actual = getActual(result);
const expected = getExpected(testCase);
expectFunc(testCase, _chai.expect, expected, actual);
}
});
});
});
}
//# sourceMappingURL=multi.js.map