jest-environment-steps
Version:
Jest Environment to run the tests as Steps
90 lines (89 loc) • 3.84 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jest_environment_node_1 = __importDefault(require("jest-environment-node"));
const getTestAbsoluteName = (test) => {
const names = [];
let parentBlock = test;
while (parentBlock) {
names.unshift(parentBlock.name);
parentBlock = parentBlock.parent;
}
return names.join(" ");
};
const getTestEntryMap = (state) => {
const testEntryMap = {};
const updateMap = (node, parentName) => {
if (node.type == "test") {
testEntryMap[[parentName, node.name].join(" ")] = node;
}
else {
for (const child of node.children) {
updateMap(child, [parentName, node.name].join(" ").trim());
}
}
};
updateMap(state.rootDescribeBlock, "");
return testEntryMap;
};
const resolveTestPattern = (pattern, testEntryMap) => {
const testEntryNames = Object.keys(testEntryMap);
testEntryNames.reverse();
let testAbsoluteName = "";
for (const testEntryName of testEntryNames) {
if (testEntryName.startsWith(`ROOT_DESCRIBE_BLOCK ${pattern}`.trim())) {
testAbsoluteName = testEntryName;
break;
}
}
return testAbsoluteName;
};
class StepEnvironment extends jest_environment_node_1.default {
handleTestEvent(event, state) {
return __awaiter(this, void 0, void 0, function* () {
const skipFutureTests = (testOrPattern, state) => {
const testEntryMap = getTestEntryMap(state);
const absoluteName = typeof testOrPattern == "string"
? resolveTestPattern(testOrPattern, testEntryMap)
: getTestAbsoluteName(testOrPattern);
let i = 0;
const absoluteNames = Object.keys(testEntryMap);
for (; i < absoluteNames.length; i++) {
if (absoluteNames[i] == absoluteName) {
i++;
break;
}
}
for (; i < absoluteNames.length; i++) {
testEntryMap[absoluteNames[i]].mode = "skip";
}
};
if (event.name == "run_start") {
const testNamePatternStr = state.testNamePattern
? state.testNamePattern.toString()
: "//i";
const testNamePattern = testNamePatternStr.substring(1, testNamePatternStr.length - 2);
// reset the testNamePattern, so that all tests are eligible to run
state.testNamePattern = undefined;
// skip the tests that appears after the pattern
skipFutureTests(testNamePattern, state);
}
//skip future tests if current test failed
if (event.name == "test_fn_failure") {
skipFutureTests(event.test, state);
}
});
}
}
exports.default = StepEnvironment;