From 7c3b608f8786c2aab7ac2a93cd60475e48eb5b3b Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Sat, 30 May 2026 21:19:44 +0300 Subject: [PATCH] refactor(parse5-utils): migrate tests from mocha/chai to node:test Replace mocha globals and chai assertions with node:test and node:assert/strict. Update test:node script to use `node --test`. - `expect(x).to.equal(y)` -> `assert.equal(x, y)` - `expect(x).to.eql(y)` -> `assert.deepEqual(x, y)` - `expect(x).to.exist` -> `assert.ok(x)` - Add `--test-force-exit` to prevent test hangs from open handles Assisted-By: Claude Opus 4.6 (1M context) --- packages/parse5-utils/package.json | 4 +- packages/parse5-utils/test/index.test.js | 110 +++++++++++++---------- 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/packages/parse5-utils/package.json b/packages/parse5-utils/package.json index 7456606506..75f932268a 100644 --- a/packages/parse5-utils/package.json +++ b/packages/parse5-utils/package.json @@ -26,8 +26,8 @@ }, "scripts": { "build": "tsc", - "test:node": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --reporter dot", - "test:watch": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --watch" + "test:node": "node --test --test-force-exit test/**/*.test.js", + "test:watch": "node --test --test-force-exit --watch test/**/*.test.js" }, "files": [ "*.d.ts", diff --git a/packages/parse5-utils/test/index.test.js b/packages/parse5-utils/test/index.test.js index 28db8eb7a7..5536700be8 100644 --- a/packages/parse5-utils/test/index.test.js +++ b/packages/parse5-utils/test/index.test.js @@ -1,4 +1,5 @@ -const { expect } = require('chai'); +const { describe, it } = require('node:test'); +const assert = require('node:assert/strict'); const { parse, serialize } = require('parse5'); const { getAttribute, getTextContent, findElement } = require('../src/index'); const utils = require('../src/index'); @@ -9,7 +10,8 @@ describe('parse5-utils', () => { const doc = parse(''); const el = utils.createElement('my-element'); utils.appendChild(doc, el); - expect(serialize(doc)).to.equal( + assert.equal( + serialize(doc), '', ); }); @@ -18,7 +20,8 @@ describe('parse5-utils', () => { const doc = parse(''); const el = utils.createElement('my-element', { foo: 'bar', x: '' }); utils.appendChild(doc, el); - expect(serialize(doc)).to.equal( + assert.equal( + serialize(doc), '', ); }); @@ -29,14 +32,15 @@ describe('parse5-utils', () => { const doc = parse(''); const el = utils.createScript(); utils.appendChild(doc, el); - expect(serialize(doc)).to.equal(''); + assert.equal(serialize(doc), ''); }); it('create a script with attributes', () => { const doc = parse(''); const el = utils.createScript({ type: 'module' }); utils.appendChild(doc, el); - expect(serialize(doc)).to.equal( + assert.equal( + serialize(doc), '', ); }); @@ -45,7 +49,8 @@ describe('parse5-utils', () => { const doc = parse(''); const el = utils.createScript({ type: 'module' }, 'console.log("x");'); utils.appendChild(doc, el); - expect(serialize(doc)).to.equal( + assert.equal( + serialize(doc), '', ); }); @@ -53,47 +58,49 @@ describe('parse5-utils', () => { describe('isHtmlFragment()', () => { it('returns whether a HTML string is a fragment', () => { - expect(utils.isHtmlFragment('')).to.equal(true); - expect(utils.isHtmlFragment('')).to.equal(true); - expect(utils.isHtmlFragment('foo')).to.equal(true); - expect(utils.isHtmlFragment('
')).to.equal(true); - expect(utils.isHtmlFragment('')).to.equal( + assert.equal(utils.isHtmlFragment(''), true); + assert.equal(utils.isHtmlFragment(''), true); + assert.equal(utils.isHtmlFragment('foo'), true); + assert.equal(utils.isHtmlFragment('
'), true); + assert.equal( + utils.isHtmlFragment(''), false, ); - expect( + assert.equal( utils.isHtmlFragment(``), - ).to.equal(false); - expect(utils.isHtmlFragment('')).to.equal(false); - expect(utils.isHtmlFragment(' ')).to.equal(false); - expect(utils.isHtmlFragment(' ')).to.equal(false); - expect(utils.isHtmlFragment(' ')).to.equal(false); - expect(utils.isHtmlFragment(' ')).to.equal(false); + false, + ); + assert.equal(utils.isHtmlFragment(''), false); + assert.equal(utils.isHtmlFragment(' '), false); + assert.equal(utils.isHtmlFragment(' '), false); + assert.equal(utils.isHtmlFragment(' '), false); + assert.equal(utils.isHtmlFragment(' '), false); }); }); describe('getAttributes()', () => { it('returns the attributes of an element', () => { const el = utils.createElement('my-element', { foo: 'bar', x: '' }); - expect(utils.getAttributes(el)).to.eql({ foo: 'bar', x: '' }); + assert.deepEqual(utils.getAttributes(el), { foo: 'bar', x: '' }); }); it('returns an empty object if there are no attributes', () => { const el = utils.createElement('my-element'); - expect(utils.getAttributes(el)).to.eql({}); + assert.deepEqual(utils.getAttributes(el), {}); }); }); describe('getAttribute()', () => { it('returns a single attribute', () => { const el = utils.createElement('my-element', { foo: 'bar', x: '' }); - expect(utils.getAttribute(el, 'foo')).to.eql('bar'); + assert.equal(utils.getAttribute(el, 'foo'), 'bar'); }); it('returns undefined if the attribute was not found', () => { const el = utils.createElement('my-element', { foo: 'bar', x: '' }); - expect(utils.getAttribute(el, 'y')).to.eql(undefined); + assert.equal(utils.getAttribute(el, 'y'), undefined); }); }); @@ -103,7 +110,8 @@ describe('parse5-utils', () => { const el = utils.createElement('my-element'); utils.appendChild(doc, el); utils.setAttribute(el, 'foo', 'bar'); - expect(serialize(doc)).to.eql( + assert.equal( + serialize(doc), '', ); }); @@ -113,7 +121,8 @@ describe('parse5-utils', () => { const el = utils.createElement('my-element', { foo: 'bar' }); utils.appendChild(doc, el); utils.setAttribute(el, 'foo', 'not-bar'); - expect(serialize(doc)).to.eql( + assert.equal( + serialize(doc), '', ); }); @@ -125,7 +134,8 @@ describe('parse5-utils', () => { const el = utils.createElement('my-element'); utils.appendChild(doc, el); utils.setAttributes(el, { foo: 'bar', lorem: 'ipsum', x: undefined }); - expect(serialize(doc)).to.eql( + assert.equal( + serialize(doc), '', ); }); @@ -137,7 +147,8 @@ describe('parse5-utils', () => { const el = utils.createElement('my-element', { foo: 'bar', x: 'y' }); utils.appendChild(doc, el); utils.removeAttribute(el, 'x'); - expect(serialize(doc)).to.eql( + assert.equal( + serialize(doc), '', ); }); @@ -148,7 +159,7 @@ describe('parse5-utils', () => { const doc = parse('
Hello world
'); const myDiv = utils.findElement(doc, e => getAttribute(e, 'id') === 'myDiv'); if (!myDiv) throw new Error(); - expect(getTextContent(myDiv)).to.equal('Hello world'); + assert.equal(getTextContent(myDiv), 'Hello world'); }); it('returns multiple nodes text', () => { @@ -157,7 +168,7 @@ describe('parse5-utils', () => { ); const myDiv = utils.findElement(doc, e => getAttribute(e, 'id') === 'myDiv'); if (!myDiv) throw new Error(); - expect(getTextContent(myDiv)).to.equal('Top levelBeforeABAfter'); + assert.equal(getTextContent(myDiv), 'Top levelBeforeABAfter'); }); }); @@ -167,7 +178,8 @@ describe('parse5-utils', () => { const el = utils.createElement('script'); utils.setTextContent(el, 'foo bar'); utils.appendChild(doc, el); - expect(serialize(doc)).to.equal( + assert.equal( + serialize(doc), '', ); }); @@ -179,7 +191,7 @@ describe('parse5-utils', () => { const div = findElement(doc, e => utils.getAttribute(e, 'id') === 'myDiv'); if (!div) throw new Error('element not found'); utils.remove(div); - expect(serialize(doc)).to.equal(''); + assert.equal(serialize(doc), ''); }); }); @@ -198,7 +210,7 @@ describe('parse5-utils', () => { if (!found) { throw new Error('No element found.'); } - expect(utils.getAttribute(found, 'foo')).to.equal('2'); + assert.equal(utils.getAttribute(found, 'foo'), '2'); }); it('returns the first match', () => { @@ -215,8 +227,8 @@ describe('parse5-utils', () => { if (!found) { throw new Error('No element found.'); } - expect(utils.getAttribute(found, 'foo')).to.equal('bar'); - expect(utils.getAttribute(found, 'index')).to.equal('1'); + assert.equal(utils.getAttribute(found, 'foo'), 'bar'); + assert.equal(utils.getAttribute(found, 'index'), '1'); }); it('returns nested elements', () => { @@ -239,7 +251,7 @@ describe('parse5-utils', () => { if (!found) { throw new Error('No element found.'); } - expect(found).to.exist; + assert.ok(found); }); }); @@ -255,8 +267,8 @@ describe('parse5-utils', () => { `); const found = utils.findElements(doc, el => utils.getAttribute(el, 'foo') === '2'); - expect(found.length).to.equal(1); - expect(utils.getAttribute(found[0], 'foo')).to.equal('2'); + assert.equal(found.length, 1); + assert.equal(utils.getAttribute(found[0], 'foo'), '2'); }); it('returns multiple matched elements', () => { @@ -270,9 +282,9 @@ describe('parse5-utils', () => { `); const found = utils.findElements(doc, el => utils.getAttribute(el, 'foo') === 'bar'); - expect(found.length).to.equal(3); + assert.equal(found.length, 3); const indices = found.map(f => utils.getAttribute(f, 'index')); - expect(indices).to.eql(['1', '2', '3']); + assert.deepEqual(indices, ['1', '2', '3']); }); it('returns an empty array when there are no matches', () => { @@ -286,7 +298,7 @@ describe('parse5-utils', () => { `); const found = utils.findElements(doc, el => utils.hasAttribute(el, 'non-existing')); - expect(found.length).to.equal(0); + assert.equal(found.length, 0); }); it('returns child elements within template elements', () => { @@ -301,7 +313,7 @@ describe('parse5-utils', () => { `); const found = utils.findElements(doc, el => utils.hasAttribute(el, 'src')); - expect(found.length).to.equal(1); + assert.equal(found.length, 1); }); }); @@ -310,7 +322,7 @@ describe('parse5-utils', () => { const document = ''; const result = utils.prependToDocument(document, '
Hello world
'); if (!result) throw new Error(); - expect(result).to.equal('
Hello world
'); + assert.equal(result, '
Hello world
'); }); it('injects before other elements', () => { @@ -318,7 +330,8 @@ describe('parse5-utils', () => { '
A
B
C
'; const result = utils.prependToDocument(document, '
Hello world
'); if (!result) throw new Error(); - expect(result).to.equal( + assert.equal( + result, '
Hello world
A
B
C
', ); }); @@ -327,13 +340,13 @@ describe('parse5-utils', () => { const document = '
A
'; const result = utils.prependToDocument(document, '
Hello world
'); if (!result) throw new Error(); - expect(result).to.equal('
Hello world
A
'); + assert.equal(result, '
Hello world
A
'); }); it('uses AST manipulation if there is no head or body', () => { const document = ''; const result = utils.prependToDocument(document, '
A
B
'); - expect(result).to.equal('
A
B
'); + assert.equal(result, '
A
B
'); }); }); @@ -342,7 +355,7 @@ describe('parse5-utils', () => { const document = ''; const result = utils.appendToDocument(document, '
Hello world
'); if (!result) throw new Error(); - expect(result).to.equal('
Hello world
'); + assert.equal(result, '
Hello world
'); }); it('injects after other elements', () => { @@ -350,7 +363,8 @@ describe('parse5-utils', () => { ''; const result = utils.appendToDocument(document, '
Hello world
'); if (!result) throw new Error(); - expect(result).to.equal( + assert.equal( + result, '
Hello world
', ); }); @@ -359,13 +373,13 @@ describe('parse5-utils', () => { const document = ''; const result = utils.appendToDocument(document, '
Hello world
'); if (!result) throw new Error(); - expect(result).to.equal('
Hello world
'); + assert.equal(result, '
Hello world
'); }); it('returns null if there is no head or body', () => { const document = ''; const result = utils.appendToDocument(document, '
A
B
'); - expect(result).to.equal('
A
B
'); + assert.equal(result, '
A
B
'); }); }); });