testplane
Version:
Tests framework based on mocha and wdio
76 lines • 3.72 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readTestFileWithTestplaneDependenciesCollecting = exports.runWithTestplaneDependenciesCollecting = exports.getCollectedTestplaneDependencies = exports.enableCollectingTestplaneDependencies = exports.disableCollectingTestplaneDependencies = void 0;
const path_1 = __importDefault(require("path"));
const module_1 = require("module");
const async_hooks_1 = require("async_hooks");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const TypedModule = module_1.Module;
const testDependenciesStorage = new async_hooks_1.AsyncLocalStorage();
const testFileDependenciesCache = new Map();
let disableCollectingDependenciesCb = null;
const disableCollectingTestplaneDependencies = () => {
if (disableCollectingDependenciesCb) {
disableCollectingDependenciesCb();
disableCollectingDependenciesCb = null;
}
};
exports.disableCollectingTestplaneDependencies = disableCollectingTestplaneDependencies;
const enableCollectingTestplaneDependencies = () => {
if (disableCollectingDependenciesCb) {
return;
}
const originalResolveFileName = TypedModule._resolveFilename;
disableCollectingDependenciesCb = () => {
TypedModule._resolveFilename = originalResolveFileName;
};
TypedModule._resolveFilename = function () {
// eslint-disable-next-line prefer-rest-params
const result = originalResolveFileName.apply(this, arguments);
try {
const store = disableCollectingDependenciesCb ? testDependenciesStorage.getStore() : null;
const relPath = result && path_1.default.isAbsolute(result) ? path_1.default.relative(process.cwd(), result) : null;
if (store && relPath) {
const posixRelPath = path_1.default.sep === path_1.default.posix.sep ? relPath : relPath.replaceAll(path_1.default.sep, path_1.default.posix.sep);
store.jsTestplaneDeps?.add(posixRelPath);
}
}
catch { } // eslint-disable-line no-empty
return result;
};
};
exports.enableCollectingTestplaneDependencies = enableCollectingTestplaneDependencies;
const getCollectedTestplaneDependencies = () => {
const store = testDependenciesStorage.getStore();
return store && store.jsTestplaneDeps ? store.jsTestplaneDeps : null;
};
exports.getCollectedTestplaneDependencies = getCollectedTestplaneDependencies;
const runWithTestplaneDependenciesCollecting = (fn) => {
(0, exports.enableCollectingTestplaneDependencies)();
const store = { jsTestplaneDeps: new Set() };
return testDependenciesStorage.run(store, fn);
};
exports.runWithTestplaneDependenciesCollecting = runWithTestplaneDependenciesCollecting;
const readTestFileWithTestplaneDependenciesCollecting = (file, fn) => {
if (!disableCollectingDependenciesCb) {
return fn();
}
const store = testDependenciesStorage.getStore();
const jsTestplaneDeps = store && store.jsTestplaneDeps;
if (!jsTestplaneDeps) {
return fn();
}
const cachedDependencies = testFileDependenciesCache.get(file);
if (cachedDependencies) {
cachedDependencies.forEach(dependency => jsTestplaneDeps.add(dependency));
return fn();
}
return fn().finally(() => {
testFileDependenciesCache.set(file, Array.from(jsTestplaneDeps).sort((a, b) => a.localeCompare(b)));
});
};
exports.readTestFileWithTestplaneDependenciesCollecting = readTestFileWithTestplaneDependenciesCollecting;
//# sourceMappingURL=testplane-selectivity.js.map