Skip to content

Commit ddc0cd6

Browse files
committed
Refactor tests
1 parent 52ba4aa commit ddc0cd6

File tree

2 files changed

+56
-58
lines changed

2 files changed

+56
-58
lines changed

package.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,20 @@
5353
"devDependencies": {
5454
"@types/tape": "^4.0.0",
5555
"c8": "^7.0.0",
56+
"hast-util-from-html": "^1.0.0",
5657
"hastscript": "^7.0.0",
5758
"is-hidden": "^2.0.0",
5859
"mdast-util-assert": "^4.0.0",
60+
"mdast-util-from-markdown": "^1.0.0",
61+
"mdast-util-gfm": "^2.0.0",
62+
"mdast-util-to-markdown": "^1.0.0",
63+
"micromark-extension-gfm": "^2.0.0",
5964
"prettier": "^2.0.0",
60-
"rehype-parse": "^8.0.0",
6165
"remark-cli": "^11.0.0",
62-
"remark-gfm": "^3.0.0",
63-
"remark-parse": "^10.0.0",
6466
"remark-preset-wooorm": "^9.0.0",
65-
"remark-stringify": "^10.0.0",
66-
"rimraf": "^3.0.0",
6767
"tape": "^5.0.0",
6868
"type-coverage": "^2.0.0",
6969
"typescript": "^4.0.0",
70-
"unified": "^10.0.0",
7170
"unist-builder": "^3.0.0",
7271
"unist-util-remove-position": "^4.0.0",
7372
"xo": "^0.53.0"
@@ -89,7 +88,15 @@
8988
"trailingComma": "none"
9089
},
9190
"xo": {
92-
"prettier": true
91+
"prettier": true,
92+
"overrides": [
93+
{
94+
"files": "test/**/*.js",
95+
"rules": {
96+
"no-await-in-loop": 0
97+
}
98+
}
99+
]
93100
},
94101
"remarkConfig": {
95102
"plugins": [

test/index.js

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
/**
2-
* @typedef {import('hast').Root} Root
3-
* @typedef {import('hast').Element} Element
42
* @typedef {import('../index.js').Options} Options
53
*/
64

7-
import fs from 'node:fs'
8-
import path from 'node:path'
5+
import fs from 'node:fs/promises'
96
import test from 'tape'
10-
import {u} from 'unist-builder'
11-
import {h} from 'hastscript'
127
import {isHidden} from 'is-hidden'
13-
import {unified} from 'unified'
14-
import remarkParse from 'remark-parse'
15-
import remarkGfm from 'remark-gfm'
16-
import rehypeParse from 'rehype-parse'
17-
import remarkStringify from 'remark-stringify'
18-
import {assert} from 'mdast-util-assert'
8+
import {h} from 'hastscript'
9+
import {fromHtml} from 'hast-util-from-html'
10+
import {assert as mdastAssert} from 'mdast-util-assert'
11+
import {fromMarkdown} from 'mdast-util-from-markdown'
12+
import {gfmFromMarkdown, gfmToMarkdown} from 'mdast-util-gfm'
13+
import {toMarkdown} from 'mdast-util-to-markdown'
14+
import {gfm} from 'micromark-extension-gfm'
15+
import {u} from 'unist-builder'
1916
import {removePosition} from 'unist-util-remove-position'
2017
import {one, all, defaultHandlers, toMdast} from '../index.js'
2118
import {wrapNeeded} from '../lib/util/wrap.js'
@@ -47,9 +44,9 @@ test('custom nodes', (t) => {
4744
})
4845

4946
test('exports', (t) => {
50-
t.assert(one, 'should export `one`')
51-
t.assert(all, 'should export `all`')
52-
t.assert(defaultHandlers, 'should export `defaultHandlers`')
47+
t.ok(one, 'should export `one`')
48+
t.ok(all, 'should export `all`')
49+
t.ok(defaultHandlers, 'should export `defaultHandlers`')
5350
t.end()
5451
})
5552

@@ -190,54 +187,41 @@ test('core', (t) => {
190187
t.end()
191188
})
192189

193-
test('fixtures', (t) => {
194-
const fixtures = path.join('test', 'fixtures')
195-
const remark = unified().use(remarkParse).use(remarkGfm).use(remarkStringify)
190+
test('fixtures', async (t) => {
191+
const fixtures = new URL('fixtures/', import.meta.url)
192+
const folders = await fs.readdir(fixtures)
196193

197-
fs.readdirSync(fixtures)
198-
.filter((d) => !isHidden(d))
199-
// eslint-disable-next-line unicorn/no-array-for-each
200-
.forEach((d) => check(d))
201-
202-
t.end()
194+
for (const folder of folders) {
195+
if (isHidden(folder)) {
196+
continue
197+
}
203198

204-
function check(/** @type {string} */ name) {
205-
const ignore = /^base\b/.test(name)
199+
const ignore = /^base\b/.test(folder)
206200

207-
t.test(name, (st) => {
201+
t.test(folder, async (st) => {
208202
const input = String(
209-
fs.readFileSync(path.join(fixtures, name, 'index.html'))
203+
await fs.readFile(new URL(folder + '/index.html', fixtures))
210204
)
211-
let output = String(
212-
fs.readFileSync(path.join(fixtures, name, 'index.md'))
205+
const expected = String(
206+
await fs.readFile(new URL(folder + '/index.md', fixtures))
213207
)
208+
// Replace middots with spaces (useful for trailing spaces).
209+
.replace(/·/g, ' ')
214210
/** @type {({stringify?: boolean, tree?: boolean} & Options) | undefined} */
215211
let config
216212

217213
try {
218214
config = JSON.parse(
219-
String(fs.readFileSync(path.join(fixtures, name, 'index.json')))
215+
String(await fs.readFile(new URL(folder + '/index.json', fixtures)))
220216
)
221217
} catch {}
222218

223-
const fromHtml = unified()
224-
.use(rehypeParse)
225-
// @ts-expect-error: turn into different tree..
226-
.use(() => {
227-
return transformer
228-
function transformer(/** @type {Root} */ tree) {
229-
return toMdast(tree, config)
230-
}
231-
})
232-
.use(remarkStringify)
233-
234-
const tree = removePosition(fromHtml.runSync(fromHtml.parse(input)), true)
235-
236-
// Replace middots with spaces (useful for trailing spaces).
237-
output = output.replace(/·/g, ' ')
219+
const hast = fromHtml(input)
220+
const mdast = toMdast(hast, config)
221+
removePosition(mdast, true)
238222

239223
st.doesNotThrow(() => {
240-
assert(tree)
224+
mdastAssert(mdast)
241225
}, 'should produce valid mdast nodes')
242226

243227
if (ignore) {
@@ -247,23 +231,30 @@ test('fixtures', (t) => {
247231

248232
if (!config || config.stringify !== false) {
249233
st.deepEqual(
250-
remark.stringify(tree),
251-
output,
234+
toMarkdown(mdast, {extensions: [gfmToMarkdown()]}),
235+
expected,
252236
'should produce the same documents'
253237
)
254238
}
255239

256240
if (!config || config.tree !== false) {
241+
const expectedMdast = fromMarkdown(expected, {
242+
extensions: [gfm()],
243+
mdastExtensions: [gfmFromMarkdown()]
244+
})
245+
removePosition(expectedMdast, true)
257246
st.deepEqual(
258-
tree,
259-
removePosition(remark.runSync(remark.parse(output)), true),
247+
mdast,
248+
expectedMdast,
260249
'should produce the same tree as remark'
261250
)
262251
}
263252

264253
st.end()
265254
})
266255
}
256+
257+
t.end()
267258
})
268259

269260
test('handlers option', (t) => {

0 commit comments

Comments
 (0)