Skip to content

Commit 4a1a05e

Browse files
committed
Refactor tests
1 parent 223bf98 commit 4a1a05e

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,22 @@
5555
"uvu": "^0.5.0"
5656
},
5757
"devDependencies": {
58-
"@types/power-assert": "^1.0.0",
5958
"@types/tape": "^4.0.0",
6059
"c8": "^7.0.0",
6160
"commonmark.json": "^0.30.0",
6261
"esbuild": "^0.17.0",
6362
"gzip-size-cli": "^5.0.0",
63+
"hast-util-from-html": "^1.0.0",
6464
"hast-util-to-html": "^8.0.0",
6565
"mdast-util-to-hast": "^12.0.0",
6666
"micromark-build": "^1.0.0",
6767
"prettier": "^2.0.0",
68-
"rehype-parse": "^8.0.0",
69-
"rehype-stringify": "^9.0.0",
7068
"remark-cli": "^11.0.0",
7169
"remark-preset-wooorm": "^9.0.0",
7270
"tape": "^5.0.0",
7371
"terser": "^5.0.0",
7472
"type-coverage": "^2.0.0",
7573
"typescript": "^4.0.0",
76-
"unified": "^10.0.0",
7774
"xo": "^0.53.0"
7875
},
7976
"scripts": {
@@ -100,7 +97,15 @@
10097
"unicorn/prefer-code-point": "off",
10198
"unicorn/prefer-switch": "off",
10299
"unicorn/prefer-node-protocol": "off"
103-
}
100+
},
101+
"overrides": [
102+
{
103+
"files": "test/**/*.js",
104+
"rules": {
105+
"no-await-in-loop": "off"
106+
}
107+
}
108+
]
104109
},
105110
"remarkConfig": {
106111
"plugins": [

test/index.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22
* @typedef {import('mdast').Root} Root
33
*/
44

5+
import assert from 'node:assert/strict'
56
import {Buffer} from 'node:buffer'
6-
import fs from 'fs'
7-
import path from 'path'
8-
import assert from 'assert'
7+
import fs from 'node:fs/promises'
98
import test from 'tape'
10-
import {unified} from 'unified'
11-
import rehypeParse from 'rehype-parse'
12-
import rehypeStringify from 'rehype-stringify'
139
import {toHast} from 'mdast-util-to-hast'
1410
import {toString} from 'mdast-util-to-string'
11+
import {fromHtml} from 'hast-util-from-html'
1512
import {toHtml} from 'hast-util-to-html'
1613
import {commonmark} from 'commonmark.json'
1714
import {fromMarkdown} from '../dev/index.js'
1815

19-
const join = path.join
20-
2116
test('mdast-util-from-markdown', (t) => {
2217
t.equal(typeof fromMarkdown, 'function', 'should expose a function')
2318

@@ -1040,26 +1035,32 @@ test('mdast-util-from-markdown', (t) => {
10401035
t.end()
10411036
})
10421037

1043-
test('fixtures', (t) => {
1044-
const base = join('test', 'fixtures')
1045-
const files = fs.readdirSync(base).filter((d) => path.extname(d) === '.md')
1038+
test('fixtures', async (t) => {
1039+
const base = new URL('fixtures/', import.meta.url)
1040+
1041+
const files = await fs.readdir(base)
10461042
let index = -1
10471043

10481044
while (++index < files.length) {
10491045
const file = files[index]
1050-
const stem = path.basename(file, path.extname(file))
1051-
const fp = join(base, stem + '.json')
1052-
const doc = fs.readFileSync(join(base, stem + '.md'))
1046+
1047+
if (!/\.md$/i.test(file)) {
1048+
continue
1049+
}
1050+
1051+
const stem = file.split('.').slice(0, -1).join('.')
1052+
const fp = new URL(stem + '.json', base)
1053+
const doc = await fs.readFile(new URL(file, base))
10531054
const actual = fromMarkdown(doc)
10541055
/** @type {Root} */
10551056
let expected
10561057

10571058
try {
1058-
expected = JSON.parse(String(fs.readFileSync(fp)))
1059+
expected = JSON.parse(String(await fs.readFile(fp)))
10591060
} catch {
10601061
// New fixture.
10611062
expected = actual
1062-
fs.writeFileSync(fp, JSON.stringify(actual, null, 2) + '\n')
1063+
await fs.writeFile(fp, JSON.stringify(actual, null, 2) + '\n')
10631064
}
10641065

10651066
t.deepEqual(actual, expected, stem)
@@ -1070,6 +1071,8 @@ test('fixtures', (t) => {
10701071

10711072
test('commonmark', (t) => {
10721073
let index = -1
1074+
1075+
// To do: update micromark.
10731076
// Changes in living version of CommonMark.
10741077
const skip = new Set([623, 624])
10751078

@@ -1079,23 +1082,19 @@ test('commonmark', (t) => {
10791082
}
10801083

10811084
const example = commonmark[index]
1082-
const root = fromMarkdown(example.markdown.slice(0, -1))
1083-
const hast = toHast(root, {allowDangerousHtml: true})
1084-
assert(hast && hast.type === 'root', 'expected `root`')
1085-
const html = toHtml(hast, {
1086-
allowDangerousHtml: true,
1087-
entities: {useNamedReferences: true},
1088-
closeSelfClosing: true
1089-
})
1085+
const input = example.markdown.slice(0, -1)
1086+
const output = example.html.slice(0, -1)
10901087

1091-
const reformat = unified()
1092-
.use(rehypeParse, {fragment: true})
1093-
.use(rehypeStringify)
1094-
1095-
const actual = reformat.processSync(html).toString()
1096-
const expected = reformat.processSync(example.html.slice(0, -1)).toString()
1088+
const mdast = fromMarkdown(input)
1089+
const hast = toHast(mdast, {allowDangerousHtml: true})
1090+
assert(hast && hast.type === 'root', 'expected `root`')
1091+
const actual = toHtml(hast, {allowDangerousHtml: true})
10971092

1098-
t.deepLooseEqual(actual, expected, example.section + ' (' + index + ')')
1093+
t.deepLooseEqual(
1094+
toHtml(fromHtml(actual, {fragment: true})),
1095+
toHtml(fromHtml(output, {fragment: true})),
1096+
example.section + ' (' + index + ')'
1097+
)
10991098
}
11001099

11011100
t.end()

0 commit comments

Comments
 (0)