@@ -10,21 +10,19 @@ import process from 'node:process'
1010import test from 'node:test'
1111import { JSDOM } from 'jsdom'
1212import { fromDom } from '../index.js'
13- import * as mod from '../index.js'
1413
1514const window = new JSDOM ( ) . window
1615globalThis . document = window . document
1716
18- test ( 'fromDom' , ( ) => {
19- assert . deepEqual (
20- Object . keys ( mod ) . sort ( ) ,
21- [ 'fromDom' ] ,
22- 'should expose the public api'
23- )
17+ test ( 'fromDom' , async function ( t ) {
18+ await t . test ( 'should expose the public api' , async function ( ) {
19+ assert . deepEqual ( Object . keys ( await import ( '../index.js' ) ) . sort ( ) , [
20+ 'fromDom'
21+ ] )
22+ } )
2423
25- assert . deepEqual (
26- fromDom ( doc ( '<title>Hello!</title><h1>World!' ) ) ,
27- {
24+ await t . test ( 'should transform a complete document' , async function ( ) {
25+ assert . deepEqual ( fromDom ( doc ( '<title>Hello!</title><h1>World!' ) ) , {
2826 type : 'root' ,
2927 children : [
3028 {
@@ -61,13 +59,11 @@ test('fromDom', () => {
6159 ]
6260 }
6361 ]
64- } ,
65- 'should transform a complete document'
66- )
62+ } )
63+ } )
6764
68- assert . deepEqual (
69- fromDom ( fragment ( '<title>Hello!</title><h1>World!' ) ) ,
70- {
65+ await t . test ( 'should transform a fragment' , async function ( ) {
66+ assert . deepEqual ( fromDom ( fragment ( '<title>Hello!</title><h1>World!' ) ) , {
7167 type : 'root' ,
7268 children : [
7369 {
@@ -83,98 +79,101 @@ test('fromDom', () => {
8379 children : [ { type : 'text' , value : 'World!' } ]
8480 }
8581 ]
86- } ,
87- 'should transform a fragment'
88- )
89-
90- assert . deepEqual (
91- fromDom ( document . createDocumentFragment ( ) ) ,
92- { type : 'root' , children : [ ] } ,
93- 'should support an empty fragment'
94- )
95-
96- assert . deepEqual (
97- fromDom ( document . createComment ( 'alpha' ) ) ,
98- { type : 'comment' , value : 'alpha' } ,
99- 'should support a comment'
100- )
101-
102- assert . deepEqual (
103- fromDom ( document . createTextNode ( 'bravo' ) ) ,
104- { type : 'text' , value : 'bravo' } ,
105- 'should support a text'
106- )
107-
108- const cdata = new JSDOM ( '<xml></xml>' , {
109- contentType : 'application/xml'
110- } ) . window . document . createCDATASection ( 'charlie' )
111-
112- assert . deepEqual (
113- fromDom ( cdata ) ,
114- { type : 'root' , children : [ ] } ,
115- 'should handle CDATA'
116- )
117-
118- const frag = document . createDocumentFragment ( )
119-
120- // eslint-disable-next-line unicorn/prefer-dom-node-append
121- frag . appendChild ( cdata )
122-
123- assert . deepEqual (
124- fromDom ( frag ) ,
125- { type : 'root' , children : [ ] } ,
126- 'should handle CDATA in HTML'
127- )
128-
129- assert . deepEqual (
130- // @ts -expect-error runtime.
131- fromDom ( ) ,
132- { type : 'root' , children : [ ] } ,
133- 'should handle a missing DOM tree'
134- )
135-
136- assert . deepEqual (
137- fromDom ( document . createTextNode ( '' ) ) ,
138- { type : 'text' , value : '' } ,
139- 'should support a text w/o value'
140- )
141-
142- assert . deepEqual (
143- fromDom ( document . createComment ( '' ) ) ,
144- { type : 'comment' , value : '' } ,
145- 'should support a comment w/o value'
146- )
147-
148- const attribute = document . createAttribute ( 'title' )
149- const element = document . createElement ( 'div' )
150- element . setAttributeNode ( attribute )
151-
152- assert . deepEqual (
153- fromDom ( element ) ,
154- { type : 'element' , tagName : 'div' , properties : { title : '' } , children : [ ] } ,
155- 'should support an attribute w/o value'
156- )
157-
158- const heading = document . createElement ( 'h2' )
159- const text = document . createTextNode ( 'Hello' )
160- heading . append ( text )
161-
162- assert . deepEqual (
163- ( ( ) => {
164- /** @type {Array<[Node, HastNodes|undefined]> } */
165- const calls = [ ]
166- fromDom ( heading , {
167- /**
168- * @param {Node } node
169- * @param {HastNodes|undefined } transformed
170- */
171- afterTransform ( node , transformed ) {
172- calls . push ( [ node , transformed ] )
173- }
174- } )
175- return calls
176- } ) ( ) ,
177- [
82+ } )
83+ } )
84+
85+ await t . test ( 'should support an empty fragment' , async function ( ) {
86+ assert . deepEqual ( fromDom ( document . createDocumentFragment ( ) ) , {
87+ type : 'root' ,
88+ children : [ ]
89+ } )
90+ } )
91+
92+ await t . test ( 'should support a comment' , async function ( ) {
93+ assert . deepEqual ( fromDom ( document . createComment ( 'alpha' ) ) , {
94+ type : 'comment' ,
95+ value : 'alpha'
96+ } )
97+ } )
98+
99+ await t . test ( 'should support a text' , async function ( ) {
100+ assert . deepEqual ( fromDom ( document . createTextNode ( 'bravo' ) ) , {
101+ type : 'text' ,
102+ value : 'bravo'
103+ } )
104+ } )
105+
106+ await t . test ( 'should handle CDATA' , async function ( ) {
107+ const cdata = new JSDOM ( '<xml></xml>' , {
108+ contentType : 'application/xml'
109+ } ) . window . document . createCDATASection ( 'charlie' )
110+
111+ assert . deepEqual ( fromDom ( cdata ) , { type : 'root' , children : [ ] } )
112+ } )
113+
114+ await t . test ( 'should handle CDATA in HTML' , async function ( ) {
115+ const cdata = new JSDOM ( '<xml></xml>' , {
116+ contentType : 'application/xml'
117+ } ) . window . document . createCDATASection ( 'charlie' )
118+ const frag = document . createDocumentFragment ( )
119+
120+ // eslint-disable-next-line unicorn/prefer-dom-node-append
121+ frag . appendChild ( cdata )
122+
123+ assert . deepEqual ( fromDom ( frag ) , { type : 'root' , children : [ ] } )
124+ } )
125+
126+ await t . test ( 'should handle a missing DOM tree' , async function ( ) {
127+ assert . deepEqual (
128+ // To do: remove.
129+ // @ts -expect-error runtime.
130+ fromDom ( ) ,
131+ { type : 'root' , children : [ ] }
132+ )
133+ } )
134+
135+ await t . test ( 'should support a text w/o value' , async function ( ) {
136+ assert . deepEqual ( fromDom ( document . createTextNode ( '' ) ) , {
137+ type : 'text' ,
138+ value : ''
139+ } )
140+ } )
141+
142+ await t . test ( 'should support a comment w/o value' , async function ( ) {
143+ assert . deepEqual ( fromDom ( document . createComment ( '' ) ) , {
144+ type : 'comment' ,
145+ value : ''
146+ } )
147+ } )
148+
149+ await t . test ( 'should support an attribute w/o value' , async function ( ) {
150+ const attribute = document . createAttribute ( 'title' )
151+ const element = document . createElement ( 'div' )
152+ element . setAttributeNode ( attribute )
153+
154+ assert . deepEqual ( fromDom ( element ) , {
155+ type : 'element' ,
156+ tagName : 'div' ,
157+ properties : { title : '' } ,
158+ children : [ ]
159+ } )
160+ } )
161+
162+ await t . test ( 'should call `afterTransform`' , async function ( ) {
163+ const heading = document . createElement ( 'h2' )
164+ const text = document . createTextNode ( 'Hello' )
165+ heading . append ( text )
166+
167+ /** @type {Array<[unknown, unknown]> } */
168+ const calls = [ ]
169+
170+ fromDom ( heading , {
171+ afterTransform ( node , transformed ) {
172+ calls . push ( [ node , transformed ] )
173+ }
174+ } )
175+
176+ assert . deepEqual ( calls , [
178177 [ text , { type : 'text' , value : 'Hello' } ] ,
179178 [
180179 heading ,
@@ -185,12 +184,11 @@ test('fromDom', () => {
185184 children : [ { type : 'text' , value : 'Hello' } ]
186185 }
187186 ]
188- ] ,
189- 'should invoke afterTransform'
190- )
187+ ] )
188+ } )
191189} )
192190
193- test ( 'fixtures' , async ( ) => {
191+ test ( 'fixtures' , async function ( t ) {
194192 const base = new URL ( 'fixtures/' , import . meta. url )
195193 const folders = await fs . readdir ( base )
196194
@@ -199,25 +197,27 @@ test('fixtures', async () => {
199197 continue
200198 }
201199
202- const treeUrl = new URL ( folder + '/index.json' , base )
203- const fixtureUrl = new URL ( folder + '/index.html' , base )
204- const input = String ( await fs . readFile ( fixtureUrl ) )
205- const actual = fromDom ( doc ( input ) )
206- /** @type {HastNodes } */
207- let expected
200+ await t . test ( folder , async function ( ) {
201+ const treeUrl = new URL ( folder + '/index.json' , base )
202+ const fixtureUrl = new URL ( folder + '/index.html' , base )
203+ const input = String ( await fs . readFile ( fixtureUrl ) )
204+ const actual = fromDom ( doc ( input ) )
205+ /** @type {HastNodes } */
206+ let expected
207+
208+ try {
209+ if ( 'UPDATE' in process . env ) {
210+ throw new Error ( 'Updating' )
211+ }
208212
209- try {
210- if ( 'UPDATE' in process . env ) {
211- throw new Error ( 'Updating' )
213+ expected = JSON . parse ( String ( await fs . readFile ( treeUrl ) ) )
214+ } catch {
215+ await fs . writeFile ( treeUrl , JSON . stringify ( actual , undefined , 2 ) )
216+ return
212217 }
213218
214- expected = JSON . parse ( String ( await fs . readFile ( treeUrl ) ) )
215- } catch {
216- await fs . writeFile ( treeUrl , JSON . stringify ( actual , null , 2 ) )
217- continue
218- }
219-
220- assert . deepEqual ( actual , expected , folder )
219+ assert . deepEqual ( actual , expected , folder )
220+ } )
221221 }
222222} )
223223
0 commit comments