@zohodesk/testinglibrary 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/.babelrc +3 -0
  2. package/build/bdd-framework/cli/commands/env.js +1 -1
  3. package/build/bdd-framework/config/configDir.js +35 -0
  4. package/build/bdd-framework/config/enrichReporterData.js +23 -0
  5. package/build/bdd-framework/config/index.js +10 -6
  6. package/build/bdd-framework/cucumber/createTestStep.js +43 -0
  7. package/build/bdd-framework/cucumber/formatter/EventDataCollector.js +126 -0
  8. package/build/bdd-framework/cucumber/formatter/GherkinDocumentParser.js +72 -0
  9. package/build/bdd-framework/cucumber/formatter/PickleParser.js +25 -0
  10. package/build/bdd-framework/cucumber/formatter/durationHelpers.js +13 -0
  11. package/build/bdd-framework/cucumber/formatter/getColorFns.js +57 -0
  12. package/build/bdd-framework/cucumber/formatter/index.js +16 -0
  13. package/build/bdd-framework/cucumber/formatter/locationHelpers.js +16 -0
  14. package/build/bdd-framework/cucumber/loadFeatures.js +62 -31
  15. package/build/bdd-framework/cucumber/loadSteps.js +7 -0
  16. package/build/bdd-framework/cucumber/resolveFeaturePaths.js +62 -0
  17. package/build/bdd-framework/cucumber/stepArguments.js +21 -0
  18. package/build/bdd-framework/cucumber/valueChecker.js +23 -0
  19. package/build/bdd-framework/gen/formatter.js +11 -23
  20. package/build/bdd-framework/gen/index.js +41 -20
  21. package/build/bdd-framework/gen/testFile.js +69 -26
  22. package/build/bdd-framework/gen/testMeta.js +60 -0
  23. package/build/bdd-framework/gen/testNode.js +11 -12
  24. package/build/bdd-framework/hooks/scenario.js +29 -6
  25. package/build/bdd-framework/hooks/worker.js +7 -1
  26. package/build/bdd-framework/index.js +8 -1
  27. package/build/bdd-framework/playwright/getLocationInFile.js +36 -9
  28. package/build/bdd-framework/playwright/loadUtils.js +33 -0
  29. package/build/bdd-framework/playwright/transform.js +5 -1
  30. package/build/bdd-framework/reporter/cucumber/base.js +57 -0
  31. package/build/bdd-framework/reporter/cucumber/custom.js +73 -0
  32. package/build/bdd-framework/reporter/cucumber/helper.js +12 -0
  33. package/build/bdd-framework/reporter/cucumber/html.js +35 -0
  34. package/build/bdd-framework/reporter/cucumber/index.js +74 -0
  35. package/build/bdd-framework/reporter/cucumber/json.js +312 -0
  36. package/build/bdd-framework/reporter/cucumber/junit.js +205 -0
  37. package/build/bdd-framework/reporter/cucumber/message.js +20 -0
  38. package/build/bdd-framework/reporter/cucumber/messagesBuilder/AttachmentMapper.js +64 -0
  39. package/build/bdd-framework/reporter/cucumber/messagesBuilder/Builder.js +196 -0
  40. package/build/bdd-framework/reporter/cucumber/messagesBuilder/GherkinDocument.js +43 -0
  41. package/build/bdd-framework/reporter/cucumber/messagesBuilder/GherkinDocumentClone.js +52 -0
  42. package/build/bdd-framework/reporter/cucumber/messagesBuilder/GherkinDocuments.js +105 -0
  43. package/build/bdd-framework/reporter/cucumber/messagesBuilder/Hook.js +70 -0
  44. package/build/bdd-framework/reporter/cucumber/messagesBuilder/Meta.js +45 -0
  45. package/build/bdd-framework/reporter/cucumber/messagesBuilder/Pickles.js +27 -0
  46. package/build/bdd-framework/reporter/cucumber/messagesBuilder/Projects.js +38 -0
  47. package/build/bdd-framework/reporter/cucumber/messagesBuilder/TestCase.js +128 -0
  48. package/build/bdd-framework/reporter/cucumber/messagesBuilder/TestCaseRun.js +126 -0
  49. package/build/bdd-framework/reporter/cucumber/messagesBuilder/TestCaseRunHooks.js +102 -0
  50. package/build/bdd-framework/reporter/cucumber/messagesBuilder/TestStepAttachments.js +50 -0
  51. package/build/bdd-framework/reporter/cucumber/messagesBuilder/TestStepRun.js +88 -0
  52. package/build/bdd-framework/reporter/cucumber/messagesBuilder/index.js +30 -0
  53. package/build/bdd-framework/reporter/cucumber/messagesBuilder/pwUtils.js +51 -0
  54. package/build/bdd-framework/reporter/cucumber/messagesBuilder/timing.js +35 -0
  55. package/build/bdd-framework/reporter/cucumber/messagesBuilder/types.js +5 -0
  56. package/build/bdd-framework/run/StepInvoker.js +21 -26
  57. package/build/bdd-framework/run/bddDataAttachment.js +46 -0
  58. package/build/bdd-framework/run/bddFixtures.js +31 -10
  59. package/build/bdd-framework/run/bddWorld.js +5 -2
  60. package/build/bdd-framework/run/bddWorldInternal.js +15 -0
  61. package/build/bdd-framework/snippets/index.js +3 -7
  62. package/build/bdd-framework/stepDefinitions/defineStep.js +1 -1
  63. package/build/bdd-framework/utils/AutofillMap.js +20 -0
  64. package/build/bdd-framework/utils/index.js +23 -0
  65. package/build/bdd-framework/utils/stripAnsiEscapes.js +20 -0
  66. package/build/core/playwright/builtInFixtures/cacheLayer.js +1 -0
  67. package/build/core/playwright/builtInFixtures/context.js +18 -1
  68. package/build/core/playwright/builtInFixtures/i18N.js +33 -0
  69. package/build/core/playwright/builtInFixtures/index.js +17 -1
  70. package/build/core/playwright/builtInFixtures/page.js +69 -39
  71. package/build/core/playwright/builtInFixtures/unauthenticatedPage.js +18 -0
  72. package/build/core/playwright/clear-caches.js +19 -8
  73. package/build/core/playwright/codegen.js +4 -4
  74. package/build/core/playwright/constants/browserTypes.js +12 -0
  75. package/build/core/playwright/custom-commands.js +1 -1
  76. package/build/core/playwright/env-initializer.js +10 -6
  77. package/build/core/playwright/helpers/auth/accountLogin.js +18 -0
  78. package/build/core/playwright/helpers/auth/checkAuthCookies.js +47 -0
  79. package/build/core/playwright/helpers/auth/getUrlOrigin.js +13 -0
  80. package/build/core/playwright/helpers/auth/getUsers.js +72 -0
  81. package/build/core/playwright/helpers/auth/index.js +58 -0
  82. package/build/core/playwright/helpers/auth/loginSteps.js +36 -0
  83. package/build/core/playwright/helpers/configFileNameProvider.js +17 -0
  84. package/build/core/playwright/helpers/getUserFixtures.js +23 -0
  85. package/build/core/playwright/helpers/mergeObjects.js +13 -0
  86. package/build/core/playwright/helpers/parseUserArgs.js +11 -0
  87. package/build/core/playwright/index.js +51 -1
  88. package/build/core/playwright/readConfigFile.js +23 -12
  89. package/build/core/playwright/report-generator.js +7 -7
  90. package/build/core/playwright/setup/config-creator.js +4 -1
  91. package/build/core/playwright/setup/config-utils.js +43 -7
  92. package/build/core/playwright/setup/custom-reporter.js +3 -2
  93. package/build/core/playwright/test-runner.js +19 -8
  94. package/build/core/playwright/types.js +43 -0
  95. package/build/index.d.ts +21 -2
  96. package/build/index.js +48 -11
  97. package/build/lib/cli.js +12 -3
  98. package/build/lib/post-install.js +18 -10
  99. package/build/setup-folder-structure/helper.js +3 -0
  100. package/build/utils/cliArgsToObject.js +5 -1
  101. package/build/utils/fileUtils.js +3 -0
  102. package/build/utils/rootPath.js +16 -9
  103. package/changelog.md +13 -0
  104. package/npm-shrinkwrap.json +18 -12
  105. package/package.json +6 -4
  106. package/build/bdd-framework/config/dir.js +0 -27
  107. package/build/bdd-framework/cucumber/loadSources.js +0 -57
  108. /package/build/bdd-framework/cucumber/{gherkin.d.js → types.js} +0 -0
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.resovleFeaturePaths = resovleFeaturePaths;
8
+ var _fs = _interopRequireDefault(require("fs"));
9
+ var _path = _interopRequireDefault(require("path"));
10
+ var _environment = require("@cucumber/cucumber/lib/api/environment");
11
+ var _console_logger = require("@cucumber/cucumber/lib/api/console_logger");
12
+ var _utils = require("../utils");
13
+ /**
14
+ * Representation of https://github.com/cucumber/cucumber-js/blob/main/src/paths/paths.ts
15
+ * Since Cucumber 10.1 resolvePaths was moved from lib/api/paths to lib/paths/paths.
16
+ * This module makes resolvePaths working with any version of Cucumber.
17
+ * See: https://github.com/cucumber/cucumber-js/pull/2361/files
18
+ *
19
+ * todo: replace with own paths resolution.
20
+ */
21
+
22
+ /**
23
+ * Returns list of absolute feature paths.
24
+ */
25
+ // eslint-disable-next-line max-statements
26
+ async function resovleFeaturePaths(runConfiguration, environment = {}) {
27
+ const {
28
+ cwd,
29
+ stderr,
30
+ debug
31
+ } = (0, _environment.mergeEnvironment)(environment);
32
+ const logger = new _console_logger.ConsoleLogger(stderr, debug);
33
+ const cucumberRoot = (0, _utils.resolvePackageRoot)('@cucumber/cucumber');
34
+ const pathsModuleBefore10_1 = _path.default.join(cucumberRoot, 'lib/api/paths.js');
35
+ const pathsModuleAfter10_1 = _path.default.join(cucumberRoot, 'lib/paths/paths.js');
36
+ const isNewResolvePaths = _fs.default.existsSync(pathsModuleAfter10_1);
37
+ if (isNewResolvePaths) {
38
+ const {
39
+ resolvePaths
40
+ } = require(pathsModuleAfter10_1);
41
+ const {
42
+ sourcePaths,
43
+ unexpandedSourcePaths
44
+ } = await resolvePaths(logger, cwd, runConfiguration.sources);
45
+ return {
46
+ featurePaths: sourcePaths,
47
+ unexpandedFeaturePaths: unexpandedSourcePaths
48
+ };
49
+ } else {
50
+ const {
51
+ resolvePaths
52
+ } = require(pathsModuleBefore10_1);
53
+ const {
54
+ featurePaths,
55
+ unexpandedFeaturePaths
56
+ } = await resolvePaths(logger, cwd, runConfiguration.sources);
57
+ return {
58
+ featurePaths,
59
+ unexpandedFeaturePaths
60
+ };
61
+ }
62
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.parseStepArgument = parseStepArgument;
8
+ var _util = _interopRequireDefault(require("util"));
9
+ var _valueChecker = require("./valueChecker");
10
+ /**
11
+ * Based on: https://github.com/cucumber/cucumber-js/blob/main/src/step_arguments.ts
12
+ */
13
+
14
+ function parseStepArgument(arg, mapping) {
15
+ if ((0, _valueChecker.doesHaveValue)(arg.dataTable)) {
16
+ return mapping.dataTable(arg.dataTable);
17
+ } else if ((0, _valueChecker.doesHaveValue)(arg.docString)) {
18
+ return mapping.docString(arg.docString);
19
+ }
20
+ throw new Error(`Unknown step argument: ${_util.default.inspect(arg)}`);
21
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.doesHaveValue = doesHaveValue;
7
+ exports.doesNotHaveValue = doesNotHaveValue;
8
+ exports.valueOrDefault = valueOrDefault;
9
+ /**
10
+ * Based on: https://github.com/cucumber/cucumber-js/blob/main/src/value_checker.ts
11
+ */
12
+ function doesHaveValue(value) {
13
+ return !doesNotHaveValue(value);
14
+ }
15
+ function doesNotHaveValue(value) {
16
+ return value === null || value === undefined;
17
+ }
18
+ function valueOrDefault(value, defaultValue) {
19
+ if (doesHaveValue(value)) {
20
+ return value;
21
+ }
22
+ return defaultValue;
23
+ }
@@ -5,26 +5,27 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.Formatter = void 0;
7
7
  var _jsStringWrap = require("../utils/jsStringWrap");
8
+ var _utils = require("../utils");
8
9
  /**
9
10
  * Helper to format Playwright test file.
10
11
  */
11
12
 
12
- const TAGS_FIXTURE_TEST_KEY_SEPARATOR = '|';
13
13
  class Formatter {
14
14
  config;
15
15
  constructor(config) {
16
16
  this.config = config;
17
17
  }
18
- fileHeader(uri, importTestFrom) {
19
- const file = (importTestFrom === null || importTestFrom === void 0 ? void 0 : importTestFrom.file) || '@zohodesk/testinglibrary';
18
+ fileHeader(featureUri, importTestFrom) {
19
+ // always use "/" for imports, see #91
20
+ const importTestFromFile = (0, _utils.toPosixPath)((importTestFrom === null || importTestFrom === void 0 ? void 0 : importTestFrom.file) || '@zohodesk/testinglibrary');
20
21
  let varName = (importTestFrom === null || importTestFrom === void 0 ? void 0 : importTestFrom.varName) || 'test';
21
22
  if (varName !== 'test') {
22
23
  varName = `${varName} as test`;
23
24
  }
24
- return [`/** Generated from: ${uri} */`,
25
+ return [`/** Generated from: ${featureUri} */`,
25
26
  // prettier-ignore
26
27
  // this.quoted() is not possible for 'import from' as backticks not parsed
27
- `import { ${varName} } from ${JSON.stringify(file)};`, ''];
28
+ `import { ${varName} } from ${JSON.stringify(importTestFromFile)};`, ''];
28
29
  }
29
30
  suite(node, children) {
30
31
  const firstLine = `test.describe${this.getSubFn(node)}(${this.quoted(node.title)}, () => {`;
@@ -40,7 +41,7 @@ class Formatter {
40
41
  }
41
42
  test(node, fixtures, children) {
42
43
  const fixturesStr = [...fixtures].join(', ');
43
- const title = this.quoted([node.title, ...node.tags].join(' '));
44
+ const title = this.quoted(node.title);
44
45
  const firstLine = `test${this.getSubFn(node)}(${title}, async ({ ${fixturesStr} }) => {`;
45
46
  if (!children.length) {
46
47
  return [`${firstLine}});`, ''];
@@ -58,13 +59,10 @@ class Formatter {
58
59
  missingStep(keyword, text) {
59
60
  return `// missing step: ${keyword}(${this.quoted(text)});`;
60
61
  }
61
- useFixtures(fixtures) {
62
- return fixtures.length > 0 ? ['// == technical section ==',
62
+ technicalSection(testMetaBuilder, featureUri, fixtures) {
63
+ return ['// == technical section ==',
63
64
  // prettier-ignore
64
- '', 'test.use({', ...fixtures.map(indent), '});'] : [];
65
- }
66
- testFixture() {
67
- return ['$test: ({}, use) => use(test),'];
65
+ '', 'test.use({', ...['$test: ({}, use) => use(test),', '$testMetaMap: ({}, use) => use(testMetaMap),', `$uri: ({}, use) => use(${this.quoted(featureUri)}),`, ...fixtures].map(indent), '});', '', 'const testMetaMap = {', ...testMetaBuilder.getObjectLines().map(indent), '};'];
68
66
  }
69
67
  bddWorldFixtures() {
70
68
  const fixturesObj = {
@@ -77,16 +75,6 @@ class Formatter {
77
75
  const fixtures = Object.keys(fixturesObj).join(', ');
78
76
  return [`$bddWorldFixtures: ({ ${fixtures} }, use) => use({ ${fixtures} }),`];
79
77
  }
80
- tagsFixture(testNodes) {
81
- const lines = testNodes.filter(node => node.tags.length).map(node => {
82
- // remove first parent as it is the same for all tests: root suite
83
- const key = `${node.titlePath.slice(1).join(TAGS_FIXTURE_TEST_KEY_SEPARATOR)} ${node.tags.join(" ")}`;
84
- return `${JSON.stringify(key)}: ${JSON.stringify(node.tags)},`;
85
- });
86
- return lines.length > 0 ? ['$tags: ({}, use, testInfo) => use({', ...lines.map(indent),
87
- // .slice(2) -> b/c we remove filename and root suite title
88
- `}[testInfo.titlePath.slice(2).join(${JSON.stringify(TAGS_FIXTURE_TEST_KEY_SEPARATOR)})] || []),`] : [];
89
- }
90
78
  scenarioHookFixtures(fixtureNames) {
91
79
  if (!fixtureNames.length) {
92
80
  return [];
@@ -120,7 +108,7 @@ class Formatter {
120
108
  /**
121
109
  * Apply this function only to string literals (mostly titles here).
122
110
  * Objects and arrays are handled with JSON.strinigfy,
123
- * b/c object keys can't be in backtiks.
111
+ * b/c object keys can't be in backticks.
124
112
  * See: https://stackoverflow.com/questions/33194138/template-string-as-object-property-name
125
113
  */
126
114
  quoted(str) {
@@ -16,11 +16,12 @@ var _config = require("../config");
16
16
  var _snippets = require("../snippets");
17
17
  var _steps = require("../stepDefinitions/decorators/steps");
18
18
  var _transform = require("../playwright/transform");
19
- var _dir = require("../config/dir");
19
+ var _configDir = require("../config/configDir");
20
20
  var _logger = require("../utils/logger");
21
21
  var _tagExpressions = _interopRequireDefault(require("@cucumber/tag-expressions"));
22
22
  var _exit = require("../utils/exit");
23
23
  var _createBdd = require("../stepDefinitions/createBdd");
24
+ var _resolveFeaturePaths = require("../cucumber/resolveFeaturePaths");
24
25
  /**
25
26
  * Generate playwright test files from Gherkin documents.
26
27
  */
@@ -29,7 +30,7 @@ class TestFilesGenerator {
29
30
  config;
30
31
  // all these props are exist
31
32
  runConfiguration;
32
- features;
33
+ featuresLoader = new _loadFeatures.FeaturesLoader();
33
34
  supportCodeLibrary;
34
35
  files = [];
35
36
  tagsExpression;
@@ -61,7 +62,7 @@ class TestFilesGenerator {
61
62
  }
62
63
  async loadCucumberConfig() {
63
64
  const environment = {
64
- cwd: (0, _dir.getPlaywrightConfigDir)()
65
+ cwd: (0, _configDir.getPlaywrightConfigDir)()
65
66
  };
66
67
  const {
67
68
  runConfiguration
@@ -72,12 +73,23 @@ class TestFilesGenerator {
72
73
  this.warnForTsNodeRegister();
73
74
  }
74
75
  async loadFeatures() {
75
- const environment = {
76
- cwd: (0, _dir.getPlaywrightConfigDir)()
77
- };
78
- this.logger.log(`Loading features from: ${this.runConfiguration.sources.paths.join(', ')}`);
79
- this.features = await (0, _loadFeatures.loadFeatures)(this.runConfiguration, environment);
80
- this.logger.log(`Loaded features: ${this.features.size}`);
76
+ const cwd = (0, _configDir.getPlaywrightConfigDir)();
77
+ const {
78
+ paths,
79
+ defaultDialect
80
+ } = this.runConfiguration.sources;
81
+ this.logger.log(`Loading features from: ${paths.join(', ')}`);
82
+ const {
83
+ featurePaths
84
+ } = await (0, _resolveFeaturePaths.resovleFeaturePaths)(this.runConfiguration, {
85
+ cwd
86
+ });
87
+ await this.featuresLoader.load(featurePaths, {
88
+ relativeTo: cwd,
89
+ defaultDialect
90
+ });
91
+ this.handleParseErrors();
92
+ this.logger.log(`Loaded features: ${this.featuresLoader.getDocumentsCount()}`);
81
93
  }
82
94
  async loadSteps() {
83
95
  const {
@@ -86,7 +98,7 @@ class TestFilesGenerator {
86
98
  } = this.runConfiguration.support;
87
99
  this.logger.log(`Loading steps from: ${requirePaths.concat(importPaths).join(', ')}`);
88
100
  const environment = {
89
- cwd: (0, _dir.getPlaywrightConfigDir)()
101
+ cwd: (0, _configDir.getPlaywrightConfigDir)()
90
102
  };
91
103
  this.supportCodeLibrary = await (0, _loadSteps.loadSteps)(this.runConfiguration, environment);
92
104
  await this.loadDecoratorSteps();
@@ -104,22 +116,20 @@ class TestFilesGenerator {
104
116
  }
105
117
  }
106
118
  buildFiles() {
107
- this.files = [...this.features.entries()].map(([doc, pickles]) => {
119
+ this.files = this.featuresLoader.getDocumentsWithPickles().map(gherkinDocument => {
108
120
  return new _testFile.TestFile({
109
- doc,
110
- pickles,
121
+ gherkinDocument,
111
122
  supportCodeLibrary: this.supportCodeLibrary,
112
- outputPath: this.getOutputPath(doc),
123
+ // doc.uri is always relative to cwd (coming after cucumber handling)
124
+ // see: https://github.com/cucumber/cucumber-js/blob/main/src/api/gherkin.ts#L51
125
+ outputPath: this.getSpecPathByFeaturePath(gherkinDocument.uri),
113
126
  config: this.config,
114
127
  tagsExpression: this.tagsExpression
115
128
  }).build();
116
- }).filter(file => file.testNodes.length > 0);
129
+ }).filter(file => file.testCount > 0);
117
130
  }
118
- getOutputPath(doc) {
119
- const configDir = (0, _dir.getPlaywrightConfigDir)();
120
- // doc.uri is always relative to cwd (coming after cucumber handling)
121
- // see: https://github.com/cucumber/cucumber-js/blob/main/src/api/gherkin.ts#L51
122
- const relFeaturePath = doc.uri;
131
+ getSpecPathByFeaturePath(relFeaturePath) {
132
+ const configDir = (0, _configDir.getPlaywrightConfigDir)();
123
133
  const absFeaturePath = _path.default.resolve(configDir, relFeaturePath);
124
134
  const relOutputPath = _path.default.relative(this.config.featuresRoot, absFeaturePath);
125
135
  if (relOutputPath.startsWith('..')) {
@@ -160,5 +170,16 @@ class TestFilesGenerator {
160
170
  this.logger.warn(`WARNING: usage of requireModule: ['ts-node/register'] is not recommended for playwright-bdd.`, `Remove this option from defineBddConfig() and`, `Playwright's built-in loader will be used to compile TypeScript step definitions.`);
161
171
  }
162
172
  }
173
+ handleParseErrors() {
174
+ const {
175
+ parseErrors
176
+ } = this.featuresLoader;
177
+ if (parseErrors.length) {
178
+ const message = parseErrors.map(parseError => {
179
+ return `Parse error in "${parseError.source.uri}" ${parseError.message}`;
180
+ }).join('\n');
181
+ (0, _exit.exit)(message);
182
+ }
183
+ }
163
184
  }
164
185
  exports.TestFilesGenerator = TestFilesGenerator;
@@ -20,8 +20,9 @@ var _fixtures = require("./fixtures");
20
20
  var _scenario = require("../hooks/scenario");
21
21
  var _worker = require("../hooks/worker");
22
22
  var _lang = require("../config/lang");
23
+ var _testMeta = require("./testMeta");
23
24
  /**
24
- * Generate test code.
25
+ * Generate Playwright test file for feature.
25
26
  */
26
27
 
27
28
  class TestFile {
@@ -29,29 +30,29 @@ class TestFile {
29
30
  lines = [];
30
31
  i18nKeywordsMap;
31
32
  formatter;
33
+ testMetaBuilder;
32
34
  hasCucumberStyle = false;
33
- testNodes = [];
34
35
  hasCustomTest = false;
35
36
  undefinedSteps = [];
37
+ featureUri;
36
38
  constructor(options) {
37
39
  this.options = options;
38
40
  this.formatter = new _formatter.Formatter(options.config);
41
+ this.testMetaBuilder = new _testMeta.TestMetaBuilder();
42
+ this.featureUri = this.getFeatureUri();
39
43
  }
40
- get sourceFile() {
41
- const {
42
- uri
43
- } = this.options.doc;
44
- if (!uri) {
45
- throw new Error(`Document without uri`);
46
- }
47
- return uri;
44
+ get gherkinDocument() {
45
+ return this.options.gherkinDocument;
46
+ }
47
+ get pickles() {
48
+ return this.gherkinDocument.pickles;
48
49
  }
49
50
  get content() {
50
51
  return this.lines.join('\n');
51
52
  }
52
53
  get language() {
53
- var _this$options$doc$fea;
54
- return ((_this$options$doc$fea = this.options.doc.feature) === null || _this$options$doc$fea === void 0 ? void 0 : _this$options$doc$fea.language) || _lang.LANG_EN;
54
+ var _this$gherkinDocument;
55
+ return ((_this$gherkinDocument = this.gherkinDocument.feature) === null || _this$gherkinDocument === void 0 ? void 0 : _this$gherkinDocument.language) || _lang.LANG_EN;
55
56
  }
56
57
  get isEnglish() {
57
58
  return (0, _lang.isEnglish)(this.language);
@@ -62,11 +63,14 @@ class TestFile {
62
63
  get outputPath() {
63
64
  return this.options.outputPath;
64
65
  }
66
+ get testCount() {
67
+ return this.testMetaBuilder.testCount;
68
+ }
65
69
  build() {
66
70
  this.loadI18nKeywords();
67
71
  this.lines = [...this.getFileHeader(),
68
72
  // prettier-ignore
69
- ...this.getRootSuite(), ...this.getFileFixtures()];
73
+ ...this.getRootSuite(), ...this.getTechnicalSection()];
70
74
  return this;
71
75
  }
72
76
  save() {
@@ -80,13 +84,23 @@ class TestFile {
80
84
  }
81
85
  getFileHeader() {
82
86
  const importTestFrom = this.getRelativeImportTestFrom();
83
- return this.formatter.fileHeader(this.sourceFile, importTestFrom);
87
+ return this.formatter.fileHeader(this.featureUri, importTestFrom);
84
88
  }
85
89
  loadI18nKeywords() {
86
90
  if (!this.isEnglish) {
87
91
  this.i18nKeywordsMap = (0, _i18n.getKeywordsMap)(this.language);
88
92
  }
89
93
  }
94
+ getFeatureUri() {
95
+ const {
96
+ uri
97
+ } = this.gherkinDocument;
98
+ if (!uri) {
99
+ var _this$gherkinDocument2;
100
+ throw new Error(`Document without uri: ${(_this$gherkinDocument2 = this.gherkinDocument.feature) === null || _this$gherkinDocument2 === void 0 ? void 0 : _this$gherkinDocument2.name}`);
101
+ }
102
+ return uri;
103
+ }
90
104
  getRelativeImportTestFrom() {
91
105
  const {
92
106
  importTestFrom
@@ -104,13 +118,13 @@ class TestFile {
104
118
  varName
105
119
  };
106
120
  }
107
- getFileFixtures() {
108
- return this.formatter.useFixtures([...this.formatter.testFixture(), ...(!this.isEnglish ? this.formatter.langFixture(this.language) : []), ...((0, _scenario.hasScenarioHooks)() || this.hasCucumberStyle ? this.formatter.bddWorldFixtures() : []), ...this.formatter.scenarioHookFixtures((0, _scenario.getScenarioHooksFixtures)()), ...this.formatter.workerHookFixtures((0, _worker.getWorkerHooksFixtures)()), ...this.formatter.tagsFixture(this.testNodes)]);
121
+ getTechnicalSection() {
122
+ return this.formatter.technicalSection(this.testMetaBuilder, this.featureUri, [...(!this.isEnglish ? this.formatter.langFixture(this.language) : []), ...((0, _scenario.hasScenarioHooks)() || this.hasCucumberStyle ? this.formatter.bddWorldFixtures() : []), ...this.formatter.scenarioHookFixtures((0, _scenario.getScenarioHooksFixtures)()), ...this.formatter.workerHookFixtures((0, _worker.getWorkerHooksFixtures)())]);
109
123
  }
110
124
  getRootSuite() {
111
125
  const {
112
126
  feature
113
- } = this.options.doc;
127
+ } = this.gherkinDocument;
114
128
  if (!feature) {
115
129
  throw new Error(`Document without feature.`);
116
130
  }
@@ -189,10 +203,11 @@ class TestFile {
189
203
  if (this.skipByTagsExpression(node)) {
190
204
  return [];
191
205
  }
206
+ const pickle = this.findPickle(scenario, exampleRow);
207
+ this.testMetaBuilder.registerTest(node, pickle);
192
208
  if (node.isSkipped()) {
193
209
  return this.formatter.test(node, new Set(), []);
194
210
  }
195
- this.testNodes.push(node);
196
211
  const {
197
212
  fixtures,
198
213
  lines
@@ -207,10 +222,11 @@ class TestFile {
207
222
  if (this.skipByTagsExpression(node)) {
208
223
  return [];
209
224
  }
225
+ const pickle = this.findPickle(scenario);
226
+ this.testMetaBuilder.registerTest(node, pickle);
210
227
  if (node.isSkipped()) {
211
228
  return this.formatter.test(node, new Set(), []);
212
229
  }
213
- this.testNodes.push(node);
214
230
  const {
215
231
  fixtures,
216
232
  lines
@@ -272,8 +288,8 @@ class TestFile {
272
288
  */
273
289
  // eslint-disable-next-line max-statements, complexity
274
290
  getStep(step, previousKeywordType, outlineExampleRowId) {
275
- const pickleStep = this.getPickleStep(step, outlineExampleRowId);
276
- const stepDefinition = (0, _loadSteps.findStepDefinition)(this.options.supportCodeLibrary, pickleStep.text, this.sourceFile);
291
+ const pickleStep = this.findPickleStep(step, outlineExampleRowId);
292
+ const stepDefinition = (0, _loadSteps.findStepDefinition)(this.options.supportCodeLibrary, pickleStep.text, this.featureUri);
277
293
  const keywordType = (0, _index.getStepKeywordType)({
278
294
  keyword: step.keyword,
279
295
  language: this.language,
@@ -317,13 +333,40 @@ class TestFile {
317
333
  stepConfig: undefined
318
334
  };
319
335
  }
320
- getPickleStep(step, outlineExampleRowId) {
321
- for (const pickle of this.options.pickles) {
336
+ /**
337
+ * Returns pickle for scenario.
338
+ * Pickle is executable entity including background and steps with example values.
339
+ */
340
+ findPickle(scenario, exampleRow) {
341
+ const pickle = this.pickles.find(pickle => {
342
+ const hasScenarioId = pickle.astNodeIds.includes(scenario.id);
343
+ const hasExampleRowId = !exampleRow || pickle.astNodeIds.includes(exampleRow.id);
344
+ return hasScenarioId && hasExampleRowId;
345
+ });
346
+ if (!pickle) {
347
+ throw new Error(`Pickle not found for scenario: ${scenario.name}`);
348
+ }
349
+ return pickle;
350
+ }
351
+ /**
352
+ * Returns pickleStep for ast step.
353
+ * PickleStep contains step text with inserted example values.
354
+ *
355
+ * Note:
356
+ * When searching for pickleStep iterate all pickles in a file
357
+ * b/c for background steps there is no own pickle.
358
+ * This can be optimized: pass optional 'pickle' parameter
359
+ * and search only inside it if it exists.
360
+ * But this increases code complexity, and performance impact seems to be minimal
361
+ * b/c number of pickles inside feature file is not very big.
362
+ */
363
+ findPickleStep(step, exampleRowId) {
364
+ for (const pickle of this.pickles) {
322
365
  const pickleStep = pickle.steps.find(({
323
366
  astNodeIds
324
367
  }) => {
325
368
  const hasStepId = astNodeIds.includes(step.id);
326
- const hasRowId = !outlineExampleRowId || astNodeIds.includes(outlineExampleRowId);
369
+ const hasRowId = !exampleRowId || astNodeIds.includes(exampleRowId);
327
370
  return hasStepId && hasRowId;
328
371
  });
329
372
  if (pickleStep) {
@@ -359,7 +402,7 @@ class TestFile {
359
402
  if (resolvedFixtures.length !== 1) {
360
403
  const suggestedTags = resolvedFixtures.filter(f => !f.byTag).map(f => (0, _testPoms.buildFixtureTag)(f.name)).join(', ');
361
404
  const suggestedTagsStr = suggestedTags.length ? ` or set one of the following tags: ${suggestedTags}` : '.';
362
- (0, _exit.exit)(`Can't guess fixture for decorator step "${pickleStep.text}" in file: ${this.sourceFile}.`, `Please refactor your Page Object classes${suggestedTagsStr}`);
405
+ (0, _exit.exit)(`Can't guess fixture for decorator step "${pickleStep.text}" in file: ${this.featureUri}.`, `Please refactor your Page Object classes${suggestedTagsStr}`);
363
406
  }
364
407
  const fixtureName = resolvedFixtures[0].name;
365
408
  return {
@@ -389,7 +432,7 @@ class TestFile {
389
432
  line
390
433
  } = examples.location;
391
434
  const titleFormatCommentLine = line - 1;
392
- const comment = this.options.doc.comments.find(c => {
435
+ const comment = this.gherkinDocument.comments.find(c => {
393
436
  return c.location.line === titleFormatCommentLine;
394
437
  });
395
438
  const commentText = comment === null || comment === void 0 || (_comment$text = comment.text) === null || _comment$text === void 0 ? void 0 : _comment$text.trim();
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TestMetaBuilder = void 0;
7
+ exports.getTestMeta = getTestMeta;
8
+ var _utils = require("../utils");
9
+ /**
10
+ * Class to build and print testMeta object containing meta info about each test.
11
+ * Tests are identified by special key constructed from title path.
12
+ *
13
+ * Example:
14
+ * const testMeta = {
15
+ * "Simple scenario": { pickleLocation: "3:10", tags: ["@foo"] },
16
+ * "Scenario with examples|Example #1": { pickleLocation: "8:26", tags: [] },
17
+ * "Rule 1|Scenario with examples|Example #1": { pickleLocation: "9:42", tags: [] },
18
+ * };
19
+ */
20
+
21
+ const TEST_KEY_SEPARATOR = '|';
22
+ class TestMetaBuilder {
23
+ tests = [];
24
+ get testCount() {
25
+ return this.tests.length;
26
+ }
27
+ registerTest(node, pickle) {
28
+ const testMeta = {
29
+ pickleLocation: (0, _utils.stringifyLocation)(pickle.location),
30
+ tags: node.tags.length ? node.tags : undefined
31
+ };
32
+ this.tests.push({
33
+ node,
34
+ testMeta
35
+ });
36
+ }
37
+ getObjectLines() {
38
+ // build object line by line to have each test on a separate line,
39
+ // but value should be in one line.
40
+ return this.tests.map(test => {
41
+ const testKey = this.getTestKey(test.node);
42
+ return `${JSON.stringify(testKey)}: ${JSON.stringify(test.testMeta)},`;
43
+ });
44
+ }
45
+ getTestKey(node) {
46
+ // .slice(1) -> b/c we remove top describe title (it's same for all tests)
47
+ return node.titlePath.slice(1).join(TEST_KEY_SEPARATOR);
48
+ }
49
+ }
50
+ exports.TestMetaBuilder = TestMetaBuilder;
51
+ function getTestMeta(testMetaMap, testInfo) {
52
+ // .slice(2) -> b/c we remove filename and top describe title
53
+ const key = testInfo.titlePath.slice(2).join(TEST_KEY_SEPARATOR);
54
+ const testMeta = testMetaMap[key];
55
+ if (!testMeta) {
56
+ // throw new Error(`Can't find testMeta for key "${key}"`);
57
+ return {};
58
+ }
59
+ return testMeta;
60
+ }
@@ -6,39 +6,38 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.TestNode = void 0;
7
7
  var _utils = require("../utils");
8
8
  /**
9
- * Universal TestNode class of parent-child relations in test file structure.
10
- * Holds tags and titles path.
9
+ * Universal TestNode class representing test or suite in a test file.
10
+ * Holds parent-child links.
11
+ * Allows to inherit tags and titles path.
11
12
  */
12
13
 
13
14
  const SPECIAL_TAGS = ['@only', '@skip', '@fixme'];
14
15
  class TestNode {
15
16
  title;
16
17
  titlePath;
17
- ownTags = [];
18
- tags = [];
18
+ ownTags;
19
+ tags;
19
20
  flags = {};
20
21
  constructor(gherkinNode, parent) {
21
- this.initOwnTags(gherkinNode);
22
- this.tags = (0, _utils.removeDuplicates)(((parent === null || parent === void 0 ? void 0 : parent.tags) || []).concat(this.ownTags));
23
22
  this.title = gherkinNode.name;
24
23
  this.titlePath = ((parent === null || parent === void 0 ? void 0 : parent.titlePath) || []).concat([this.title]);
24
+ this.ownTags = (0, _utils.removeDuplicates)(getTagNames(gherkinNode.tags));
25
+ this.tags = (0, _utils.removeDuplicates)(((parent === null || parent === void 0 ? void 0 : parent.tags) || []).concat(this.ownTags));
26
+ this.initFlags();
25
27
  }
26
28
  isSkipped() {
27
29
  return this.flags.skip || this.flags.fixme;
28
30
  }
29
- initOwnTags(gherkinNode) {
30
- const tagNames = (0, _utils.removeDuplicates)(getTagNames(gherkinNode.tags));
31
- tagNames.forEach(tag => {
31
+ initFlags() {
32
+ this.ownTags.forEach(tag => {
32
33
  if (isSpecialTag(tag)) {
33
34
  this.setFlag(tag);
34
- } else {
35
- this.ownTags.push(tag);
36
35
  }
37
36
  });
38
37
  }
39
38
  // eslint-disable-next-line complexity
40
39
  setFlag(tag) {
41
- // in case of several system tags, @only takes precendence
40
+ // in case of several special tags, @only takes precendence
42
41
  if (tag === '@only') {
43
42
  this.flags = {
44
43
  only: true