107 lines
15 KiB
JavaScript
107 lines
15 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const phase_1 = __importDefault(require("../test-run/phase"));
|
||
|
const process_test_fn_error_1 = __importDefault(require("../errors/process-test-fn-error"));
|
||
|
const execute_fn_with_timeout_1 = __importDefault(require("../utils/execute-fn-with-timeout"));
|
||
|
class FixtureHookController {
|
||
|
constructor(tests, browserConnectionCount) {
|
||
|
this._fixtureMap = FixtureHookController._createFixtureMap(tests, browserConnectionCount);
|
||
|
}
|
||
|
static _ensureFixtureMapItem(fixtureMap, fixture) {
|
||
|
if (!fixtureMap.has(fixture)) {
|
||
|
const item = {
|
||
|
started: false,
|
||
|
runningFixtureBeforeHook: false,
|
||
|
fixtureBeforeHookErr: null,
|
||
|
pendingTestRunCount: 0,
|
||
|
fixtureCtx: Object.create(null),
|
||
|
};
|
||
|
fixtureMap.set(fixture, item);
|
||
|
}
|
||
|
}
|
||
|
static _createFixtureMap(tests, browserConnectionCount) {
|
||
|
return tests.reduce((fixtureMap, test) => {
|
||
|
const fixture = test.fixture;
|
||
|
if (!test.skip) {
|
||
|
FixtureHookController._ensureFixtureMapItem(fixtureMap, fixture);
|
||
|
const item = fixtureMap.get(fixture);
|
||
|
item.pendingTestRunCount += browserConnectionCount;
|
||
|
}
|
||
|
return fixtureMap;
|
||
|
}, new Map());
|
||
|
}
|
||
|
_getFixtureMapItem(test) {
|
||
|
return test.skip ? null : this._fixtureMap.get(test.fixture);
|
||
|
}
|
||
|
isTestBlocked(test) {
|
||
|
const item = this._getFixtureMapItem(test);
|
||
|
return !!item && item.runningFixtureBeforeHook;
|
||
|
}
|
||
|
async _runFixtureBeforeHook(item, fn, testRun) {
|
||
|
if (!fn)
|
||
|
return true;
|
||
|
item.runningFixtureBeforeHook = true;
|
||
|
try {
|
||
|
await (0, execute_fn_with_timeout_1.default)(fn, testRun.executionTimeout, item.fixtureCtx);
|
||
|
}
|
||
|
catch (err) {
|
||
|
item.fixtureBeforeHookErr = (0, process_test_fn_error_1.default)(err);
|
||
|
}
|
||
|
item.runningFixtureBeforeHook = false;
|
||
|
return !item.fixtureBeforeHookErr;
|
||
|
}
|
||
|
async _runFixtureAfterHook(item, fn, testRun) {
|
||
|
if (!fn)
|
||
|
return;
|
||
|
testRun.phase = phase_1.default.inFixtureAfterHook;
|
||
|
try {
|
||
|
await (0, execute_fn_with_timeout_1.default)(fn, testRun.executionTimeout, item.fixtureCtx);
|
||
|
}
|
||
|
catch (err) {
|
||
|
testRun.addError((0, process_test_fn_error_1.default)(err));
|
||
|
}
|
||
|
}
|
||
|
async runFixtureBeforeHookIfNecessary(testRun) {
|
||
|
const fixture = testRun.test.fixture;
|
||
|
const item = this._getFixtureMapItem(testRun.test);
|
||
|
if (item) {
|
||
|
const shouldRunBeforeHook = !item.started;
|
||
|
item.started = true;
|
||
|
const success = shouldRunBeforeHook
|
||
|
&& await this._runFixtureBeforeHook(item, fixture.globalBeforeFn, testRun)
|
||
|
&& await this._runFixtureBeforeHook(item, fixture.beforeFn, testRun);
|
||
|
// NOTE: fail all tests in fixture if fixture.before hook has error
|
||
|
if (!success && item.fixtureBeforeHookErr) {
|
||
|
testRun.phase = phase_1.default.inFixtureBeforeHook;
|
||
|
testRun.addError(item.fixtureBeforeHookErr);
|
||
|
return false;
|
||
|
}
|
||
|
testRun.fixtureCtx = item.fixtureCtx;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
async runFixtureAfterHookIfNecessary(testRun) {
|
||
|
var _a;
|
||
|
const fixture = testRun.test.fixture;
|
||
|
const item = this._getFixtureMapItem(testRun.test);
|
||
|
if (!item)
|
||
|
return;
|
||
|
item.pendingTestRunCount--;
|
||
|
if (item.pendingTestRunCount !== 0)
|
||
|
return;
|
||
|
await this._runFixtureAfterHook(item, fixture.afterFn, testRun);
|
||
|
await this._runFixtureAfterHook(item, fixture.globalAfterFn, testRun);
|
||
|
if (item.fixtureCtx) {
|
||
|
await ((_a = testRun.compilerService) === null || _a === void 0 ? void 0 : _a.removeFixtureCtxsFromState({
|
||
|
fixtureIds: [fixture.id],
|
||
|
}));
|
||
|
}
|
||
|
this._fixtureMap.delete(fixture);
|
||
|
}
|
||
|
}
|
||
|
exports.default = FixtureHookController;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZml4dHVyZS1ob29rLWNvbnRyb2xsZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvcnVubmVyL2ZpeHR1cmUtaG9vay1jb250cm9sbGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsOERBQStDO0FBQy9DLDRGQUFpRTtBQUlqRSwrRkFBb0U7QUFVcEUsTUFBcUIscUJBQXFCO0lBR3RDLFlBQW9CLEtBQWEsRUFBRSxzQkFBOEI7UUFDN0QsSUFBSSxDQUFDLFdBQVcsR0FBRyxxQkFBcUIsQ0FBQyxpQkFBaUIsQ0FBQyxLQUFLLEVBQUUsc0JBQXNCLENBQUMsQ0FBQztJQUM5RixDQUFDO0lBRU8sTUFBTSxDQUFDLHFCQUFxQixDQUFFLFVBQXNDLEVBQUUsT0FBZ0I7UUFDMUYsSUFBSSxDQUFDLFVBQVUsQ0FBQyxHQUFHLENBQUMsT0FBTyxDQUFDLEVBQUU7WUFDMUIsTUFBTSxJQUFJLEdBQUc7Z0JBQ1QsT0FBTyxFQUFtQixLQUFLO2dCQUMvQix3QkFBd0IsRUFBRSxLQUFLO2dCQUMvQixvQkFBb0IsRUFBTSxJQUFJO2dCQUM5QixtQkFBbUIsRUFBTyxDQUFDO2dCQUMzQixVQUFVLEVBQWdCLE1BQU0sQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDO2FBQ2hELENBQUM7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