UNPKG

jest-dynalite

Version:

Run your tests using Jest & Dynalite

80 lines 3.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTables = exports.getDynalitePort = exports.setConfigDir = exports.NotFoundError = exports.CONFIG_FILE_NAME_TS = exports.CONFIG_FILE_NAME_CJS = exports.CONFIG_FILE_NAME = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = require("path"); const utils_1 = require("./utils"); exports.CONFIG_FILE_NAME = "jest-dynalite-config.js"; exports.CONFIG_FILE_NAME_CJS = "jest-dynalite-config.cjs"; exports.CONFIG_FILE_NAME_TS = "jest-dynalite-config.ts"; class NotFoundError extends Error { constructor(dir) { super(`Could not find '${exports.CONFIG_FILE_NAME}' or '${exports.CONFIG_FILE_NAME_TS}' in dir ${dir}`); } } exports.NotFoundError = NotFoundError; if (!process.env.JEST_DYNALITE_CONFIG_DIRECTORY) { process.env.JEST_DYNALITE_CONFIG_DIRECTORY = process.cwd(); } const findConfigOrError = (directory) => { const foundFile = [exports.CONFIG_FILE_NAME, exports.CONFIG_FILE_NAME_CJS, exports.CONFIG_FILE_NAME_TS].find((config) => { const file = (0, path_1.resolve)(directory, config); return fs_1.default.existsSync(file); }); if (!foundFile) { throw new NotFoundError((0, path_1.resolve)(directory)); } return foundFile; }; const readConfig = () => { const configFile = findConfigOrError(process.env.JEST_DYNALITE_CONFIG_DIRECTORY); const file = (0, path_1.resolve)(process.env.JEST_DYNALITE_CONFIG_DIRECTORY, configFile); try { const importedConfig = require(file); // eslint-disable-line import/no-dynamic-require, global-require if ("default" in importedConfig) { return importedConfig.default; } return importedConfig; } catch (e) { throw new Error(`Something went wrong reading your ${configFile}: ${e.message}`); } }; const setConfigDir = (directory) => { // Only allow this directory to be set if a config exists findConfigOrError(directory); process.env.JEST_DYNALITE_CONFIG_DIRECTORY = directory; }; exports.setConfigDir = setConfigDir; const getDynalitePort = () => { const { basePort = 8000 } = readConfig(); if (Number.isInteger(basePort) && basePort > 0 && basePort <= 65535) { return basePort + parseInt(process.env.JEST_WORKER_ID || "1", 10); } throw new TypeError(`Option "basePort" must be an number between 1 and 65535. Received "${basePort.toString()}"`); }; exports.getDynalitePort = getDynalitePort; // Cache the tables result from the config function, so that we // are not calling it over and over let tablesCache; const getTables = async () => { if (tablesCache) { return tablesCache; } const tablesConfig = readConfig().tables; if ((0, utils_1.isFunction)(tablesConfig)) { tablesCache = await tablesConfig(); } else { tablesCache = tablesConfig !== null && tablesConfig !== void 0 ? tablesConfig : []; } if (!Array.isArray(tablesCache)) { throw new Error("jest-dynalite requires that the tables configuration is an array"); } return tablesCache; }; exports.getTables = getTables; //# sourceMappingURL=config.js.map