Skip to content

Commit 8b2cafe

Browse files
authored
Merge pull request #3078 from modernweb-dev/migrate/parse5-utils-node-test
refactor(parse5-utils): migrate tests to node:test
2 parents 24138a4 + 7c3b608 commit 8b2cafe

2 files changed

Lines changed: 64 additions & 50 deletions

File tree

packages/parse5-utils/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
},
2727
"scripts": {
2828
"build": "tsc",
29-
"test:node": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --reporter dot",
30-
"test:watch": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --watch"
29+
"test:node": "node --test --test-force-exit test/**/*.test.js",
30+
"test:watch": "node --test --test-force-exit --watch test/**/*.test.js"
3131
},
3232
"files": [
3333
"*.d.ts",

packages/parse5-utils/test/index.test.js

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { expect } = require('chai');
1+
const { describe, it } = require('node:test');
2+
const assert = require('node:assert/strict');
23
const { parse, serialize } = require('parse5');
34
const { getAttribute, getTextContent, findElement } = require('../src/index');
45
const utils = require('../src/index');
@@ -9,7 +10,8 @@ describe('parse5-utils', () => {
910
const doc = parse('');
1011
const el = utils.createElement('my-element');
1112
utils.appendChild(doc, el);
12-
expect(serialize(doc)).to.equal(
13+
assert.equal(
14+
serialize(doc),
1315
'<html><head></head><body></body></html><my-element></my-element>',
1416
);
1517
});
@@ -18,7 +20,8 @@ describe('parse5-utils', () => {
1820
const doc = parse('');
1921
const el = utils.createElement('my-element', { foo: 'bar', x: '' });
2022
utils.appendChild(doc, el);
21-
expect(serialize(doc)).to.equal(
23+
assert.equal(
24+
serialize(doc),
2225
'<html><head></head><body></body></html><my-element foo="bar" x=""></my-element>',
2326
);
2427
});
@@ -29,14 +32,15 @@ describe('parse5-utils', () => {
2932
const doc = parse('');
3033
const el = utils.createScript();
3134
utils.appendChild(doc, el);
32-
expect(serialize(doc)).to.equal('<html><head></head><body></body></html><script></script>');
35+
assert.equal(serialize(doc), '<html><head></head><body></body></html><script></script>');
3336
});
3437

3538
it('create a script with attributes', () => {
3639
const doc = parse('');
3740
const el = utils.createScript({ type: 'module' });
3841
utils.appendChild(doc, el);
39-
expect(serialize(doc)).to.equal(
42+
assert.equal(
43+
serialize(doc),
4044
'<html><head></head><body></body></html><script type="module"></script>',
4145
);
4246
});
@@ -45,55 +49,58 @@ describe('parse5-utils', () => {
4549
const doc = parse('');
4650
const el = utils.createScript({ type: 'module' }, 'console.log("x");');
4751
utils.appendChild(doc, el);
48-
expect(serialize(doc)).to.equal(
52+
assert.equal(
53+
serialize(doc),
4954
'<html><head></head><body></body></html><script type="module">console.log("x");</script>',
5055
);
5156
});
5257
});
5358

5459
describe('isHtmlFragment()', () => {
5560
it('returns whether a HTML string is a fragment', () => {
56-
expect(utils.isHtmlFragment('<my-element></my-element>')).to.equal(true);
57-
expect(utils.isHtmlFragment('')).to.equal(true);
58-
expect(utils.isHtmlFragment('foo')).to.equal(true);
59-
expect(utils.isHtmlFragment('<div></div>')).to.equal(true);
60-
expect(utils.isHtmlFragment('<!-- COMMENT --><!DOCTYPE><my-element></my-element>')).to.equal(
61+
assert.equal(utils.isHtmlFragment('<my-element></my-element>'), true);
62+
assert.equal(utils.isHtmlFragment(''), true);
63+
assert.equal(utils.isHtmlFragment('foo'), true);
64+
assert.equal(utils.isHtmlFragment('<div></div>'), true);
65+
assert.equal(
66+
utils.isHtmlFragment('<!-- COMMENT --><!DOCTYPE><my-element></my-element>'),
6167
false,
6268
);
63-
expect(
69+
assert.equal(
6470
utils.isHtmlFragment(`<!--
6571
COMMENT
6672
--><!DOCTYPE><my-element></my-element>`),
67-
).to.equal(false);
68-
expect(utils.isHtmlFragment('<!DOCTYPE><my-element></my-element>')).to.equal(false);
69-
expect(utils.isHtmlFragment(' <!DOCTYPE><my-element></my-element>')).to.equal(false);
70-
expect(utils.isHtmlFragment(' <html><my-element></my-element></html>')).to.equal(false);
71-
expect(utils.isHtmlFragment(' <body><my-element></my-element></body>')).to.equal(false);
72-
expect(utils.isHtmlFragment(' <head><my-element></my-element></head>')).to.equal(false);
73+
false,
74+
);
75+
assert.equal(utils.isHtmlFragment('<!DOCTYPE><my-element></my-element>'), false);
76+
assert.equal(utils.isHtmlFragment(' <!DOCTYPE><my-element></my-element>'), false);
77+
assert.equal(utils.isHtmlFragment(' <html><my-element></my-element></html>'), false);
78+
assert.equal(utils.isHtmlFragment(' <body><my-element></my-element></body>'), false);
79+
assert.equal(utils.isHtmlFragment(' <head><my-element></my-element></head>'), false);
7380
});
7481
});
7582

7683
describe('getAttributes()', () => {
7784
it('returns the attributes of an element', () => {
7885
const el = utils.createElement('my-element', { foo: 'bar', x: '' });
79-
expect(utils.getAttributes(el)).to.eql({ foo: 'bar', x: '' });
86+
assert.deepEqual(utils.getAttributes(el), { foo: 'bar', x: '' });
8087
});
8188

8289
it('returns an empty object if there are no attributes', () => {
8390
const el = utils.createElement('my-element');
84-
expect(utils.getAttributes(el)).to.eql({});
91+
assert.deepEqual(utils.getAttributes(el), {});
8592
});
8693
});
8794

8895
describe('getAttribute()', () => {
8996
it('returns a single attribute', () => {
9097
const el = utils.createElement('my-element', { foo: 'bar', x: '' });
91-
expect(utils.getAttribute(el, 'foo')).to.eql('bar');
98+
assert.equal(utils.getAttribute(el, 'foo'), 'bar');
9299
});
93100

94101
it('returns undefined if the attribute was not found', () => {
95102
const el = utils.createElement('my-element', { foo: 'bar', x: '' });
96-
expect(utils.getAttribute(el, 'y')).to.eql(undefined);
103+
assert.equal(utils.getAttribute(el, 'y'), undefined);
97104
});
98105
});
99106

@@ -103,7 +110,8 @@ describe('parse5-utils', () => {
103110
const el = utils.createElement('my-element');
104111
utils.appendChild(doc, el);
105112
utils.setAttribute(el, 'foo', 'bar');
106-
expect(serialize(doc)).to.eql(
113+
assert.equal(
114+
serialize(doc),
107115
'<html><head></head><body></body></html><my-element foo="bar"></my-element>',
108116
);
109117
});
@@ -113,7 +121,8 @@ describe('parse5-utils', () => {
113121
const el = utils.createElement('my-element', { foo: 'bar' });
114122
utils.appendChild(doc, el);
115123
utils.setAttribute(el, 'foo', 'not-bar');
116-
expect(serialize(doc)).to.eql(
124+
assert.equal(
125+
serialize(doc),
117126
'<html><head></head><body></body></html><my-element foo="not-bar"></my-element>',
118127
);
119128
});
@@ -125,7 +134,8 @@ describe('parse5-utils', () => {
125134
const el = utils.createElement('my-element');
126135
utils.appendChild(doc, el);
127136
utils.setAttributes(el, { foo: 'bar', lorem: 'ipsum', x: undefined });
128-
expect(serialize(doc)).to.eql(
137+
assert.equal(
138+
serialize(doc),
129139
'<html><head></head><body></body></html><my-element foo="bar" lorem="ipsum"></my-element>',
130140
);
131141
});
@@ -137,7 +147,8 @@ describe('parse5-utils', () => {
137147
const el = utils.createElement('my-element', { foo: 'bar', x: 'y' });
138148
utils.appendChild(doc, el);
139149
utils.removeAttribute(el, 'x');
140-
expect(serialize(doc)).to.eql(
150+
assert.equal(
151+
serialize(doc),
141152
'<html><head></head><body></body></html><my-element foo="bar"></my-element>',
142153
);
143154
});
@@ -148,7 +159,7 @@ describe('parse5-utils', () => {
148159
const doc = parse('<html><body><div id="myDiv">Hello world</div></body></html>');
149160
const myDiv = utils.findElement(doc, e => getAttribute(e, 'id') === 'myDiv');
150161
if (!myDiv) throw new Error();
151-
expect(getTextContent(myDiv)).to.equal('Hello world');
162+
assert.equal(getTextContent(myDiv), 'Hello world');
152163
});
153164

154165
it('returns multiple nodes text', () => {
@@ -157,7 +168,7 @@ describe('parse5-utils', () => {
157168
);
158169
const myDiv = utils.findElement(doc, e => getAttribute(e, 'id') === 'myDiv');
159170
if (!myDiv) throw new Error();
160-
expect(getTextContent(myDiv)).to.equal('Top levelBeforeABAfter');
171+
assert.equal(getTextContent(myDiv), 'Top levelBeforeABAfter');
161172
});
162173
});
163174

@@ -167,7 +178,8 @@ describe('parse5-utils', () => {
167178
const el = utils.createElement('script');
168179
utils.setTextContent(el, 'foo bar');
169180
utils.appendChild(doc, el);
170-
expect(serialize(doc)).to.equal(
181+
assert.equal(
182+
serialize(doc),
171183
'<html><head></head><body></body></html><script>foo bar</script>',
172184
);
173185
});
@@ -179,7 +191,7 @@ describe('parse5-utils', () => {
179191
const div = findElement(doc, e => utils.getAttribute(e, 'id') === 'myDiv');
180192
if (!div) throw new Error('element not found');
181193
utils.remove(div);
182-
expect(serialize(doc)).to.equal('<html><head></head><body></body></html>');
194+
assert.equal(serialize(doc), '<html><head></head><body></body></html>');
183195
});
184196
});
185197

@@ -198,7 +210,7 @@ describe('parse5-utils', () => {
198210
if (!found) {
199211
throw new Error('No element found.');
200212
}
201-
expect(utils.getAttribute(found, 'foo')).to.equal('2');
213+
assert.equal(utils.getAttribute(found, 'foo'), '2');
202214
});
203215

204216
it('returns the first match', () => {
@@ -215,8 +227,8 @@ describe('parse5-utils', () => {
215227
if (!found) {
216228
throw new Error('No element found.');
217229
}
218-
expect(utils.getAttribute(found, 'foo')).to.equal('bar');
219-
expect(utils.getAttribute(found, 'index')).to.equal('1');
230+
assert.equal(utils.getAttribute(found, 'foo'), 'bar');
231+
assert.equal(utils.getAttribute(found, 'index'), '1');
220232
});
221233

222234
it('returns nested elements', () => {
@@ -239,7 +251,7 @@ describe('parse5-utils', () => {
239251
if (!found) {
240252
throw new Error('No element found.');
241253
}
242-
expect(found).to.exist;
254+
assert.ok(found);
243255
});
244256
});
245257

@@ -255,8 +267,8 @@ describe('parse5-utils', () => {
255267
</html>
256268
`);
257269
const found = utils.findElements(doc, el => utils.getAttribute(el, 'foo') === '2');
258-
expect(found.length).to.equal(1);
259-
expect(utils.getAttribute(found[0], 'foo')).to.equal('2');
270+
assert.equal(found.length, 1);
271+
assert.equal(utils.getAttribute(found[0], 'foo'), '2');
260272
});
261273

262274
it('returns multiple matched elements', () => {
@@ -270,9 +282,9 @@ describe('parse5-utils', () => {
270282
</html>
271283
`);
272284
const found = utils.findElements(doc, el => utils.getAttribute(el, 'foo') === 'bar');
273-
expect(found.length).to.equal(3);
285+
assert.equal(found.length, 3);
274286
const indices = found.map(f => utils.getAttribute(f, 'index'));
275-
expect(indices).to.eql(['1', '2', '3']);
287+
assert.deepEqual(indices, ['1', '2', '3']);
276288
});
277289

278290
it('returns an empty array when there are no matches', () => {
@@ -286,7 +298,7 @@ describe('parse5-utils', () => {
286298
</html>
287299
`);
288300
const found = utils.findElements(doc, el => utils.hasAttribute(el, 'non-existing'));
289-
expect(found.length).to.equal(0);
301+
assert.equal(found.length, 0);
290302
});
291303

292304
it('returns child elements within template elements', () => {
@@ -301,7 +313,7 @@ describe('parse5-utils', () => {
301313
`);
302314

303315
const found = utils.findElements(doc, el => utils.hasAttribute(el, 'src'));
304-
expect(found.length).to.equal(1);
316+
assert.equal(found.length, 1);
305317
});
306318
});
307319

@@ -310,15 +322,16 @@ describe('parse5-utils', () => {
310322
const document = '<html><head></head><body></body></html>';
311323
const result = utils.prependToDocument(document, '<div>Hello world</div>');
312324
if (!result) throw new Error();
313-
expect(result).to.equal('<html><head><div>Hello world</div></head><body></body></html>');
325+
assert.equal(result, '<html><head><div>Hello world</div></head><body></body></html>');
314326
});
315327

316328
it('injects before other elements', () => {
317329
const document =
318330
'<html><head><div>A</div><div>B</div></head><body><div>C</div></body></html>';
319331
const result = utils.prependToDocument(document, '<div>Hello world</div>');
320332
if (!result) throw new Error();
321-
expect(result).to.equal(
333+
assert.equal(
334+
result,
322335
'<html><head><div>Hello world</div><div>A</div><div>B</div></head><body><div>C</div></body></html>',
323336
);
324337
});
@@ -327,13 +340,13 @@ describe('parse5-utils', () => {
327340
const document = '<html><body><div>A</div></body></html>';
328341
const result = utils.prependToDocument(document, '<div>Hello world</div>');
329342
if (!result) throw new Error();
330-
expect(result).to.equal('<html><body><div>Hello world</div><div>A</div></body></html>');
343+
assert.equal(result, '<html><body><div>Hello world</div><div>A</div></body></html>');
331344
});
332345

333346
it('uses AST manipulation if there is no head or body', () => {
334347
const document = '<html></html>';
335348
const result = utils.prependToDocument(document, '<div>A</div><div>B</div>');
336-
expect(result).to.equal('<html><head><div>A</div><div>B</div></head><body></body></html>');
349+
assert.equal(result, '<html><head><div>A</div><div>B</div></head><body></body></html>');
337350
});
338351
});
339352

@@ -342,15 +355,16 @@ describe('parse5-utils', () => {
342355
const document = '<html><head></head><body></body></html>';
343356
const result = utils.appendToDocument(document, '<div>Hello world</div>');
344357
if (!result) throw new Error();
345-
expect(result).to.equal('<html><head></head><body><div>Hello world</div></body></html>');
358+
assert.equal(result, '<html><head></head><body><div>Hello world</div></body></html>');
346359
});
347360

348361
it('injects after other elements', () => {
349362
const document =
350363
'<html><head><script>A</script></head><body><script>B</script><script>C</script></body></html>';
351364
const result = utils.appendToDocument(document, '<div>Hello world</div>');
352365
if (!result) throw new Error();
353-
expect(result).to.equal(
366+
assert.equal(
367+
result,
354368
'<html><head><script>A</script></head><body><script>B</script><script>C</script><div>Hello world</div></body></html>',
355369
);
356370
});
@@ -359,13 +373,13 @@ describe('parse5-utils', () => {
359373
const document = '<html><head><script>A</script></head></html>';
360374
const result = utils.appendToDocument(document, '<div>Hello world</div>');
361375
if (!result) throw new Error();
362-
expect(result).to.equal('<html><head><script>A</script><div>Hello world</div></head></html>');
376+
assert.equal(result, '<html><head><script>A</script><div>Hello world</div></head></html>');
363377
});
364378

365379
it('returns null if there is no head or body', () => {
366380
const document = '<html></html>';
367381
const result = utils.appendToDocument(document, '<div>A</div><div>B</div>');
368-
expect(result).to.equal('<html><head></head><body><div>A</div><div>B</div></body></html>');
382+
assert.equal(result, '<html><head></head><body><div>A</div><div>B</div></body></html>');
369383
});
370384
});
371385
});

0 commit comments

Comments
 (0)