4
4
5
5
/* eslint-env browser */
6
6
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 '
11
11
import { JSDOM } from 'jsdom'
12
12
import { fromDom } from '../index.js'
13
13
14
14
const window = new JSDOM ( ) . window
15
15
globalThis . document = window . document
16
16
17
- test ( 'hast-util-from-dom' , ( t ) => {
18
- t . deepEqual (
17
+ test ( 'hast-util-from-dom' , ( ) => {
18
+ assert . deepEqual (
19
19
fromDom ( doc ( '<title>Hello!</title><h1>World!' ) ) ,
20
20
{
21
21
type : 'root' ,
@@ -58,7 +58,7 @@ test('hast-util-from-dom', (t) => {
58
58
'should transform a complete document'
59
59
)
60
60
61
- t . deepEqual (
61
+ assert . deepEqual (
62
62
fromDom ( fragment ( '<title>Hello!</title><h1>World!' ) ) ,
63
63
{
64
64
type : 'root' ,
@@ -80,19 +80,19 @@ test('hast-util-from-dom', (t) => {
80
80
'should transform a fragment'
81
81
)
82
82
83
- t . deepEqual (
83
+ assert . deepEqual (
84
84
fromDom ( document . createDocumentFragment ( ) ) ,
85
85
{ type : 'root' , children : [ ] } ,
86
86
'should support an empty fragment'
87
87
)
88
88
89
- t . deepEqual (
89
+ assert . deepEqual (
90
90
fromDom ( document . createComment ( 'alpha' ) ) ,
91
91
{ type : 'comment' , value : 'alpha' } ,
92
92
'should support a comment'
93
93
)
94
94
95
- t . deepEqual (
95
+ assert . deepEqual (
96
96
fromDom ( document . createTextNode ( 'bravo' ) ) ,
97
97
{ type : 'text' , value : 'bravo' } ,
98
98
'should support a text'
@@ -102,7 +102,7 @@ test('hast-util-from-dom', (t) => {
102
102
contentType : 'application/xml'
103
103
} ) . window . document . createCDATASection ( 'charlie' )
104
104
105
- t . deepEqual (
105
+ assert . deepEqual (
106
106
fromDom ( cdata ) ,
107
107
{ type : 'root' , children : [ ] } ,
108
108
'should handle CDATA'
@@ -113,26 +113,26 @@ test('hast-util-from-dom', (t) => {
113
113
// eslint-disable-next-line unicorn/prefer-dom-node-append
114
114
frag . appendChild ( cdata )
115
115
116
- t . deepEqual (
116
+ assert . deepEqual (
117
117
fromDom ( frag ) ,
118
118
{ type : 'root' , children : [ ] } ,
119
119
'should handle CDATA in HTML'
120
120
)
121
121
122
- t . deepEqual (
122
+ assert . deepEqual (
123
123
// @ts -expect-error runtime.
124
124
fromDom ( ) ,
125
125
{ type : 'root' , children : [ ] } ,
126
126
'should handle a missing DOM tree'
127
127
)
128
128
129
- t . deepEqual (
129
+ assert . deepEqual (
130
130
fromDom ( document . createTextNode ( '' ) ) ,
131
131
{ type : 'text' , value : '' } ,
132
132
'should support a text w/o value'
133
133
)
134
134
135
- t . deepEqual (
135
+ assert . deepEqual (
136
136
fromDom ( document . createComment ( '' ) ) ,
137
137
{ type : 'comment' , value : '' } ,
138
138
'should support a comment w/o value'
@@ -142,7 +142,7 @@ test('hast-util-from-dom', (t) => {
142
142
const element = document . createElement ( 'div' )
143
143
element . setAttributeNode ( attribute )
144
144
145
- t . deepEqual (
145
+ assert . deepEqual (
146
146
fromDom ( element ) ,
147
147
{ type : 'element' , tagName : 'div' , properties : { title : '' } , children : [ ] } ,
148
148
'should support an attribute w/o value'
@@ -152,7 +152,7 @@ test('hast-util-from-dom', (t) => {
152
152
const text = document . createTextNode ( 'Hello' )
153
153
heading . append ( text )
154
154
155
- t . deepEqual (
155
+ assert . deepEqual (
156
156
( ( ) => {
157
157
/** @type {Array<[Node, HastNode|undefined]> } */
158
158
const calls = [ ]
@@ -181,45 +181,48 @@ test('hast-util-from-dom', (t) => {
181
181
] ,
182
182
'should invoke afterTransform'
183
183
)
184
-
185
- t . end ( )
186
184
} )
187
185
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 )
196
189
197
- t . end ( )
190
+ for ( const folder of folders ) {
191
+ if ( folder . charAt ( 0 ) === '.' ) {
192
+ continue
193
+ }
198
194
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
206
201
207
202
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 ) ) )
209
208
} catch {
210
- fs . writeFileSync ( output , JSON . stringify ( actual , null , 2 ) )
211
- return
209
+ await fs . writeFile ( treeUrl , JSON . stringify ( actual , null , 2 ) )
210
+ continue
212
211
}
213
212
214
- t . deepEqual ( actual , parsedExpected , path . basename ( fixturePath ) )
213
+ assert . deepEqual ( actual , expected , folder )
215
214
}
216
215
} )
217
216
218
- function fragment ( /** @type {string } */ htmlString ) {
217
+ /**
218
+ * @param {string } value
219
+ * @returns {DocumentFragment }
220
+ */
221
+ function fragment ( value ) {
219
222
const node = document . createDocumentFragment ( )
220
223
const temporary = document . createElement ( 'body' )
221
224
222
- temporary . innerHTML = htmlString
225
+ temporary . innerHTML = value
223
226
224
227
let child = temporary . firstChild
225
228
@@ -232,6 +235,10 @@ function fragment(/** @type {string} */ htmlString) {
232
235
return node
233
236
}
234
237
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
237
244
}
0 commit comments