Skip to content

Commit c847210

Browse files
committed
refactor(dev-server-legacy): migrate tests from mocha/chai to node:test
Replace mocha globals and chai assertions with node:test and node:assert/strict. First TypeScript test migration in the series. - `import { expect } from 'chai'` -> `import assert from 'node:assert/strict'` - Add `import { describe, it } from 'node:test'` - `expect(x).to.equal(y)` -> `assert.equal(x, y)` - `this.timeout(10000)` -> `{ timeout: 10000 }` option on describe - `__dirname` -> `import.meta.dirname` - `../src/legacyPlugin.js` -> `../dist/legacyPlugin.js` - `./userAgents.js` -> `./userAgents.ts` - Add `--test-force-exit` to prevent test hangs from open handles Assisted-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 597fb32 commit c847210

3 files changed

Lines changed: 23 additions & 25 deletions

File tree

packages/dev-server-legacy/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"scripts": {
2828
"build": "tsc",
2929
"start": "wds --open --config demo/server.config.mjs",
30-
"test:node": "mocha \"test/**/*.test.ts\" --require ts-node/register --reporter dot",
31-
"test:watch": "mocha \"test/**/*.test.ts\" --require ts-node/register --watch --watch-files src,test"
30+
"test:node": "node --test --test-force-exit test/**/*.test.ts",
31+
"test:watch": "node --test --test-force-exit --watch test/**/*.test.ts"
3232
},
3333
"files": [
3434
"*.d.ts",

packages/dev-server-legacy/test/transform-html.test.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { expect } from 'chai';
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert/strict';
23
import { createTestServer } from '@web/dev-server-core/test-helpers';
34
import { fetchText, expectIncludes } from '@web/dev-server-core/test-helpers';
45

5-
import { legacyPlugin } from '../src/legacyPlugin.js';
6-
import { modernUserAgents, legacyUserAgents } from './userAgents.js';
6+
import { legacyPlugin } from '../dist/legacyPlugin.js';
7+
import { modernUserAgents, legacyUserAgents } from './userAgents.ts';
78

89
const htmlBody = `
910
<html>
@@ -25,12 +26,10 @@ const inlineScriptHtmlBody = `
2526
</body>
2627
</html>`;
2728

28-
describe('legacyPlugin - transform html', function () {
29-
this.timeout(10000);
30-
29+
describe('legacyPlugin - transform html', { timeout: 10000 }, () => {
3130
it(`does not do any work on a modern browser`, async () => {
3231
const { server, host } = await createTestServer({
33-
rootDir: __dirname,
32+
rootDir: import.meta.dirname,
3433
plugins: [
3534
{
3635
name: 'test',
@@ -48,13 +47,13 @@ describe('legacyPlugin - transform html', function () {
4847
headers: { 'user-agent': modernUserAgents['Chrome 78'] },
4948
});
5049

51-
expect(text.trim()).to.equal(htmlBody.trim());
50+
assert.equal(text.trim(), htmlBody.trim());
5251
server.stop();
5352
});
5453

5554
it(`injects polyfills into the HTML page on legacy browsers`, async () => {
5655
const { server, host } = await createTestServer({
57-
rootDir: __dirname,
56+
rootDir: import.meta.dirname,
5857
plugins: [
5958
{
6059
name: 'test',
@@ -80,7 +79,7 @@ describe('legacyPlugin - transform html', function () {
8079

8180
it(`injects systemjs param to inline modules`, async () => {
8281
const { server, host } = await createTestServer({
83-
rootDir: __dirname,
82+
rootDir: import.meta.dirname,
8483
plugins: [
8584
{
8685
name: 'test',
@@ -104,7 +103,7 @@ describe('legacyPlugin - transform html', function () {
104103

105104
it(`handles inline scripts`, async () => {
106105
const { server, host } = await createTestServer({
107-
rootDir: __dirname,
106+
rootDir: import.meta.dirname,
108107
plugins: [
109108
{
110109
name: 'test',
@@ -131,7 +130,7 @@ describe('legacyPlugin - transform html', function () {
131130

132131
it(`can request inline scripts`, async () => {
133132
const { server, host } = await createTestServer({
134-
rootDir: __dirname,
133+
rootDir: import.meta.dirname,
135134
plugins: [
136135
{
137136
name: 'test',
@@ -158,7 +157,7 @@ describe('legacyPlugin - transform html', function () {
158157

159158
it(`includes url parameters in inline script key`, async () => {
160159
const { server, host } = await createTestServer({
161-
rootDir: __dirname,
160+
rootDir: import.meta.dirname,
162161
plugins: [
163162
{
164163
name: 'test',

packages/dev-server-legacy/test/transform-js.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { expect } from 'chai';
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert/strict';
23
import { createTestServer } from '@web/dev-server-core/test-helpers';
34
import { fetchText, expectIncludes, expectNotIncludes } from '@web/dev-server-core/test-helpers';
45

5-
import { legacyPlugin } from '../src/legacyPlugin.js';
6-
import { modernUserAgents, legacyUserAgents } from './userAgents.js';
6+
import { legacyPlugin } from '../dist/legacyPlugin.js';
7+
import { modernUserAgents, legacyUserAgents } from './userAgents.ts';
78

89
const modernCode = `
910
class Foo {
@@ -16,13 +17,11 @@ async function doImport() {
1617
1718
console.log(window?.foo?.bar);`;
1819

19-
describe('legacyPlugin - transform js', function () {
20-
this.timeout(10000);
21-
20+
describe('legacyPlugin - transform js', { timeout: 10000 }, () => {
2221
for (const [name, userAgent] of Object.entries(modernUserAgents)) {
2322
it(`does not do any work on ${name}`, async () => {
2423
const { server, host } = await createTestServer({
25-
rootDir: __dirname,
24+
rootDir: import.meta.dirname,
2625
plugins: [
2726
{
2827
name: 'test',
@@ -39,15 +38,15 @@ describe('legacyPlugin - transform js', function () {
3938
const text = await fetchText(`${host}/app.js`, {
4039
headers: { 'user-agent': userAgent },
4140
});
42-
expect(text.trim()).to.equal(modernCode.trim());
41+
assert.equal(text.trim(), modernCode.trim());
4342
server.stop();
4443
});
4544
}
4645

4746
for (const [name, userAgent] of Object.entries(legacyUserAgents)) {
4847
it(`transforms to es5 on ${name}`, async () => {
4948
const { server, host } = await createTestServer({
50-
rootDir: __dirname,
49+
rootDir: import.meta.dirname,
5150
plugins: [
5251
{
5352
name: 'test',
@@ -78,7 +77,7 @@ describe('legacyPlugin - transform js', function () {
7877

7978
it(`transforms to SystemJS when systemjs paramater is given ${name}`, async () => {
8079
const { server, host } = await createTestServer({
81-
rootDir: __dirname,
80+
rootDir: import.meta.dirname,
8281
plugins: [
8382
{
8483
name: 'test',

0 commit comments

Comments
 (0)