@digital-commons-official/codeceptjs-storyboard 0.4.12 → 0.4.13

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.
package/README.md CHANGED
@@ -169,10 +169,20 @@ Background steps and outline examples appear in execution order. Each example ro
169
169
 
170
170
  `# Chapter`, `# Note` and `# Copy` remain the annotation keys in every language; outline values are substituted in them too. Text inside doc strings and data tables does not become an extra card or annotation.
171
171
 
172
- Invalid syntax or an unknown dialect produces an error naming the feature file. The parser also reads rule backgrounds, but **CodeceptJS 4.2.0 does not execute scenarios nested in `Rule`**. Runner support is tracked in [#8](https://gitlab.com/digital-commons/software/libraries/npm/codeceptjs-storyboard/-/issues/8).
172
+ Invalid syntax or an unknown dialect produces an error naming the feature file.
173
+ The enabled plugin completes CodeceptJS's Gherkin loader for `Rule` scenarios,
174
+ including localized keywords such as `Règle`. The feature background runs before
175
+ each case; a rule background runs only for cases in that rule. Outline examples
176
+ remain separate tests and rule tags can be selected with `--grep`. Repeated
177
+ scenario titles in different rules keep their own source annotations.
178
+ An empty selection fails clearly instead of succeeding with zero tests.
173
179
 
174
180
  The [multilingual storyboard](tests/e2e/storyboards/05-languages/storyboard-languages.svg) runs English, French and German examples and shows a real visual regression on a French sentence.
175
181
 
182
+ The [rule storyboard](tests/e2e/storyboards/07-rules/storyboard-rules.svg) executes
183
+ two French outline examples and a second rule, checks both backgrounds and the
184
+ tag filter, and proves the exact sentence of a deliberate visual regression.
185
+
176
186
  ### What you get
177
187
 
178
188
  - On every run the SVG of the scenario is written under `_output/<feature-dir>/<scenario-tag>.svg`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digital-commons-official/codeceptjs-storyboard",
3
- "version": "0.4.12",
3
+ "version": "0.4.13",
4
4
  "description": "Storyboard plugin for CodeceptJS: one Gherkin sentence = one card = one pixel baseline, and every scenario renders itself as a single SVG storyboard",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
package/src/index.js CHANGED
@@ -780,11 +780,14 @@ function logVisualRegression (frame, outputSvg) {
780
780
  // (feature name) in lib/mocha/gherkin.js; titles carry the tags appended.
781
781
  // ---------------------------------------------------------------------------
782
782
  module.exports = function storyboardPlugin (config = {}) {
783
- const { event, recorder } = require('codeceptjs')
783
+ const { event, recorder, container } = require('codeceptjs')
784
784
  const replayCommand = config.replayCommand ?? 'npx codeceptjs run --steps --grep "{tag}"'
785
785
  const writeGallery = require('./gallery')
786
786
  const entries = new Map()
787
787
  let themePromise
788
+ event.dispatcher.prependListener(event.all.before, () => {
789
+ require('./rules')(container.mocha())
790
+ })
788
791
  event.dispatcher.on(event.all.before, () => {
789
792
  entries.clear()
790
793
  themePromise = null
@@ -815,7 +818,7 @@ module.exports = function storyboardPlugin (config = {}) {
815
818
  scenario: stripTags(test.title),
816
819
  file,
817
820
  featureFile: test.file,
818
- occurrence: Math.max(0, (test.parent.tests || []).filter(other => other.title === test.title).indexOf(test)),
821
+ occurrence: Math.max(0, (test.parent.tests || []).filter(other => stripTags(other.title) === stripTags(test.title)).indexOf(test)),
819
822
  baseDir: base,
820
823
  rerun: tag ? replayCommand.replaceAll('{tag}', tag) : ''
821
824
  })
package/src/rules.js ADDED
@@ -0,0 +1,71 @@
1
+ const fs = require('node:fs')
2
+ const { randomUUID } = require('node:crypto')
3
+ const { AstBuilder, GherkinClassicTokenMatcher, Parser, compile } = require('@cucumber/gherkin')
4
+
5
+ function ruleSource (source, feature, rule, next) {
6
+ const lines = source.split('\n')
7
+ const end = next ? Math.min(next.location.line, ...next.tags.map(tag => tag.location.line)) - 1 : lines.length
8
+ const tags = [...feature.tags, ...rule.tags].map(tag => tag.name).join(' ')
9
+ return `# language: ${feature.language}\n${tags}\n${feature.keyword}: ${feature.name} / ${rule.name}\n` + lines.slice(rule.location.line, end).join('\n')
10
+ }
11
+
12
+ function appendRule (suite, parsed, rule) {
13
+ // Reuse CodeceptJS's wrapped tests and BDD hooks without wrapping them twice.
14
+ // The original feature hook runs first; this hook belongs only to this rule.
15
+ parsed.ctx = suite.ctx
16
+ const tests = new Set(parsed.tests)
17
+ const background = parsed._beforeEach.find(hook => hook.title.endsWith(': Before'))
18
+ if (background) {
19
+ suite.beforeEach(`storyboard rule: ${rule.name}`, function (done) {
20
+ if (tests.has(this.currentTest)) return background.fn.call(this, done)
21
+ done()
22
+ })
23
+ }
24
+ for (const test of parsed.tests) {
25
+ test.title = `${test.title} ${rule.tags.map(tag => tag.name).join(' ')}`.trim()
26
+ test.fullTitle = () => `${suite.title}: ${test.title}`
27
+ suite.addTest(test)
28
+ }
29
+ }
30
+
31
+ function completeFeature (suite) {
32
+ const source = fs.readFileSync(suite.file, 'utf8')
33
+ const parser = new Parser(new AstBuilder(randomUUID), new GherkinClassicTokenMatcher())
34
+ const document = parser.parse(source)
35
+ const expected = compile(document, suite.file, randomUUID).length
36
+ if (suite.tests.length === expected) return // A future runner may support rules itself.
37
+ const rules = document.feature.children.filter(child => child.rule).map(child => child.rule)
38
+ const module = require('codeceptjs/lib/mocha/gherkin')
39
+ const parse = module.default || module
40
+ for (const [index, rule] of rules.entries()) {
41
+ appendRule(suite, parse(ruleSource(source, document.feature, rule, rules[index + 1]), suite.file), rule)
42
+ }
43
+ if (suite.tests.length !== expected) throw new Error(`storyboard: runner loaded ${suite.tests.length} of ${expected} scenarios from ${suite.file}`)
44
+ }
45
+
46
+ function completeRules (mocha) {
47
+ for (const suite of mocha.suite.suites) {
48
+ if (suite.feature) completeFeature(suite)
49
+ }
50
+ const grep = mocha.options.grep || /.*/
51
+ const pattern = grep instanceof RegExp ? grep : new RegExp(grep)
52
+ let selected = 0
53
+ mocha.suite.eachTest(test => {
54
+ pattern.lastIndex = 0
55
+ if (pattern.test(test.fullTitle()) !== Boolean(mocha.options.invert)) selected++
56
+ })
57
+ if (!selected) throw new Error('storyboard: no executable scenarios match this run')
58
+ }
59
+
60
+ module.exports = function loadRules (mocha) {
61
+ try {
62
+ completeRules(mocha)
63
+ } catch (error) {
64
+ // CodeceptJS logs and swallows event-listener exceptions. Make loader
65
+ // failures actual failing Mocha tests, including an empty grep selection.
66
+ const { createTest } = require('codeceptjs/lib/mocha/test')
67
+ mocha.grep(/.*/)
68
+ mocha.options.invert = false
69
+ mocha.suite.addTest(createTest(error.message, () => { throw error }))
70
+ }
71
+ }