Skip to content

Commit 0522abc

Browse files
authored
Merge pull request #3093 from modernweb-dev/migrate/dev-server-hmr-node-test
refactor(dev-server-hmr): migrate tests to node:test
2 parents e87fd72 + ac9c291 commit 0522abc

4 files changed

Lines changed: 98 additions & 83 deletions

File tree

packages/dev-server-hmr/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"build": "tsc",
2929
"start:lit-html": "wds --config demo/lit-html/server.config.mjs",
3030
"start:vanilla": "wds --config demo/vanilla/server.config.mjs",
31-
"test:node": "mocha \"test/**/*.test.ts\" --require ts-node/register --reporter dot",
32-
"test:watch": "mocha \"test/**/*.test.ts\" --require ts-node/register --watch --watch-files src,test"
31+
"test:node": "node --experimental-strip-types --test --test-force-exit test/**/*.test.ts",
32+
"test:watch": "node --experimental-strip-types --test --test-force-exit --watch test/**/*.test.ts"
3333
},
3434
"files": [
3535
"*.d.ts",

packages/dev-server-hmr/test/HmrPlugin.test.ts

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
import { expect } from 'chai';
2-
import { stubMethod, restore as restoreStubs } from 'hanbi';
3-
import { createTestServer, fetchText, expectIncludes } from '@web/dev-server-core/test-helpers';
1+
import { describe, it, afterEach, mock } from 'node:test';
2+
import assert from 'node:assert/strict';
3+
import {
4+
createTestServer,
5+
fetchText,
6+
expectIncludes,
7+
expectNotIncludes,
8+
} from '@web/dev-server-core/test-helpers';
49
import { posix as pathUtil } from 'path';
510

6-
import { hmrPlugin } from '../src/index.js';
7-
import { NAME_HMR_CLIENT_IMPORT } from '../src/HmrPlugin.js';
8-
import { mockFile, mockFiles } from './utils.js';
11+
import { hmrPlugin } from '../dist/index.js';
12+
import { NAME_HMR_CLIENT_IMPORT } from '../dist/HmrPlugin.js';
13+
import { mockFile, mockFiles } from './utils.ts';
914

1015
describe('HmrPlugin', () => {
1116
afterEach(async () => {
12-
restoreStubs();
17+
mock.restoreAll();
1318
});
1419

1520
it('should emit update for tracked files', async () => {
1621
const { server, host } = await createTestServer({
17-
rootDir: __dirname,
22+
rootDir: import.meta.dirname,
1823
plugins: [
1924
mockFile(
2025
'/foo.js',
@@ -26,12 +31,13 @@ describe('HmrPlugin', () => {
2631
],
2732
});
2833
const { fileWatcher, webSockets } = server;
29-
const stub = stubMethod(webSockets!, 'send');
34+
const stub = mock.method(webSockets!, 'send');
3035
try {
3136
await fetch(`${host}/foo.js`);
32-
fileWatcher.emit('change', pathUtil.join(__dirname, '/foo.js'));
37+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/foo.js'));
3338

34-
expect(stub.firstCall!.args[0]).to.equal(
39+
assert.equal(
40+
stub.mock.calls[0].arguments[0],
3541
JSON.stringify({
3642
type: 'hmr:update',
3743
url: '/foo.js',
@@ -44,7 +50,7 @@ describe('HmrPlugin', () => {
4450

4551
it('should bubble updates for changed dependencies', async () => {
4652
const { server, host } = await createTestServer({
47-
rootDir: __dirname,
53+
rootDir: import.meta.dirname,
4854
plugins: [
4955
mockFile(
5056
'/foo.js',
@@ -55,13 +61,14 @@ describe('HmrPlugin', () => {
5561
],
5662
});
5763
const { fileWatcher, webSockets } = server;
58-
const stub = stubMethod(webSockets!, 'send');
64+
const stub = mock.method(webSockets!, 'send');
5965
try {
6066
await fetch(`${host}/foo.js`);
6167
await fetch(`${host}/bar.js`);
62-
fileWatcher.emit('change', pathUtil.join(__dirname, '/bar.js'));
68+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/bar.js'));
6369

64-
expect(stub.firstCall!.args[0]).to.equal(
70+
assert.equal(
71+
stub.mock.calls[0].arguments[0],
6572
JSON.stringify({
6673
type: 'hmr:update',
6774
url: '/foo.js',
@@ -74,22 +81,23 @@ describe('HmrPlugin', () => {
7481

7582
it('should not reload if dependent handles change', async () => {
7683
const { server, host } = await createTestServer({
77-
rootDir: __dirname,
84+
rootDir: import.meta.dirname,
7885
plugins: [
7986
mockFile('/foo.js', `import '/bar.js'; import.meta.hot.accept();`),
8087
mockFile('/bar.js', `export const s = 808;`),
8188
hmrPlugin(),
8289
],
8390
});
8491
const { fileWatcher, webSockets } = server;
85-
const stub = stubMethod(webSockets!, 'send');
92+
const stub = mock.method(webSockets!, 'send');
8693
try {
8794
await fetch(`${host}/foo.js`);
8895
await fetch(`${host}/bar.js`);
89-
fileWatcher.emit('change', pathUtil.join(__dirname, '/bar.js'));
96+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/bar.js'));
9097

91-
expect(stub.callCount).to.equal(1);
92-
expect(stub.firstCall!.args[0]).to.equal(
98+
assert.equal(stub.mock.callCount(), 1);
99+
assert.equal(
100+
stub.mock.calls[0].arguments[0],
93101
JSON.stringify({
94102
type: 'hmr:update',
95103
url: '/foo.js',
@@ -102,22 +110,23 @@ describe('HmrPlugin', () => {
102110

103111
it('should reload if dependents do not handle change', async () => {
104112
const { server, host } = await createTestServer({
105-
rootDir: __dirname,
113+
rootDir: import.meta.dirname,
106114
plugins: [
107115
mockFile('/foo.js', `import '/bar.js';`),
108116
mockFile('/bar.js', `export const s = 808;`),
109117
hmrPlugin(),
110118
],
111119
});
112120
const { fileWatcher, webSockets } = server;
113-
const stub = stubMethod(webSockets!, 'send');
121+
const stub = mock.method(webSockets!, 'send');
114122
try {
115123
await fetch(`${host}/foo.js`);
116124
await fetch(`${host}/bar.js`);
117-
fileWatcher.emit('change', pathUtil.join(__dirname, '/bar.js'));
125+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/bar.js'));
118126

119-
expect(stub.callCount).to.equal(1);
120-
expect(stub.firstCall!.args[0]).to.equal(
127+
assert.equal(stub.mock.callCount(), 1);
128+
assert.equal(
129+
stub.mock.calls[0].arguments[0],
121130
JSON.stringify({
122131
type: 'hmr:reload',
123132
}),
@@ -129,7 +138,7 @@ describe('HmrPlugin', () => {
129138

130139
it('handles dependencies referenced relatively', async () => {
131140
const { server, host } = await createTestServer({
132-
rootDir: __dirname,
141+
rootDir: import.meta.dirname,
133142
plugins: [
134143
mockFile(
135144
'/root/foo.js',
@@ -140,13 +149,14 @@ describe('HmrPlugin', () => {
140149
],
141150
});
142151
const { fileWatcher, webSockets } = server;
143-
const stub = stubMethod(webSockets!, 'send');
152+
const stub = mock.method(webSockets!, 'send');
144153
try {
145154
await fetch(`${host}/root/foo.js`);
146155
await fetch(`${host}/root/bar.js`);
147-
fileWatcher.emit('change', pathUtil.join(__dirname, '/root/bar.js'));
156+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/root/bar.js'));
148157

149-
expect(stub.firstCall!.args[0]).to.equal(
158+
assert.equal(
159+
stub.mock.calls[0].arguments[0],
150160
JSON.stringify({
151161
type: 'hmr:update',
152162
url: '/root/foo.js',
@@ -159,7 +169,7 @@ describe('HmrPlugin', () => {
159169

160170
it('should bubble updates for changed dynamic import dependencies', async () => {
161171
const { server, host } = await createTestServer({
162-
rootDir: __dirname,
172+
rootDir: import.meta.dirname,
163173
plugins: [
164174
mockFile(
165175
'/foo.js',
@@ -170,13 +180,14 @@ describe('HmrPlugin', () => {
170180
],
171181
});
172182
const { fileWatcher, webSockets } = server;
173-
const stub = stubMethod(webSockets!, 'send');
183+
const stub = mock.method(webSockets!, 'send');
174184
try {
175185
await fetch(`${host}/foo.js`);
176186
await fetch(`${host}/bar.js`);
177-
fileWatcher.emit('change', pathUtil.join(__dirname, '/bar.js'));
187+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/bar.js'));
178188

179-
expect(stub.firstCall!.args[0]).to.equal(
189+
assert.equal(
190+
stub.mock.calls[0].arguments[0],
180191
JSON.stringify({
181192
type: 'hmr:update',
182193
url: '/foo.js',
@@ -189,7 +200,7 @@ describe('HmrPlugin', () => {
189200

190201
it('imports changed dependencies with a unique URL', async () => {
191202
const { server, host } = await createTestServer({
192-
rootDir: __dirname,
203+
rootDir: import.meta.dirname,
193204
plugins: [
194205
mockFiles({
195206
'/a.js': "import '/b.js'; import '/c.js'; import.meta.hot.accept();",
@@ -204,11 +215,11 @@ describe('HmrPlugin', () => {
204215
await fetchText(`${host}/a.js`);
205216
await fetchText(`${host}/b.js`);
206217
await fetchText(`${host}/c.js`);
207-
fileWatcher.emit('change', pathUtil.join(__dirname, '/b.js'));
218+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/b.js'));
208219

209220
const updatedA = await fetchText(`${host}/a.js?m=1234567890123`);
210221
await fetchText(`${host}/b.js?m=1234567890123`);
211-
expect(/import '\/b\.js\?m=\d{13}';/.test(updatedA)).to.equal(true);
222+
assert.match(updatedA, /import '\/b\.js\?m=\d{13}';/);
212223
expectIncludes(updatedA, "import '/c.js';");
213224
} finally {
214225
await server.stop();
@@ -217,7 +228,7 @@ describe('HmrPlugin', () => {
217228

218229
it('imports deeply changed dependencies with a unique URL', async () => {
219230
const { server, host } = await createTestServer({
220-
rootDir: __dirname,
231+
rootDir: import.meta.dirname,
221232
plugins: [
222233
mockFiles({
223234
'/a.js': "import '/b.js'; import.meta.hot.accept();",
@@ -232,21 +243,21 @@ describe('HmrPlugin', () => {
232243
await fetchText(`${host}/a.js`);
233244
await fetchText(`${host}/b.js`);
234245
await fetchText(`${host}/c.js`);
235-
fileWatcher.emit('change', pathUtil.join(__dirname, '/c.js'));
246+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/c.js'));
236247

237248
const updatedA = await fetchText(`${host}/a.js?m=1234567890123`);
238249
const updatedB = await fetchText(`${host}/b.js?m=1234567890123`);
239250
await fetchText(`${host}/c.js?m=1234567890123`);
240-
expect(/import '\/b\.js\?m=\d{13}';/.test(updatedA)).to.equal(true);
241-
expect(/import '\/c\.js\?m=\d{13}';/.test(updatedB)).to.equal(true);
251+
assert.match(updatedA, /import '\/b\.js\?m=\d{13}';/);
252+
assert.match(updatedB, /import '\/c\.js\?m=\d{13}';/);
242253
} finally {
243254
await server.stop();
244255
}
245256
});
246257

247258
it('multiple dependents will import deep dependency changes with a unique URL', async () => {
248259
const { server, host } = await createTestServer({
249-
rootDir: __dirname,
260+
rootDir: import.meta.dirname,
250261
plugins: [
251262
mockFiles({
252263
'/a1.js': "import '/b.js'; import.meta.hot.accept(); // a1",
@@ -264,22 +275,22 @@ describe('HmrPlugin', () => {
264275
await fetchText(`${host}/a2.js`);
265276
await fetchText(`${host}/b.js`);
266277
await fetchText(`${host}/c.js`);
267-
fileWatcher.emit('change', pathUtil.join(__dirname, '/c.js'));
278+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/c.js'));
268279

269280
const updatedA1 = await fetchText(`${host}/a1.js?m=1234567890123`);
270281
const updatedA2 = await fetchText(`${host}/a2.js?m=1234567890123`);
271282
await fetchText(`${host}/b.js?m=1234567890123`);
272283
await fetchText(`${host}/c.js?m=1234567890123`);
273-
expect(/import '\/b\.js\?m=\d{13}';/.test(updatedA1)).to.equal(true);
274-
expect(/import '\/b\.js\?m=\d{13}';/.test(updatedA2)).to.equal(true);
284+
assert.match(updatedA1, /import '\/b\.js\?m=\d{13}';/);
285+
assert.match(updatedA2, /import '\/b\.js\?m=\d{13}';/);
275286
} finally {
276287
await server.stop();
277288
}
278289
});
279290

280291
it('does not get confused by dynamic imports with non string literals', async () => {
281292
const { server, host } = await createTestServer({
282-
rootDir: __dirname,
293+
rootDir: import.meta.dirname,
283294
plugins: [
284295
mockFile(
285296
'/foo.js',
@@ -290,13 +301,14 @@ describe('HmrPlugin', () => {
290301
],
291302
});
292303
const { fileWatcher, webSockets } = server;
293-
const stub = stubMethod(webSockets!, 'send');
304+
const stub = mock.method(webSockets!, 'send');
294305
try {
295306
await fetch(`${host}/foo.js`);
296307
await fetch(`${host}/bar.js`);
297-
fileWatcher.emit('change', pathUtil.join(__dirname, '/bar.js'));
308+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/bar.js'));
298309

299-
expect(stub.firstCall!.args[0]).to.equal(
310+
assert.equal(
311+
stub.mock.calls[0].arguments[0],
300312
JSON.stringify({
301313
type: 'hmr:update',
302314
url: '/foo.js',
@@ -309,7 +321,7 @@ describe('HmrPlugin', () => {
309321

310322
it('should emit reload for tracked files', async () => {
311323
const { server, host } = await createTestServer({
312-
rootDir: __dirname,
324+
rootDir: import.meta.dirname,
313325
plugins: [
314326
mockFile(
315327
'/foo.js',
@@ -321,12 +333,13 @@ describe('HmrPlugin', () => {
321333
],
322334
});
323335
const { fileWatcher, webSockets } = server;
324-
const stub = stubMethod(webSockets!, 'send');
336+
const stub = mock.method(webSockets!, 'send');
325337
try {
326338
await fetch(`${host}/foo.js`);
327-
fileWatcher.emit('change', pathUtil.join(__dirname, '/foo.js'));
339+
fileWatcher.emit('change', pathUtil.join(import.meta.dirname, '/foo.js'));
328340

329-
expect(stub.firstCall!.args[0]).to.equal(
341+
assert.equal(
342+
stub.mock.calls[0].arguments[0],
330343
JSON.stringify({
331344
type: 'hmr:reload',
332345
}),
@@ -338,22 +351,22 @@ describe('HmrPlugin', () => {
338351

339352
it('serves a hmr client', async () => {
340353
const { server, host } = await createTestServer({
341-
rootDir: __dirname,
354+
rootDir: import.meta.dirname,
342355
plugins: [hmrPlugin()],
343356
});
344357

345358
try {
346359
const response = await fetch(`${host}${NAME_HMR_CLIENT_IMPORT}`);
347360
const body = await response.text();
348-
expect(body.includes('class HotModule')).to.equal(true);
361+
expectIncludes(body, 'class HotModule');
349362
} finally {
350363
await server.stop();
351364
}
352365
});
353366

354367
it('transforms hmr-capable js files', async () => {
355368
const { server, host } = await createTestServer({
356-
rootDir: __dirname,
369+
rootDir: import.meta.dirname,
357370
plugins: [
358371
mockFile(
359372
'/foo.js',
@@ -369,23 +382,23 @@ describe('HmrPlugin', () => {
369382
const response = await fetch(`${host}/foo.js`);
370383
const body = await response.text();
371384

372-
expect(body.includes('__WDS_HMR__')).to.equal(true);
385+
expectIncludes(body, '__WDS_HMR__');
373386
} finally {
374387
await server.stop();
375388
}
376389
});
377390

378391
it('does not transform non-hmr js files', async () => {
379392
const { server, host } = await createTestServer({
380-
rootDir: __dirname,
393+
rootDir: import.meta.dirname,
381394
plugins: [mockFile('/foo.js', `export const foo = 5;`), hmrPlugin()],
382395
});
383396

384397
try {
385398
const response = await fetch(`${host}/foo.js`);
386399
const body = await response.text();
387400

388-
expect(body.includes('__WDS_HMR__')).to.equal(false);
401+
expectNotIncludes(body, '__WDS_HMR__');
389402
} finally {
390403
await server.stop();
391404
}

0 commit comments

Comments
 (0)