Skip to content

Commit 2be85f6

Browse files
committed
Use Node test runner
1 parent 35cfa4e commit 2be85f6

File tree

3 files changed

+116
-80
lines changed

3 files changed

+116
-80
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ jobs:
1717
strategy:
1818
matrix:
1919
node:
20-
- lts/fermium
20+
- lts/gallium
2121
- node

package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,18 @@
4444
"unist-util-visit": "^4.0.0"
4545
},
4646
"devDependencies": {
47-
"@types/tape": "^4.0.0",
47+
"@types/node": "^18.0.0",
4848
"c8": "^7.0.0",
49+
"mdast-util-from-markdown": "^1.0.0",
50+
"mdast-util-gfm": "^2.0.0",
51+
"micromark-extension-gfm": "^2.0.0",
4952
"prettier": "^2.0.0",
50-
"remark": "^14.0.0",
5153
"remark-cli": "^11.0.0",
52-
"remark-gfm": "^3.0.0",
53-
"remark-parse": "^10.0.0",
5454
"remark-preset-wooorm": "^9.0.0",
5555
"remark-usage": "^10.0.0",
56-
"tape": "^5.0.0",
5756
"type-coverage": "^2.0.0",
5857
"typescript": "^4.0.0",
5958
"unified": "^10.0.0",
60-
"unist-builder": "^3.0.0",
6159
"xo": "^0.53.0"
6260
},
6361
"scripts": {
@@ -79,6 +77,12 @@
7977
"xo": {
8078
"prettier": true,
8179
"overrides": [
80+
{
81+
"files": "test/**/*.js",
82+
"rules": {
83+
"no-await-in-loop": "off"
84+
}
85+
},
8286
{
8387
"files": "example.js",
8488
"rules": {
@@ -89,7 +93,7 @@
8993
},
9094
"remarkConfig": {
9195
"plugins": [
92-
"preset-wooorm",
96+
"remark-preset-wooorm",
9397
[
9498
"remark-usage",
9599
{

test/index.js

Lines changed: 104 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef {import('mdast').Root} Root
33
* @typedef {import('mdast').Blockquote} Blockquote
4+
* @typedef {import('mdast').BlockContent} BlockContent
45
* @typedef {import('mdast').List} List
56
* @typedef {import('../index.js').Options} Options
67
*
@@ -10,116 +11,131 @@
1011
* @typedef {Options & TestConfig} Config
1112
*/
1213

13-
import fs from 'node:fs'
14-
import path from 'node:path'
15-
import test from 'tape'
16-
import {unified} from 'unified'
17-
import remarkParse from 'remark-parse'
18-
import remarkGfm from 'remark-gfm'
14+
import assert from 'node:assert/strict'
15+
import fs from 'node:fs/promises'
16+
import test from 'node:test'
17+
import {fromMarkdown} from 'mdast-util-from-markdown'
18+
import {gfmFromMarkdown} from 'mdast-util-gfm'
19+
import {gfm} from 'micromark-extension-gfm'
1920
import {visit} from 'unist-util-visit'
20-
import {u} from 'unist-builder'
2121
import {toc} from '../index.js'
2222

23-
const join = path.join
23+
test('mdast-util-toc', () => {
24+
assert.equal(typeof toc, 'function', 'should be a function')
2425

25-
test('mdast-util-toc', (t) => {
26-
t.is(typeof toc, 'function', 'should be a function')
27-
28-
t.throws(
26+
assert.throws(
2927
() => {
3028
// @ts-expect-error runtime.
3129
toc()
3230
},
3331
/Cannot read propert/,
3432
'should fail without node'
3533
)
36-
37-
t.end()
3834
})
3935

40-
test('Fixtures', (t) => {
41-
const root = join('test', 'fixtures')
42-
const files = fs.readdirSync(root)
36+
test('Fixtures', async () => {
37+
const root = new URL('fixtures/', import.meta.url)
38+
const files = await fs.readdir(root)
4339
let index = -1
4440

4541
while (++index < files.length) {
4642
const name = files[index]
4743

4844
if (name.indexOf('.') === 0) continue
4945

50-
const input = fs.readFileSync(join(root, name, 'input.md'))
46+
const input = await fs.readFile(new URL(name + '/input.md', root))
5147
/** @type {Config} */
5248
let config = {}
5349

5450
try {
5551
config = JSON.parse(
56-
String(fs.readFileSync(join(root, name, 'config.json')))
52+
String(await fs.readFile(new URL(name + '/config.json', root)))
5753
)
5854
} catch {}
5955

60-
const processor = unified().use(remarkParse).use(remarkGfm)
6156
const {useCustomHProperty, ...options} = config
6257

58+
const tree = fromMarkdown(input, {
59+
mdastExtensions: [gfmFromMarkdown()],
60+
extensions: [gfm()]
61+
})
62+
6363
if (useCustomHProperty) {
64-
processor.use(() => (tree) => {
65-
const node = /** @type {Root} */ (tree)
66-
visit(node, 'heading', (heading) => {
67-
heading.data = {hProperties: {id: 'b'}}
68-
})
64+
visit(tree, 'heading', (heading) => {
65+
heading.data = {hProperties: {id: 'b'}}
6966
})
7067
}
7168

72-
const tree = /** @type {Root} */ (processor.runSync(processor.parse(input)))
7369
const actual = toc(tree, options)
70+
7471
/** @type {Root} */
7572
const expected = JSON.parse(
76-
String(fs.readFileSync(join(root, name, 'output.json')))
73+
String(await fs.readFile(new URL(name + '/output.json', root)))
7774
)
7875

79-
t.deepEqual(actual, expected, name)
76+
assert.deepEqual(actual, expected, name)
8077
}
81-
82-
t.end()
8378
})
8479

85-
test('processing nodes', (t) => {
86-
const rootNode = /** @type {Root} */ (
87-
u('root', [
88-
u('heading', {depth: 1}, [u('text', 'Alpha')]),
89-
u('heading', {depth: 2}, [u('text', 'Bravo')])
90-
])
91-
)
92-
93-
const parentNode = /** @type {Blockquote} */ (
94-
u('blockquote', rootNode.children)
95-
)
96-
97-
const blockquoteNode = /** @type {Root} */ (
98-
u('root', [
99-
u('heading', {depth: 1}, [u('text', 'Charlie')]),
100-
u('heading', {depth: 2}, [u('text', 'Delta')]),
101-
u('blockquote', rootNode.children)
102-
])
103-
)
80+
test('processing nodes', () => {
81+
/** @type {Array<BlockContent>} */
82+
const fragment = [
83+
{type: 'heading', depth: 1, children: [{type: 'text', value: 'Alpha'}]},
84+
{type: 'heading', depth: 2, children: [{type: 'text', value: 'Bravo'}]}
85+
]
10486

10587
/** @type {List} */
106-
const expectedRootMap = u('list', {ordered: false, spread: true}, [
107-
u('listItem', {spread: true}, [
108-
u('paragraph', [
109-
u('link', {title: null, url: '#alpha'}, [u('text', 'Alpha')])
110-
]),
111-
u('list', {ordered: false, spread: false}, [
112-
u('listItem', {spread: false}, [
113-
u('paragraph', [
114-
u('link', {title: null, url: '#bravo'}, [u('text', 'Bravo')])
115-
])
116-
])
117-
])
118-
])
119-
])
120-
121-
t.deepEqual(
122-
toc(rootNode),
88+
const expectedRootMap = {
89+
type: 'list',
90+
ordered: false,
91+
spread: true,
92+
children: [
93+
{
94+
type: 'listItem',
95+
spread: true,
96+
children: [
97+
{
98+
type: 'paragraph',
99+
children: [
100+
{
101+
type: 'link',
102+
title: null,
103+
url: '#alpha',
104+
children: [{type: 'text', value: 'Alpha'}]
105+
}
106+
]
107+
},
108+
{
109+
type: 'list',
110+
ordered: false,
111+
spread: false,
112+
children: [
113+
{
114+
type: 'listItem',
115+
spread: false,
116+
children: [
117+
{
118+
type: 'paragraph',
119+
children: [
120+
{
121+
type: 'link',
122+
title: null,
123+
url: '#bravo',
124+
children: [{type: 'text', value: 'Bravo'}]
125+
}
126+
]
127+
}
128+
]
129+
}
130+
]
131+
}
132+
]
133+
}
134+
]
135+
}
136+
137+
assert.deepEqual(
138+
toc({type: 'root', children: fragment}),
123139
{
124140
index: null,
125141
endIndex: null,
@@ -128,8 +144,8 @@ test('processing nodes', (t) => {
128144
'can process root nodes'
129145
)
130146

131-
t.deepEqual(
132-
toc(parentNode),
147+
assert.deepEqual(
148+
toc({type: 'blockquote', children: fragment}),
133149
{
134150
index: null,
135151
endIndex: null,
@@ -138,15 +154,31 @@ test('processing nodes', (t) => {
138154
'can process non-root nodes'
139155
)
140156

141-
t.deepEqual(
142-
toc(blockquoteNode, {parents: 'blockquote'}),
157+
assert.deepEqual(
158+
toc(
159+
{
160+
type: 'root',
161+
children: [
162+
{
163+
type: 'heading',
164+
depth: 1,
165+
children: [{type: 'text', value: 'Charlie'}]
166+
},
167+
{
168+
type: 'heading',
169+
depth: 2,
170+
children: [{type: 'text', value: 'Delta'}]
171+
},
172+
{type: 'blockquote', children: fragment}
173+
]
174+
},
175+
{parents: 'blockquote'}
176+
),
143177
{
144178
index: null,
145179
endIndex: null,
146180
map: expectedRootMap
147181
},
148182
'can process custom parent nodes'
149183
)
150-
151-
t.end()
152184
})

0 commit comments

Comments
 (0)