Skip to content

Commit

Permalink
test: ensure each test case in a rule has a title (act-rules#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeeyyy authored Mar 4, 2020
1 parent 8a1b34f commit 898bb0f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 8 deletions.
2 changes: 1 addition & 1 deletion __tests__/link-reference-has-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const uniqueArray = require('../utils/unique-array')

describe(`Validate link references`, () => {
describeRule('Rules', ({ markdownAST }) => validateLinkReferences(markdownAST))
describePage('Rules', ({ markdownAST }) => validateLinkReferences(markdownAST))
describePage('Pages', ({ markdownAST }) => validateLinkReferences(markdownAST))
})

function validateLinkReferences(markdownAST) {
Expand Down
29 changes: 29 additions & 0 deletions _rules/__tests__/testcase-has-heading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const describeRule = require('../../test-utils/describe-rule')
const getMarkdownAstNodesOfType = require('../../utils/get-markdown-ast-nodes-of-type')

describeRule('testcase has heading', ({ filename, markdownAST }) => {
/**
* get all headings of test case examples (eg: #### Failed Example 1)
*/
const testcaseHeadings = getMarkdownAstNodesOfType(markdownAST, 'heading')
.filter(({ depth, children }) => {
return depth === 4 && children && children.length > 0
})
.map(({ children }) => {
const [textNode] = children
return textNode.value
})

/**
* get code blocks in markdown body
*/
const testcaseCodeSnippets = getMarkdownAstNodesOfType(markdownAST, 'code')

/**
* Check if filename has `id` as a part of the name
*/
test('each testcase has a heading', () => {
const msg = `Not all test cases have headings in ${filename}.`
expect(testcaseHeadings.length, msg).toBe(testcaseCodeSnippets.length)
})
})
File renamed without changes.
42 changes: 37 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@
"is-url": "^1.2.4",
"jest-expect-message": "^1.0.2",
"remark": "^11.0.2",
"remark-frontmatter": "^1.3.2",
"remove-markdown": "^0.3.0",
"unified": "^8.4.2",
"unist-util-visit": "^2.0.2"
},
"devDependencies": {
Expand Down
11 changes: 9 additions & 2 deletions utils/get-markdown-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const fs = require('fs')
const globby = require('globby')
const path = require('path')
const fastmatter = require('fastmatter')
const remark = require('remark')
const unified = require('unified')
const remarkParse = require('remark-parse')
const remarkFrontmatter = require('remark-frontmatter')

/**
* Parse all markdown files in a given directory and construct metadata of each markdown file
Expand All @@ -14,8 +16,13 @@ const remark = require('remark')
const getMarkdownData = (dir, exclude = []) => {
return globby.sync([`${dir}/**/*.md`, ...exclude]).map(markdownPath => {
const filename = path.parse(markdownPath).base

const fileContents = fs.readFileSync(markdownPath, { encoding: 'utf-8' })
const markdownAST = remark.parse(fileContents)
const unifiedProcesser = unified()
.use(remarkParse)
.use(remarkFrontmatter)
const markdownAST = unifiedProcesser.parse(fileContents)

const { attributes: frontmatter, body } = fastmatter(fileContents)
return {
path: markdownPath,
Expand Down

0 comments on commit 898bb0f

Please sign in to comment.