Skip to content

Commit 02b7ae8

Browse files
authored
Merge pull request #3098 from modernweb-dev/migrate/dev-server-core-node-test
test(dev-server-core): migrate from mocha/chai to node:test
2 parents b949cff + fc908c9 commit 02b7ae8

17 files changed

Lines changed: 279 additions & 291 deletions

packages/dev-server-core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
"start:event-stream": "node demo/event-stream/start-server.js",
4040
"start:http2": "node demo/http2/start-server.js",
4141
"start:import-asset": "node demo/import-asset/start-server.js",
42-
"test": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --exit --reporter dot",
43-
"test:watch": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --watch --watch-files src,test"
42+
"test:node": "node --experimental-strip-types --test --test-force-exit \"test/**/*.test.ts\"",
43+
"test:watch": "node --experimental-strip-types --test --test-force-exit --watch \"test/**/*.test.ts\""
4444
},
4545
"files": [
4646
".self-signed-dev-server-ssl.cert",

packages/dev-server-core/src/test-helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import portfinder from 'portfinder';
2-
import { expect } from 'chai';
32
import { green, red, yellow } from 'nanocolors';
43

54
import { DevServer } from './server/DevServer.js';
@@ -62,8 +61,9 @@ export const timeout = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms
6261

6362
export async function fetchText(url: string, init?: RequestInit) {
6463
const response = await fetch(url, init);
65-
66-
expect(response.status).to.equal(200);
64+
if (response.status !== 200) {
65+
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
66+
}
6767
return response.text();
6868
}
6969

packages/dev-server-core/test/helpers.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import {
44
timeout,
55
fetchText,
66
expectIncludes,
7+
expectNotIncludes,
78
virtualFilesPlugin,
8-
} from '../src/test-helpers.js';
9-
import { DevServerCoreConfig } from '../src/server/DevServerCoreConfig.js';
9+
} from '../dist/test-helpers.js';
10+
import type { DevServerCoreConfig } from '../dist/server/DevServerCoreConfig.js';
1011

1112
export function createTestServer(config: Partial<DevServerCoreConfig> = {}) {
1213
return originalCreateTestServer({
13-
rootDir: path.resolve(__dirname, 'fixtures', 'basic'),
14+
rootDir: path.resolve(import.meta.dirname, 'fixtures', 'basic'),
1415
...config,
1516
});
1617
}
1718

18-
export { timeout, fetchText, expectIncludes, virtualFilesPlugin };
19+
export { timeout, fetchText, expectIncludes, expectNotIncludes, virtualFilesPlugin };

packages/dev-server-core/test/middleware/basePathMiddleware.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { expect } from 'chai';
1+
import assert from 'node:assert/strict';
2+
import { describe, it, beforeEach, afterEach } from 'node:test';
23

3-
import { DevServer } from '../../src/server/DevServer.js';
4-
import { createTestServer } from '../helpers.js';
4+
import type { DevServer } from '../../dist/server/DevServer.js';
5+
import { createTestServer, expectIncludes } from '../helpers.ts';
56

67
describe('base path middleware', () => {
78
describe('without a trailing /', () => {
@@ -19,20 +20,20 @@ describe('base path middleware', () => {
1920
const response = await fetch(`${host}/foo/index.html`);
2021
const responseText = await response.text();
2122

22-
expect(response.status).to.equal(200);
23-
expect(responseText).to.include('<title>My app</title>');
23+
assert.equal(response.status, 200);
24+
expectIncludes(responseText, '<title>My app</title>');
2425
});
2526

2627
it('can request without base path', async () => {
2728
const response = await fetch(`${host}/index.html`);
2829
const responseText = await response.text();
2930

30-
expect(response.status).to.equal(200);
31-
expect(responseText).to.include('<title>My app</title>');
31+
assert.equal(response.status, 200);
32+
expectIncludes(responseText, '<title>My app</title>');
3233
});
3334
});
3435

35-
context('with a trailing /', () => {
36+
describe('with a trailing /', () => {
3637
let host: string;
3738
let server: DevServer;
3839
beforeEach(async () => {
@@ -47,16 +48,16 @@ describe('base path middleware', () => {
4748
const response = await fetch(`${host}/foo/index.html`);
4849
const responseText = await response.text();
4950

50-
expect(response.status).to.equal(200);
51-
expect(responseText).to.include('<title>My app</title>');
51+
assert.equal(response.status, 200);
52+
expectIncludes(responseText, '<title>My app</title>');
5253
});
5354

5455
it('can request without base path', async () => {
5556
const response = await fetch(`${host}/index.html`);
5657
const responseText = await response.text();
5758

58-
expect(response.status).to.equal(200);
59-
expect(responseText).to.include('<title>My app</title>');
59+
assert.equal(response.status, 200);
60+
expectIncludes(responseText, '<title>My app</title>');
6061
});
6162
});
6263
});

packages/dev-server-core/test/middleware/etagCacheMiddleware.test.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { expect } from 'chai';
1+
import assert from 'node:assert/strict';
2+
import { describe, it, beforeEach, afterEach } from 'node:test';
23
import path from 'path';
34
import fs from 'fs';
45
import { nanoid } from 'nanoid';
56

6-
import { createTestServer, timeout } from '../helpers.js';
7-
import { DevServer } from '../../src/server/DevServer.js';
7+
import { createTestServer, timeout } from '../helpers.ts';
8+
import type { DevServer } from '../../dist/server/DevServer.js';
89

9-
const fixtureDir = path.resolve(__dirname, '..', 'fixtures', 'basic');
10+
const fixtureDir = path.resolve(import.meta.dirname, '..', 'fixtures', 'basic');
1011
const testFileAName = '/cached-file-a.js';
1112
const testFileBName = '/cached-file-b.js';
1213
const testFileAPath = path.join(fixtureDir, testFileAName);
@@ -24,7 +25,7 @@ describe('etag cache middleware', () => {
2425
server.stop();
2526
});
2627

27-
context('', () => {
28+
describe('cached file a', () => {
2829
beforeEach(() => {
2930
fs.writeFileSync(testFileAPath, '// this file is cached', 'utf-8');
3031
});
@@ -37,21 +38,21 @@ describe('etag cache middleware', () => {
3738
const initialResponse = await fetch(`${host}${testFileAName}`);
3839
const etag = initialResponse.headers.get('etag')!;
3940

40-
expect(initialResponse.status).to.equal(200);
41-
expect(await initialResponse.text()).to.equal('// this file is cached');
41+
assert.equal(initialResponse.status, 200);
42+
assert.equal(await initialResponse.text(), '// this file is cached');
4243

43-
expect(etag).to.be.a('string');
44+
assert.equal(typeof etag, 'string');
4445

4546
const cachedResponse = await fetch(`${host}${testFileAName}`, {
4647
headers: { 'If-None-Match': etag, 'Cache-Control': 'max-age=3600' },
4748
});
4849

49-
expect(cachedResponse.status).to.equal(304);
50-
expect(await cachedResponse.text()).to.equal('');
50+
assert.equal(cachedResponse.status, 304);
51+
assert.equal(await cachedResponse.text(), '');
5152
});
5253
});
5354

54-
context('', () => {
55+
describe('cached file b', () => {
5556
beforeEach(() => {
5657
fs.writeFileSync(testFileBPath, '// this file is cached', 'utf-8');
5758
});
@@ -66,9 +67,9 @@ describe('etag cache middleware', () => {
6667
const initialResponse = await fetch(`${host}${testFileBName}`);
6768
const etag = initialResponse.headers.get('etag');
6869

69-
expect(initialResponse.status).to.equal(200);
70-
expect(await initialResponse.text()).to.equal('// this file is cached');
71-
expect(etag).to.be.a('string');
70+
assert.equal(initialResponse.status, 200);
71+
assert.equal(await initialResponse.text(), '// this file is cached');
72+
assert.equal(typeof etag, 'string');
7273

7374
await timeout(10);
7475
const fileContent = `// the cache is busted${nanoid()}`;
@@ -78,8 +79,8 @@ describe('etag cache middleware', () => {
7879
const headers = { headers: { 'if-none-match': etag } as Record<string, string> };
7980
const cachedResponse = await fetch(`${host}${testFileBName}`, headers);
8081

81-
expect(cachedResponse.status).to.equal(200);
82-
expect(await cachedResponse.text()).to.equal(fileContent);
82+
assert.equal(cachedResponse.status, 200);
83+
assert.equal(await cachedResponse.text(), fileContent);
8384
});
8485
});
8586
});
Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { expect } from 'chai';
1+
import assert from 'node:assert/strict';
2+
import { describe, it, beforeEach, afterEach } from 'node:test';
23
import path from 'path';
34

4-
import { createTestServer } from '../helpers.js';
5-
import { DevServer } from '../../src/server/DevServer.js';
5+
import { createTestServer, expectIncludes, expectNotIncludes } from '../helpers.ts';
6+
import type { DevServer } from '../../dist/server/DevServer.js';
67

78
describe('history api fallback middleware', () => {
89
describe('index in root', () => {
@@ -11,7 +12,7 @@ describe('history api fallback middleware', () => {
1112

1213
beforeEach(async () => {
1314
({ host, server } = await createTestServer({
14-
appIndex: path.resolve(__dirname, '..', 'fixtures', 'basic', 'index.html'),
15+
appIndex: path.resolve(import.meta.dirname, '..', 'fixtures', 'basic', 'index.html'),
1516
}));
1617
});
1718

@@ -23,41 +24,41 @@ describe('history api fallback middleware', () => {
2324
const response = await fetch(`${host}/index.html`);
2425
const responseText = await response.text();
2526

26-
expect(response.status).to.equal(200);
27-
expect(responseText).to.include('<title>My app</title>');
27+
assert.equal(response.status, 200);
28+
expectIncludes(responseText, '<title>My app</title>');
2829
});
2930

3031
it('returns the fallback index.html for non-file requests', async () => {
3132
const response = await fetch(`${host}/foo`);
3233
const responseText = await response.text();
3334

34-
expect(response.status).to.equal(200);
35-
expect(responseText).to.include('<title>My app</title>');
35+
assert.equal(response.status, 200);
36+
expectIncludes(responseText, '<title>My app</title>');
3637
});
3738

3839
it('returns the fallback index.html for file requests with multiple segments', async () => {
3940
const response = await fetch(`${host}/foo/bar/baz`);
4041
const responseText = await response.text();
4142

42-
expect(response.status).to.equal(200);
43-
expect(responseText).to.include('<title>My app</title>');
43+
assert.equal(response.status, 200);
44+
expectIncludes(responseText, '<title>My app</title>');
4445
});
4546

4647
it('does not return index.html for file requests', async () => {
4748
const response = await fetch(`${host}/src/hello-world.txt`);
4849
const responseText = await response.text();
4950

50-
expect(response.status).to.equal(200);
51-
expect(responseText).to.include('Hello world!');
52-
expect(responseText).to.not.include('<title>My app</title>');
51+
assert.equal(response.status, 200);
52+
expectIncludes(responseText, 'Hello world!');
53+
expectNotIncludes(responseText, '<title>My app</title>');
5354
});
5455

5556
it('does return index.html for requests that have url parameters with . characters (issue 1059)', async () => {
5657
const response = await fetch(`${host}/text-files/foo/bar/?baz=open.wc`);
5758
const responseText = await response.text();
5859

59-
expect(response.status).to.equal(200);
60-
expect(responseText).to.include('<title>My app</title>');
60+
assert.equal(response.status, 200);
61+
expectIncludes(responseText, '<title>My app</title>');
6162
});
6263
});
6364

@@ -67,7 +68,7 @@ describe('history api fallback middleware', () => {
6768

6869
beforeEach(async () => {
6970
({ host, server } = await createTestServer({
70-
appIndex: path.resolve(__dirname, '..', 'fixtures', 'basic', 'src', 'index.html'),
71+
appIndex: path.resolve(import.meta.dirname, '..', 'fixtures', 'basic', 'src', 'index.html'),
7172
}));
7273
});
7374

@@ -79,40 +80,40 @@ describe('history api fallback middleware', () => {
7980
const response = await fetch(`${host}/src/index.html`);
8081
const responseText = await response.text();
8182

82-
expect(response.status).to.equal(200);
83-
expect(responseText).to.include('<title>My app 2</title>');
83+
assert.equal(response.status, 200);
84+
expectIncludes(responseText, '<title>My app 2</title>');
8485
});
8586

8687
it('returns the fallback index.html for non-file requests', async () => {
8788
const response = await fetch(`${host}/src/foo`);
8889
const responseText = await response.text();
8990

90-
expect(response.status).to.equal(200);
91-
expect(responseText).to.include('<title>My app 2</title>');
91+
assert.equal(response.status, 200);
92+
expectIncludes(responseText, '<title>My app 2</title>');
9293
});
9394

9495
it('returns the fallback index.html for file requests with multiple segments', async () => {
9596
const response = await fetch(`${host}/src/foo/bar/baz`);
9697
const responseText = await response.text();
9798

98-
expect(response.status).to.equal(200);
99-
expect(responseText).to.include('<title>My app 2</title>');
99+
assert.equal(response.status, 200);
100+
expectIncludes(responseText, '<title>My app 2</title>');
100101
});
101102

102103
it('does not return the index.html for requests outside the index root', async () => {
103104
const response = await fetch(`${host}/foo`);
104105
const responseText = await response.text();
105106

106-
expect(response.status).to.equal(404);
107-
expect(responseText).to.not.include('<title>My app 2</title>');
107+
assert.equal(response.status, 404);
108+
expectNotIncludes(responseText, '<title>My app 2</title>');
108109
});
109110

110111
it('does return index.html for requests that have url parameters with . characters (issue 1059)', async () => {
111112
const response = await fetch(`${host}/src/foo/bar/?baz=open.wc`);
112113
const responseText = await response.text();
113114

114-
expect(response.status).to.equal(200);
115-
expect(responseText).to.include('<title>My app 2</title>');
115+
assert.equal(response.status, 200);
116+
expectIncludes(responseText, '<title>My app 2</title>');
116117
});
117118
});
118119
});

packages/dev-server-core/test/middleware/pluginFileParsedMiddleware.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { expect } from 'chai';
2-
import { Context } from 'koa';
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
import type { Context } from 'koa';
34

4-
import { createTestServer } from '../helpers.js';
5+
import { createTestServer, expectIncludes } from '../helpers.ts';
56

67
describe('plugin-file-parsed middleware', () => {
78
it('is called after other plugin hooks', async () => {
@@ -44,11 +45,11 @@ describe('plugin-file-parsed middleware', () => {
4445
try {
4546
const response = await fetch(`${host}/foo.js`);
4647

47-
expect(response.status).to.equal(200);
48-
expect(response.headers.get('content-type')).to.include('application/javascript');
49-
expect(order[order.length - 1]).to.equal('fileParsed');
50-
expect(context).to.exist;
51-
expect(context!.path).to.equal('/foo.js');
48+
assert.equal(response.status, 200);
49+
expectIncludes(response.headers.get('content-type')!, 'application/javascript');
50+
assert.equal(order[order.length - 1], 'fileParsed');
51+
assert.ok(context);
52+
assert.equal(context!.path, '/foo.js');
5253
} finally {
5354
server.stop();
5455
}

packages/dev-server-core/test/middleware/pluginMimeTypeMiddleware.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { expect } from 'chai';
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
23

3-
import { createTestServer } from '../helpers.js';
4+
import { createTestServer, expectIncludes } from '../helpers.ts';
45

56
describe('plugin-mime-type middleware', () => {
67
it('can set the mime type of a file with a string', async () => {
@@ -20,8 +21,8 @@ describe('plugin-mime-type middleware', () => {
2021
try {
2122
const response = await fetch(`${host}/src/hello-world.txt`);
2223

23-
expect(response.status).to.equal(200);
24-
expect(response.headers.get('content-type')).to.include('application/javascript');
24+
assert.equal(response.status, 200);
25+
expectIncludes(response.headers.get('content-type')!, 'application/javascript');
2526
} finally {
2627
server.stop();
2728
}
@@ -44,8 +45,8 @@ describe('plugin-mime-type middleware', () => {
4445
try {
4546
const response = await fetch(`${host}/src/hello-world.txt`);
4647

47-
expect(response.status).to.equal(200);
48-
expect(response.headers.get('content-type')).to.include('application/javascript');
48+
assert.equal(response.status, 200);
49+
expectIncludes(response.headers.get('content-type')!, 'application/javascript');
4950
} finally {
5051
server.stop();
5152
}

0 commit comments

Comments
 (0)