Skip to content

Commit cf9575f

Browse files
authored
feat: allow skipping JavaScript blocks (#88)
* feat: allow skipping JS blocks * update readme * remove console.log
1 parent a80d055 commit cf9575f

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# cypress-markdown-preprocessor
2+
23
[![ci status][ci image]][ci url] [![badges status][badges image]][badges url] [![renovate-app badge][renovate-badge]][renovate-app] ![cypress version](https://img.shields.io/badge/cypress-9.4.1-brightgreen) ![@cypress/fiddle version](https://img.shields.io/badge/@cypress/fiddle-1.19.0-brightgreen)
4+
35
> Use Markdown files as Cypress specs
46
57
Read [Write Cypress Markdown Preprocessor](https://glebbahmutov.com/blog/write-cypress-preprocessor/)
@@ -71,6 +73,16 @@ To create suites of tests, use `/` to separate the suite name from the test name
7173
```
7274
<!-- fiddle-end -->
7375

76+
### Skipping JavaScript blocks
77+
78+
You can skip JS blocks in the fiddle by using `js skip` syntax
79+
80+
````
81+
```js skip
82+
// this JavaScript block won't be included in fiddle
83+
```
84+
````
85+
7486
## Examples
7587

7688
See the presentation [Using End-to-end Tests as Documentation](https://slides.com/bahmutov/tests-are-docs)

cypress/integration/js-skip.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Skips JS block
2+
3+
<!-- fiddle Two JS blocks -->
4+
5+
This HTML block is ignored
6+
7+
```html skip
8+
<div>This HTML does not run</div>
9+
<script>
10+
throw new Error('Should skip HTML block')
11+
</script>
12+
```
13+
14+
The first JS block should be ignored
15+
16+
```js skip
17+
// THIS JavaScript WILL BE SKIPPED
18+
// and not included in the extracted fiddle
19+
expect(false).to.be.true
20+
```
21+
22+
```js
23+
// this JS block will run
24+
expect(42).to.equal(42)
25+
```
26+
27+
<!-- fiddle.end -->

src/markdown-utils.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
// @ts-check
22
const { parse } = require('@textlint/markdown-to-ast')
33
const debug = require('debug')('cypress-markdown-preprocessor')
4-
const verbose = require('debug')('cypress-markdown-preprocessor:verbose')
4+
const verbose = require('debug')(
5+
'cypress-markdown-preprocessor:verbose',
6+
)
57

68
/**
79
* Finds optional fiddle name from the comment line
810
* `<!-- fiddle my name -->` returns "my name".
911
*/
1012
const findFiddleName = (commentLine) => {
1113
debug('finding fiddle name from line: "%s"', commentLine)
12-
const matches = /fiddle(?:\.only|\.skip|\.export)? (.+)-->/.exec(commentLine)
14+
const matches = /fiddle(?:\.only|\.skip|\.export)? (.+)-->/.exec(
15+
commentLine,
16+
)
1317
if (matches && matches.length) {
1418
const testTitle = matches[1].trim()
1519
debug('test title: "%s"', testTitle)
@@ -21,7 +25,8 @@ const findFiddleName = (commentLine) => {
2125

2226
const isFiddleOnly = (line) => line.startsWith('<!-- fiddle.only ')
2327
const isFiddleSkip = (line) => line.startsWith('<!-- fiddle.skip ')
24-
const isFiddleExport = (line) => line.startsWith('<!-- fiddle.export ')
28+
const isFiddleExport = (line) =>
29+
line.startsWith('<!-- fiddle.export ')
2530

2631
const isFiddleMarkup = (s) => s && s.startsWith('<!-- fiddle-markup')
2732

@@ -79,7 +84,9 @@ function extractFiddles(md) {
7984
let startLine
8085
do {
8186
debug('start with %d', start)
82-
start = lines.findIndex((line, k) => k >= start && isFiddleStartLine(line))
87+
start = lines.findIndex(
88+
(line, k) => k >= start && isFiddleStartLine(line),
89+
)
8390
if (start === -1) {
8491
break
8592
}
@@ -117,28 +124,47 @@ function extractFiddles(md) {
117124
// list of fiddles converted into JavaScript
118125
const testFiddles = []
119126

120-
const isHtmlCodeBlock = (n) => n.type === 'CodeBlock' && n.lang === 'html'
127+
const shouldIncludeBlock = (block) => {
128+
if (!block.meta) {
129+
return true
130+
}
131+
return !block.meta.includes('skip')
132+
}
133+
134+
const isHtmlCodeBlock = (n) =>
135+
n.type === 'CodeBlock' && n.lang === 'html'
121136
const isLiveHtml = (n) => n.type === 'Html'
122137
const isJavaScript = (n) =>
123-
n.type === 'CodeBlock' && (n.lang === 'js' || n.lang === 'javascript')
138+
n.type === 'CodeBlock' &&
139+
(n.lang === 'js' || n.lang === 'javascript')
124140

125141
fiddles.forEach((fiddle) => {
126142
const ast = parse(fiddle.fiddle)
127143
// console.log('markdown fiddle AST')
128144
// console.log(ast)
129-
const htmlCodeBlockMaybe = ast.children.find((s) => isHtmlCodeBlock(s))
145+
const htmlCodeBlockMaybe = ast.children.find(
146+
(s) => isHtmlCodeBlock(s) && shouldIncludeBlock(s),
147+
)
130148
const htmlLiveBlockMaybe = ast.children.find(
131-
(s) => isLiveHtml(s) && !isFiddleMarkup(s.value),
149+
(s) =>
150+
isLiveHtml(s) &&
151+
!isFiddleMarkup(s.value) &&
152+
shouldIncludeBlock(s),
153+
)
154+
const htmlMarkup = ast.children.find((s) =>
155+
isFiddleMarkup(s.value),
132156
)
133-
const htmlMarkup = ast.children.find((s) => isFiddleMarkup(s.value))
134157

135158
// console.log('found html block?', htmlMaybe)
136159

137160
// a single fiddle can have multiple JS blocks
138161
// we want to find them all and merge into a single test
139-
const jsMaybe = ast.children.filter(isJavaScript)
162+
const jsMaybe = ast.children
163+
.filter(isJavaScript)
164+
.filter(shouldIncludeBlock)
140165

141166
if (jsMaybe.length) {
167+
// console.log(jsMaybe)
142168
const testCode = jsMaybe.map((b) => b.value).join('\n')
143169

144170
const htmlNode = htmlLiveBlockMaybe || htmlCodeBlockMaybe

0 commit comments

Comments
 (0)