@itwin/certa
Version:
A mocha-based integration test runner
92 lines • 4.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CertaConfig = void 0;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const fs = require("fs");
const path = require("path");
const lodash = require("lodash");
const jsonc_parser_1 = require("jsonc-parser");
const SECONDS = 1000;
const MINUTES = 60 * SECONDS;
var CertaConfig;
(function (CertaConfig) {
CertaConfig.defaults = {
debug: false,
cover: false,
ports: {
debugging: 5858,
frontend: 3000,
frontendDebugging: 9223,
},
mochaOptions: {
invert: false,
timeout: 2 * MINUTES,
reporter: "spec",
},
chromeOptions: {
// NB: These should stay **empty** arrays, otherwise lodash.defaultsDeep will not merge them correctly.
args: new Array(),
publicDirs: new Array(),
},
};
// List of config options that may be relative file paths and should be resolved to absolute paths.
const _filePathOpts = [
"backendInitModule",
"testBundle",
"instrumentedTestBundle",
"chromeOptions.publicDirs",
];
/**
* Resolves all relative file paths in `opts` to absolute paths, starting from `baseDir`.
* NB: This mutates `opts`.
* @param baseDir The starting point for resolving relative paths.
* @param opts A partial CertaConfig object.
*/
function resolvePaths(baseDir, opts) {
for (const propPath of _filePathOpts) {
const relativeFilePath = lodash.get(opts, propPath);
if (relativeFilePath) {
if (Array.isArray(relativeFilePath))
lodash.set(opts, propPath, relativeFilePath.map((p) => path.resolve(baseDir, p)));
else
lodash.set(opts, propPath, path.resolve(baseDir, relativeFilePath));
}
}
}
/**
* Creates a complete CertaConfig object by combining a partial config with default values.
* All relative paths are resolved from the current working directory.
* Throws if the `testBundle` option is undefined or invalid.
* @param opts A partial CertaConfig object.
*/
function fromObject(opts) {
const resolvedOpts = lodash.defaultsDeep(opts, CertaConfig.defaults);
resolvePaths(process.cwd(), resolvedOpts);
if (!resolvedOpts.testBundle)
throw new Error("The required testBundle option was not set.");
if (!fs.existsSync(resolvedOpts.testBundle))
throw new Error(`The specified testBundle file "${resolvedOpts.testBundle}" does not exist.`);
return resolvedOpts;
}
CertaConfig.fromObject = fromObject;
/**
* Creates a complete CertaConfig object by loading options from a certa.json config file,
* applying run-time overrides, then combining that with default values.
* All relative paths specified in the config file are resolved from the config file directory.
* All other relative paths (overrides or defaults) are resolved from the current working directory.
* Throws if the `testBundle` option is undefined or invalid.
* @param filePath The path to a certa.json config file.
* @param overrides A partial CertaConfig object. These values will always override any options set in the config file.
*/
function fromConfigFile(filePath, overrides) {
const fileContents = fs.readFileSync(filePath);
const fileOpts = (0, jsonc_parser_1.parse)(fileContents.toString()); // Parsing with jsonc-parser lets us safely handle comments.
resolvePaths(path.dirname(filePath), fileOpts);
return fromObject(lodash.defaultsDeep(overrides, fileOpts));
}
CertaConfig.fromConfigFile = fromConfigFile;
})(CertaConfig || (exports.CertaConfig = CertaConfig = {}));
//# sourceMappingURL=CertaConfig.js.map