Skip to content

Commit bbc2e8a

Browse files
committed
Use Node test runner
1 parent 1d4ed1f commit bbc2e8a

File tree

3 files changed

+61
-49
lines changed

3 files changed

+61
-49
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/hydrogen
2121
- node

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,13 @@
3838
"web-namespaces": "^2.0.0"
3939
},
4040
"devDependencies": {
41-
"@types/glob": "^8.0.0",
41+
"@types/node": "^18.0.0",
4242
"@types/jsdom": "^20.0.0",
43-
"@types/tape": "^4.0.0",
4443
"c8": "^7.0.0",
45-
"glob": "^8.0.0",
4644
"jsdom": "^21.0.0",
4745
"prettier": "^2.0.0",
4846
"remark-cli": "^11.0.0",
4947
"remark-preset-wooorm": "^9.0.0",
50-
"tape": "^5.0.0",
5148
"type-coverage": "^2.0.0",
5249
"typescript": "^4.0.0",
5350
"xo": "^0.53.0"
@@ -69,7 +66,15 @@
6966
"trailingComma": "none"
7067
},
7168
"xo": {
72-
"prettier": true
69+
"prettier": true,
70+
"overrides": [
71+
{
72+
"files": "test/**/*.js",
73+
"rules": {
74+
"no-await-in-loop": 0
75+
}
76+
}
77+
]
7378
},
7479
"remarkConfig": {
7580
"plugins": [

test/index.js

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
/* eslint-env browser */
66

7-
import fs from 'node:fs'
8-
import path from 'node:path'
9-
import test from 'tape'
10-
import glob from 'glob'
7+
import assert from 'node:assert/strict'
8+
import fs from 'node:fs/promises'
9+
import process from 'node:process'
10+
import test from 'node:test'
1111
import {JSDOM} from 'jsdom'
1212
import {fromDom} from '../index.js'
1313

1414
const window = new JSDOM().window
1515
globalThis.document = window.document
1616

17-
test('hast-util-from-dom', (t) => {
18-
t.deepEqual(
17+
test('hast-util-from-dom', () => {
18+
assert.deepEqual(
1919
fromDom(doc('<title>Hello!</title><h1>World!')),
2020
{
2121
type: 'root',
@@ -58,7 +58,7 @@ test('hast-util-from-dom', (t) => {
5858
'should transform a complete document'
5959
)
6060

61-
t.deepEqual(
61+
assert.deepEqual(
6262
fromDom(fragment('<title>Hello!</title><h1>World!')),
6363
{
6464
type: 'root',
@@ -80,19 +80,19 @@ test('hast-util-from-dom', (t) => {
8080
'should transform a fragment'
8181
)
8282

83-
t.deepEqual(
83+
assert.deepEqual(
8484
fromDom(document.createDocumentFragment()),
8585
{type: 'root', children: []},
8686
'should support an empty fragment'
8787
)
8888

89-
t.deepEqual(
89+
assert.deepEqual(
9090
fromDom(document.createComment('alpha')),
9191
{type: 'comment', value: 'alpha'},
9292
'should support a comment'
9393
)
9494

95-
t.deepEqual(
95+
assert.deepEqual(
9696
fromDom(document.createTextNode('bravo')),
9797
{type: 'text', value: 'bravo'},
9898
'should support a text'
@@ -102,7 +102,7 @@ test('hast-util-from-dom', (t) => {
102102
contentType: 'application/xml'
103103
}).window.document.createCDATASection('charlie')
104104

105-
t.deepEqual(
105+
assert.deepEqual(
106106
fromDom(cdata),
107107
{type: 'root', children: []},
108108
'should handle CDATA'
@@ -113,26 +113,26 @@ test('hast-util-from-dom', (t) => {
113113
// eslint-disable-next-line unicorn/prefer-dom-node-append
114114
frag.appendChild(cdata)
115115

116-
t.deepEqual(
116+
assert.deepEqual(
117117
fromDom(frag),
118118
{type: 'root', children: []},
119119
'should handle CDATA in HTML'
120120
)
121121

122-
t.deepEqual(
122+
assert.deepEqual(
123123
// @ts-expect-error runtime.
124124
fromDom(),
125125
{type: 'root', children: []},
126126
'should handle a missing DOM tree'
127127
)
128128

129-
t.deepEqual(
129+
assert.deepEqual(
130130
fromDom(document.createTextNode('')),
131131
{type: 'text', value: ''},
132132
'should support a text w/o value'
133133
)
134134

135-
t.deepEqual(
135+
assert.deepEqual(
136136
fromDom(document.createComment('')),
137137
{type: 'comment', value: ''},
138138
'should support a comment w/o value'
@@ -142,7 +142,7 @@ test('hast-util-from-dom', (t) => {
142142
const element = document.createElement('div')
143143
element.setAttributeNode(attribute)
144144

145-
t.deepEqual(
145+
assert.deepEqual(
146146
fromDom(element),
147147
{type: 'element', tagName: 'div', properties: {title: ''}, children: []},
148148
'should support an attribute w/o value'
@@ -152,7 +152,7 @@ test('hast-util-from-dom', (t) => {
152152
const text = document.createTextNode('Hello')
153153
heading.append(text)
154154

155-
t.deepEqual(
155+
assert.deepEqual(
156156
(() => {
157157
/** @type {Array<[Node, HastNode|undefined]>} */
158158
const calls = []
@@ -181,45 +181,48 @@ test('hast-util-from-dom', (t) => {
181181
],
182182
'should invoke afterTransform'
183183
)
184-
185-
t.end()
186184
})
187185

188-
test('fixtures', (t) => {
189-
const root = path.join('test', 'fixtures')
190-
const fixturePaths = glob.sync(path.join(root, '**/*/'))
191-
let index = -1
192-
193-
while (++index < fixturePaths.length) {
194-
each(fixturePaths[index])
195-
}
186+
test('fixtures', async () => {
187+
const base = new URL('fixtures/', import.meta.url)
188+
const folders = await fs.readdir(base)
196189

197-
t.end()
190+
for (const folder of folders) {
191+
if (folder.charAt(0) === '.') {
192+
continue
193+
}
198194

199-
function each(/** @type {string} */ fixturePath) {
200-
const input = path.join(fixturePath, 'index.html')
201-
const output = path.join(fixturePath, 'index.json')
202-
const fixtureHtml = String(fs.readFileSync(input))
203-
const actual = fromDom(doc(fixtureHtml))
204-
/** @type {unknown} */
205-
let parsedExpected
195+
const treeUrl = new URL(folder + '/index.json', base)
196+
const fixtureUrl = new URL(folder + '/index.html', base)
197+
const input = String(await fs.readFile(fixtureUrl))
198+
const actual = fromDom(doc(input))
199+
/** @type {HastNode} */
200+
let expected
206201

207202
try {
208-
parsedExpected = JSON.parse(String(fs.readFileSync(output)))
203+
if ('UPDATE' in process.env) {
204+
throw new Error('Updating')
205+
}
206+
207+
expected = JSON.parse(String(await fs.readFile(treeUrl)))
209208
} catch {
210-
fs.writeFileSync(output, JSON.stringify(actual, null, 2))
211-
return
209+
await fs.writeFile(treeUrl, JSON.stringify(actual, null, 2))
210+
continue
212211
}
213212

214-
t.deepEqual(actual, parsedExpected, path.basename(fixturePath))
213+
assert.deepEqual(actual, expected, folder)
215214
}
216215
})
217216

218-
function fragment(/** @type {string} */ htmlString) {
217+
/**
218+
* @param {string} value
219+
* @returns {DocumentFragment}
220+
*/
221+
function fragment(value) {
219222
const node = document.createDocumentFragment()
220223
const temporary = document.createElement('body')
221224

222-
temporary.innerHTML = htmlString
225+
temporary.innerHTML = value
223226

224227
let child = temporary.firstChild
225228

@@ -232,6 +235,10 @@ function fragment(/** @type {string} */ htmlString) {
232235
return node
233236
}
234237

235-
function doc(/** @type {string} */ htmlString) {
236-
return new JSDOM(htmlString).window.document
238+
/**
239+
* @param {string} value
240+
* @returns {Document}
241+
*/
242+
function doc(value) {
243+
return new JSDOM(value).window.document
237244
}

0 commit comments

Comments
 (0)