UNPKG

playwright-bdd

Version:
54 lines 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TagsFromPathInjector = void 0; const tagsFromPath_1 = require("../utils/tagsFromPath"); const AutofillMap_1 = require("../utils/AutofillMap"); /** * Adds tags inferred from feature file paths to the parsed Gherkin model. * Cucumber reports read tags from GherkinDocument/Pickle messages, so path tags must be injected * there in addition to being used by generated Playwright tests. */ class TagsFromPathInjector { constructor(newId) { this.newId = newId; } inject(gherkinDocument) { const { feature, uri } = gherkinDocument; if (!feature || !uri) return gherkinDocument; const tagsFromPath = (0, tagsFromPath_1.extractTagsFromPath)(uri); if (!tagsFromPath.length) return gherkinDocument; const tagNodes = this.injectTagsInFeature(feature, tagsFromPath); this.injectTagsInPickles(gherkinDocument, tagNodes); return gherkinDocument; } injectTagsInFeature(feature, tagsFromPath) { const tagNodesByName = new AutofillMap_1.AutofillMap(); feature.tags.forEach((tag) => tagNodesByName.set(tag.name, tag)); const pathTagNames = new Set(tagsFromPath); const tagNodes = [...pathTagNames].map((tagName) => { return tagNodesByName.getOrCreate(tagName, () => ({ id: this.newId(), name: tagName, location: { line: 1, column: 1 }, })); }); feature.tags = [...tagNodesByName.values()]; return tagNodes; } injectTagsInPickles(gherkinDocument, tagNodes) { gherkinDocument.pickles = gherkinDocument.pickles.map((pickle) => { const existingTagNames = new Set(pickle.tags.map((tag) => tag.name)); const pathTags = tagNodes .filter((tagNode) => !existingTagNames.has(tagNode.name)) .map((tagNode) => ({ name: tagNode.name, astNodeId: tagNode.id, })); return pathTags.length ? { ...pickle, tags: [...pickle.tags, ...pathTags] } : pickle; }); } } exports.TagsFromPathInjector = TagsFromPathInjector; //# sourceMappingURL=TagsFromPathInjector.js.map