@digital-commons-official/codeceptjs-storyboard 0.2.0 โ 0.3.0
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 +47 -11
- package/package.json +1 -1
- package/src/index.d.ts +27 -1
- package/src/index.js +23 -5
- package/src/theme.js +67 -0
package/README.md
CHANGED
|
@@ -55,6 +55,42 @@ When the run ends, open `_output/index.html` to browse its storyboards. The page
|
|
|
55
55
|
|
|
56
56
|
A filtered run lists only the scenarios it ran. In baseline-update mode, `storyboards/index.html` lists the passing scenarios from that update; failed scenarios never replace reviewed storyboards. Run the full suite in update mode to rebuild the complete reviewed gallery.
|
|
57
57
|
|
|
58
|
+
### Colours and fonts
|
|
59
|
+
|
|
60
|
+
Set `theme` on the storyboard plugin to an object, a CSS file path, or an HTTP(S) URL:
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
storyboard: {
|
|
64
|
+
require: '@digital-commons-official/codeceptjs-storyboard',
|
|
65
|
+
enabled: true,
|
|
66
|
+
theme: './storyboard.css'
|
|
67
|
+
// Or: theme: 'https://example.org/storyboard.css'
|
|
68
|
+
// Or: theme: { bg: '#f6f8fb', text: '#192b3d', sans: 'Georgia, serif' }
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Relative paths start at the directory containing `codecept.conf.js`. The plugin loads the theme once per run, before the first scenario's steps, and embeds its values in every SVG. Remote requests time out after five seconds. A missing, unreachable or invalid theme emits a warning and uses the default dark theme.
|
|
73
|
+
|
|
74
|
+
```css
|
|
75
|
+
:root {
|
|
76
|
+
--sb-bg: #f6f8fb;
|
|
77
|
+
--sb-card: #ffffff;
|
|
78
|
+
--sb-slot: #edf2f8;
|
|
79
|
+
--sb-text: #192b3d;
|
|
80
|
+
--sb-dim: #42576b;
|
|
81
|
+
--sb-accent: #175fa4;
|
|
82
|
+
--sb-code-bg: #e7eef7;
|
|
83
|
+
--sb-given: #356b16;
|
|
84
|
+
--sb-when: #175fa4;
|
|
85
|
+
--sb-then: #7b3990;
|
|
86
|
+
--sb-font-sans: Georgia, serif;
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Supported object keys are `bg`, `card`, `slot`, `text`, `dim`, `accent`, `badge`, `codeBg`, `given`, `when`, `then`, `fail`, `sans` and `mono`. CSS uses `--sb-<key>`, except `codeBg` becomes `--sb-code-bg`, and the fonts use `--sb-font-sans` and `--sb-font-mono`. Omitted values keep their defaults. Colours accept hex, named colours, `rgb()` and `hsl()` values; fonts accept installed font family names and fallback lists.
|
|
91
|
+
|
|
92
|
+
Only these properties are read. Imports, external fonts and arbitrary CSS rules are not embedded. The SVG works offline; install the same fonts on machines where matching typography matters. Themes change the labels and cards around captured frames; the screenshot pixels stay unchanged.
|
|
93
|
+
|
|
58
94
|
## Usage ๐
|
|
59
95
|
|
|
60
96
|
### The `.feature` is the single human-authored source
|
|
@@ -109,17 +145,17 @@ Every baseline written this way must be inspected by a human, which is why the m
|
|
|
109
145
|
|
|
110
146
|
## API ๐
|
|
111
147
|
|
|
112
|
-
| Function
|
|
113
|
-
|
|
114
|
-
| `storyboardStep(register, sentence, fn)`
|
|
115
|
-
| `capturePageFrame(I, name)`
|
|
116
|
-
| `captureElementFrame(I, name, selector)`
|
|
117
|
-
| `addStoryboardFrame(I, png)`
|
|
118
|
-
| `annotate({ note, copy })`
|
|
119
|
-
| `assertOrUpdateBaseline(I, name)`
|
|
120
|
-
| `render(outFile, { imageDir, failure })` | Write the SVG
|
|
121
|
-
| `parseScenarioLines(featureFile, title)`
|
|
122
|
-
| `begin`, `panel`, `frame`, `panels`
|
|
148
|
+
| Function | Purpose |
|
|
149
|
+
|-------------------------------------------------|--------------------------------------------------------------------------|
|
|
150
|
+
| `storyboardStep(register, sentence, fn)` | Register a sentence literally through `Given`, `When` or `Then` |
|
|
151
|
+
| `capturePageFrame(I, name)` | Full-viewport frame, returns the PNG path |
|
|
152
|
+
| `captureElementFrame(I, name, selector)` | Element-cropped frame, returns the PNG path |
|
|
153
|
+
| `addStoryboardFrame(I, png)` | Attach the frame to the open card and assert it at `tolerance: 0` |
|
|
154
|
+
| `annotate({ note, copy })` | Runtime override of the open card's note and copyable command |
|
|
155
|
+
| `assertOrUpdateBaseline(I, name)` | The assert-or-rewrite primitive `addStoryboardFrame` relies on |
|
|
156
|
+
| `render(outFile, { imageDir, failure, theme })` | Write the SVG, optionally with a theme object |
|
|
157
|
+
| `parseScenarioLines(featureFile, title)` | Every sentence of a scenario with its `# Chapter` / `# Note` / `# Copy` |
|
|
158
|
+
| `begin`, `panel`, `frame`, `panels` | The board primitives the plugin drives (useful for standalone rendering) |
|
|
123
159
|
|
|
124
160
|
Type definitions ship in `src/index.d.ts`.
|
|
125
161
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digital-commons-official/codeceptjs-storyboard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
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.d.ts
CHANGED
|
@@ -64,7 +64,33 @@ export interface FailureFrame {
|
|
|
64
64
|
paths: { expected: string, diff: string, actual: string }
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/** Colours and installed font families used around the captured frames. */
|
|
68
|
+
export interface Theme {
|
|
69
|
+
bg?: string
|
|
70
|
+
card?: string
|
|
71
|
+
slot?: string
|
|
72
|
+
text?: string
|
|
73
|
+
dim?: string
|
|
74
|
+
accent?: string
|
|
75
|
+
badge?: string
|
|
76
|
+
codeBg?: string
|
|
77
|
+
given?: string
|
|
78
|
+
when?: string
|
|
79
|
+
then?: string
|
|
80
|
+
fail?: string
|
|
81
|
+
sans?: string
|
|
82
|
+
mono?: string
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface StoryboardConfig {
|
|
86
|
+
/** Theme object, CSS file relative to codecept.conf.js, or HTTP(S) CSS URL. */
|
|
87
|
+
theme?: Theme | string
|
|
88
|
+
[option: string]: unknown
|
|
89
|
+
}
|
|
90
|
+
|
|
67
91
|
export interface RenderOptions {
|
|
92
|
+
/** Inline overrides for standalone rendering. */
|
|
93
|
+
theme?: Theme
|
|
68
94
|
/** Substitute every frame with the same-named PNG inside this directory (the reviewed baselines). */
|
|
69
95
|
imageDir?: string
|
|
70
96
|
/** Marks the drifted card in red, adds the expected/diff/actual band and the sentences never reached. */
|
|
@@ -75,7 +101,7 @@ type StepRegister = (pattern: string, fn: (...args: any[]) => unknown) => void
|
|
|
75
101
|
type StepFunction = (...args: any[]) => Promise<unknown> | unknown
|
|
76
102
|
|
|
77
103
|
/** The CodeceptJS plugin factory: `plugins: { storyboard: { require: '@digital-commons-official/codeceptjs-storyboard', enabled: true } }`. */
|
|
78
|
-
declare function storyboardPlugin (config?:
|
|
104
|
+
declare function storyboardPlugin (config?: StoryboardConfig): void
|
|
79
105
|
|
|
80
106
|
declare namespace storyboardPlugin {
|
|
81
107
|
/** Fill the header for the scenario about to run (the plugin calls it on test.before). */
|
package/src/index.js
CHANGED
|
@@ -55,6 +55,7 @@ const fs = require('fs')
|
|
|
55
55
|
const path = require('path')
|
|
56
56
|
const { PNG } = require('pngjs')
|
|
57
57
|
const { tryTo } = require('codeceptjs/effects')
|
|
58
|
+
const { PROPERTIES, normalizeTheme, loadTheme } = require('./theme')
|
|
58
59
|
|
|
59
60
|
let board = null
|
|
60
61
|
|
|
@@ -389,7 +390,13 @@ function render (outFile, options = {}) {
|
|
|
389
390
|
if (!board || !board.panels.length) {
|
|
390
391
|
throw new Error('storyboard.render() called with no panels')
|
|
391
392
|
}
|
|
392
|
-
const
|
|
393
|
+
const custom = normalizeTheme(options.theme || board.theme || {})
|
|
394
|
+
const t = { ...THEME }
|
|
395
|
+
const declarations = Object.entries(custom).map(([key, value]) => {
|
|
396
|
+
t[key] = `var(${PROPERTIES[key]})`
|
|
397
|
+
return `${PROPERTIES[key]}:${value}`
|
|
398
|
+
}).join(';')
|
|
399
|
+
const themeCss = declarations ? `:root{${declarations}}` : ''
|
|
393
400
|
const g = GRID
|
|
394
401
|
const failure = options.failure || null
|
|
395
402
|
const resolveImage = (file) =>
|
|
@@ -735,7 +742,7 @@ function render (outFile, options = {}) {
|
|
|
735
742
|
' role="img" aria-labelledby="sb-title sb-desc">\n' +
|
|
736
743
|
`<title id="sb-title">${esc(board.feature)} โ ${esc(board.scenario)}</title>\n` +
|
|
737
744
|
`<desc id="sb-desc">e2e storyboard ยท ${esc(board.file)} ยท replay: ${esc(board.rerun)}</desc>\n` +
|
|
738
|
-
|
|
745
|
+
`<style>${themeCss}.copy{user-select:all;-webkit-user-select:all;cursor:text}.copy:hover{text-decoration:underline}</style>\n` +
|
|
739
746
|
`<rect width="${width}" height="${height}" fill="${t.bg}"/>\n` +
|
|
740
747
|
parts.join('\n') +
|
|
741
748
|
'\n</svg>\n'
|
|
@@ -807,11 +814,15 @@ function logVisualRegression (frame, outputSvg) {
|
|
|
807
814
|
// codeceptjs 3.7.8 sets test.title / test.tags / test.file / test.parent.title
|
|
808
815
|
// (feature name) in lib/mocha/gherkin.js; titles carry the tags appended.
|
|
809
816
|
// ---------------------------------------------------------------------------
|
|
810
|
-
module.exports = function storyboardPlugin () {
|
|
811
|
-
const { event } = require('codeceptjs')
|
|
817
|
+
module.exports = function storyboardPlugin (config = {}) {
|
|
818
|
+
const { event, recorder } = require('codeceptjs')
|
|
812
819
|
const writeGallery = require('./gallery')
|
|
813
820
|
const entries = new Map()
|
|
814
|
-
|
|
821
|
+
let themePromise
|
|
822
|
+
event.dispatcher.on(event.all.before, () => {
|
|
823
|
+
entries.clear()
|
|
824
|
+
themePromise = null
|
|
825
|
+
})
|
|
815
826
|
event.dispatcher.on(event.test.before, (test) => {
|
|
816
827
|
const tags = test.tags || []
|
|
817
828
|
const tag = tags.length ? tags[tags.length - 1] : ''
|
|
@@ -841,6 +852,13 @@ module.exports = function storyboardPlugin () {
|
|
|
841
852
|
baseDir: base,
|
|
842
853
|
rerun: tag ? `task test -- --grep "${tag}"` : ''
|
|
843
854
|
})
|
|
855
|
+
if (config.theme) {
|
|
856
|
+
const current = board
|
|
857
|
+
recorder.add('load storyboard theme', async () => {
|
|
858
|
+
themePromise = themePromise || loadTheme(config.theme, e2eDir())
|
|
859
|
+
current.theme = await themePromise
|
|
860
|
+
})
|
|
861
|
+
}
|
|
844
862
|
})
|
|
845
863
|
|
|
846
864
|
// Every Gherkin sentence opens its own card AUTOMATICALLY, in scenario
|
package/src/theme.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const fs = require('fs/promises')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
|
|
4
|
+
const PROPERTIES = {
|
|
5
|
+
bg: '--sb-bg',
|
|
6
|
+
card: '--sb-card',
|
|
7
|
+
slot: '--sb-slot',
|
|
8
|
+
text: '--sb-text',
|
|
9
|
+
dim: '--sb-dim',
|
|
10
|
+
accent: '--sb-accent',
|
|
11
|
+
badge: '--sb-badge',
|
|
12
|
+
codeBg: '--sb-code-bg',
|
|
13
|
+
given: '--sb-given',
|
|
14
|
+
when: '--sb-when',
|
|
15
|
+
then: '--sb-then',
|
|
16
|
+
fail: '--sb-fail',
|
|
17
|
+
sans: '--sb-font-sans',
|
|
18
|
+
mono: '--sb-font-mono'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function normalizeTheme (input) {
|
|
22
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) throw new Error('theme must be an object')
|
|
23
|
+
const theme = {}
|
|
24
|
+
for (const key of Object.keys(PROPERTIES)) {
|
|
25
|
+
if (!Object.hasOwn(input, key)) continue
|
|
26
|
+
const value = String(input[key]).trim()
|
|
27
|
+
const font = key === 'sans' || key === 'mono'
|
|
28
|
+
const valid = font
|
|
29
|
+
? /^[a-zA-Z0-9 ,"'_-]+$/.test(value)
|
|
30
|
+
: /^(?:#[\da-f]{3}|#[\da-f]{4}|#[\da-f]{6}|#[\da-f]{8}|[a-z]+|(?:rgb|hsl)a?\([\d.,% /+-]+\))$/i.test(value)
|
|
31
|
+
if (!valid) throw new Error(`invalid theme value for ${key}`)
|
|
32
|
+
theme[key] = font ? value.replace(/"/g, "'") : value
|
|
33
|
+
}
|
|
34
|
+
return theme
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function parseCss (css) {
|
|
38
|
+
const declarations = new Map()
|
|
39
|
+
const source = css.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
40
|
+
for (const match of source.matchAll(/(--sb-[\w-]+)\s*:\s*([^;}]+)/g)) declarations.set(match[1], match[2].trim())
|
|
41
|
+
const input = {}
|
|
42
|
+
for (const [key, property] of Object.entries(PROPERTIES)) {
|
|
43
|
+
if (declarations.has(property)) input[key] = declarations.get(property)
|
|
44
|
+
}
|
|
45
|
+
if (!Object.keys(input).length) throw new Error('no supported theme properties')
|
|
46
|
+
return normalizeTheme(input)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function loadTheme (input, baseDir, warn = console.warn) {
|
|
50
|
+
try {
|
|
51
|
+
if (typeof input !== 'string') return normalizeTheme(input)
|
|
52
|
+
let css
|
|
53
|
+
if (/^https?:\/\//i.test(input)) {
|
|
54
|
+
const response = await fetch(input, { signal: AbortSignal.timeout(5000) })
|
|
55
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
|
56
|
+
css = await response.text()
|
|
57
|
+
} else {
|
|
58
|
+
css = await fs.readFile(path.resolve(baseDir, input), 'utf8')
|
|
59
|
+
}
|
|
60
|
+
return parseCss(css)
|
|
61
|
+
} catch (error) {
|
|
62
|
+
warn(`storyboard: could not load theme (${error.message}); using the default theme`)
|
|
63
|
+
return {}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = { PROPERTIES, normalizeTheme, loadTheme }
|