Skip to content

Commit fcd7840

Browse files
authored
Merge pull request #3084 from modernweb-dev/migrate/dev-server-import-maps-node-test
refactor(dev-server-import-maps): migrate tests to node:test
2 parents 6c4bdff + 113fd37 commit fcd7840

3 files changed

Lines changed: 41 additions & 49 deletions

File tree

packages/dev-server-import-maps/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
},
2727
"scripts": {
2828
"build": "tsc",
29-
"test": "mocha \"test/**/*.test.ts\" --require ts-node/register",
3029
"test:browser": "node ../test-runner/dist/bin.js test-browser/test/**/*.test.{js,html} --config test-browser/web-test-runner.config.mjs",
31-
"test:watch": "mocha \"test/**/*.test.ts\" --require ts-node/register --watch --watch-files src,test"
30+
"test:node": "node --experimental-strip-types --test --test-force-exit test/**/*.test.ts",
31+
"test:watch": "node --experimental-strip-types --test --test-force-exit --watch test/**/*.test.ts"
3232
},
3333
"files": [
3434
"*.d.ts",

packages/dev-server-import-maps/test/injection.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { it } from 'node:test';
12
import { createTestServer, expectNotIncludes } from '@web/dev-server-core/test-helpers';
23
import { fetchText, expectIncludes, virtualFilesPlugin } from '@web/dev-server-core/test-helpers';
34

4-
import { importMapsPlugin } from '../src/importMapsPlugin.js';
5+
import { importMapsPlugin } from '../dist/importMapsPlugin.js';
56

67
it('can inject an import map into any page', async () => {
78
const { server, host } = await createTestServer({
8-
rootDir: __dirname,
9+
rootDir: import.meta.dirname,
910
plugins: [
1011
virtualFilesPlugin({
1112
'/index.html': '<html><body></body></html>',
@@ -28,7 +29,7 @@ it('can inject an import map into any page', async () => {
2829

2930
it('can use an include pattern', async () => {
3031
const { server, host } = await createTestServer({
31-
rootDir: __dirname,
32+
rootDir: import.meta.dirname,
3233
plugins: [
3334
virtualFilesPlugin({
3435
'/foo/a.html': '<html><body></body></html>',
@@ -58,7 +59,7 @@ it('can use an include pattern', async () => {
5859

5960
it('can use an exclude pattern', async () => {
6061
const { server, host } = await createTestServer({
61-
rootDir: __dirname,
62+
rootDir: import.meta.dirname,
6263
plugins: [
6364
virtualFilesPlugin({
6465
'/foo/a.html': '<html><body></body></html>',
@@ -86,7 +87,7 @@ it('can use an exclude pattern', async () => {
8687

8788
it('treats directory paths with an implicit index.html file', async () => {
8889
const { server, host } = await createTestServer({
89-
rootDir: __dirname,
90+
rootDir: import.meta.dirname,
9091
plugins: [
9192
{
9293
name: 'test',
@@ -116,7 +117,7 @@ it('treats directory paths with an implicit index.html file', async () => {
116117

117118
it('merges with an existing import map', async () => {
118119
const { server, host } = await createTestServer({
119-
rootDir: __dirname,
120+
rootDir: import.meta.dirname,
120121
plugins: [
121122
virtualFilesPlugin({
122123
'/index.html':
@@ -146,7 +147,7 @@ it('merges with an existing import map', async () => {
146147

147148
it('merges import map scopes', async () => {
148149
const { server, host } = await createTestServer({
149-
rootDir: __dirname,
150+
rootDir: import.meta.dirname,
150151
plugins: [
151152
virtualFilesPlugin({
152153
'/index.html':
@@ -176,7 +177,7 @@ it('merges import map scopes', async () => {
176177

177178
it('the import map in the HTML file takes priority over the injected import map', async () => {
178179
const { server, host } = await createTestServer({
179-
rootDir: __dirname,
180+
rootDir: import.meta.dirname,
180181
plugins: [
181182
virtualFilesPlugin({
182183
'/index.html':

packages/dev-server-import-maps/test/resolving.test.ts

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { describe, it, mock } from 'node:test';
2+
import assert from 'node:assert/strict';
13
import { fetchText, expectIncludes, virtualFilesPlugin } from '@web/dev-server-core/test-helpers';
24
import { createTestServer } from '@web/dev-server-core/test-helpers';
3-
import { expect } from 'chai';
4-
import { spy } from 'hanbi';
55
import path from 'path';
66

7-
import { importMapsPlugin } from '../src/importMapsPlugin.js';
8-
import { IMPORT_MAP_PARAM } from '../src/utils.js';
7+
import { importMapsPlugin } from '../dist/importMapsPlugin.js';
8+
import { IMPORT_MAP_PARAM } from '../dist/utils.js';
99

1010
function createHtml(importMap: Record<string, unknown>) {
1111
return `
@@ -33,7 +33,7 @@ describe('applies import map id', () => {
3333
'/index.html': createHtml({ foo: './mocked-foo.js' }),
3434
};
3535
const { server, host } = await createTestServer({
36-
rootDir: __dirname,
36+
rootDir: import.meta.dirname,
3737
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
3838
});
3939

@@ -48,7 +48,7 @@ describe('applies import map id', () => {
4848
'/index.html': createHtml({ foo: './mocked-foo.js' }),
4949
};
5050
const { server, host } = await createTestServer({
51-
rootDir: __dirname,
51+
rootDir: import.meta.dirname,
5252
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
5353
});
5454

@@ -63,7 +63,7 @@ describe('applies import map id', () => {
6363
'/index.html': createHtml({ foo: './mocked-foo.js' }),
6464
};
6565
const { server, host } = await createTestServer({
66-
rootDir: __dirname,
66+
rootDir: import.meta.dirname,
6767
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
6868
});
6969

@@ -80,7 +80,7 @@ describe('applies import map id', () => {
8080
'/app.js': 'import "foo"; import foo from "./bar.js";',
8181
};
8282
const { server, host } = await createTestServer({
83-
rootDir: __dirname,
83+
rootDir: import.meta.dirname,
8484
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
8585
});
8686

@@ -98,7 +98,7 @@ describe('applies import map id', () => {
9898
'/app.js': 'import "bar";',
9999
};
100100
const { server, host } = await createTestServer({
101-
rootDir: __dirname,
101+
rootDir: import.meta.dirname,
102102
plugins: [
103103
{
104104
name: 'test',
@@ -124,7 +124,7 @@ describe('applies import map id', () => {
124124
'/app.js': 'import "bar";',
125125
};
126126
const { server, host } = await createTestServer({
127-
rootDir: __dirname,
127+
rootDir: import.meta.dirname,
128128
plugins: [
129129
{
130130
name: 'test',
@@ -150,7 +150,7 @@ describe('applies import map id', () => {
150150
'/app.js': 'import "bar?foo=bar";',
151151
};
152152
const { server, host } = await createTestServer({
153-
rootDir: __dirname,
153+
rootDir: import.meta.dirname,
154154
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
155155
});
156156

@@ -169,7 +169,7 @@ describe('resolving imports', () => {
169169
'/app.js': 'import "foo";\nimport bar from "./bar.js";',
170170
};
171171
const { server, host } = await createTestServer({
172-
rootDir: __dirname,
172+
rootDir: import.meta.dirname,
173173
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
174174
});
175175

@@ -187,7 +187,7 @@ describe('resolving imports', () => {
187187
'/x/y/app.js': 'import bar from "../../bar.js";\nimport bar from "../bar.js";',
188188
};
189189
const { server, host } = await createTestServer({
190-
rootDir: __dirname,
190+
rootDir: import.meta.dirname,
191191
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
192192
});
193193

@@ -205,7 +205,7 @@ describe('resolving imports', () => {
205205
'/x/y/app.js': 'import "x";',
206206
};
207207
const { server, host } = await createTestServer({
208-
rootDir: __dirname,
208+
rootDir: import.meta.dirname,
209209
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
210210
});
211211

@@ -222,7 +222,7 @@ describe('resolving imports', () => {
222222
'/x/app.js': 'import "./y/bar.js"; \n import "./bar.js"; \n import "../bar.js";',
223223
};
224224
const { server, host } = await createTestServer({
225-
rootDir: __dirname,
225+
rootDir: import.meta.dirname,
226226
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
227227
});
228228

@@ -249,7 +249,7 @@ describe('resolving imports', () => {
249249
'/x/app.js': 'import "bar";',
250250
};
251251
const { server, host } = await createTestServer({
252-
rootDir: __dirname,
252+
rootDir: import.meta.dirname,
253253
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
254254
});
255255

@@ -274,7 +274,7 @@ describe('resolving imports', () => {
274274
'/x/app.js': 'import "bar";',
275275
};
276276
const { server, host } = await createTestServer({
277-
rootDir: __dirname,
277+
rootDir: import.meta.dirname,
278278
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
279279
});
280280

@@ -289,7 +289,7 @@ describe('resolving imports', () => {
289289
let i = 0;
290290

291291
const { server, host } = await createTestServer({
292-
rootDir: __dirname,
292+
rootDir: import.meta.dirname,
293293
plugins: [
294294
{
295295
name: 'test',
@@ -329,7 +329,7 @@ describe('resolving imports', () => {
329329
'/index.html': '<html><body><script src="./app.js"></script></body></html>',
330330
};
331331
const { server, host } = await createTestServer({
332-
rootDir: __dirname,
332+
rootDir: import.meta.dirname,
333333
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
334334
});
335335

@@ -351,36 +351,27 @@ describe('resolving imports', () => {
351351
</body>
352352
</html>`,
353353
};
354-
const loggerSpies = {
355-
log: spy(),
356-
debug: spy(),
357-
error: spy(),
358-
warn: spy(),
359-
group: spy(),
360-
groupEnd: spy(),
361-
logSyntaxError: spy(),
362-
};
363354
const logger = {
364-
log: loggerSpies.log.handler,
365-
debug: loggerSpies.debug.handler,
366-
error: loggerSpies.error.handler,
367-
warn: loggerSpies.warn.handler,
368-
group: loggerSpies.group.handler,
369-
groupEnd: loggerSpies.groupEnd.handler,
370-
logSyntaxError: loggerSpies.logSyntaxError.handler,
355+
log: mock.fn(),
356+
debug: mock.fn(),
357+
error: mock.fn(),
358+
warn: mock.fn(),
359+
group: mock.fn(),
360+
groupEnd: mock.fn(),
361+
logSyntaxError: mock.fn(),
371362
};
372363
const { server, host } = await createTestServer(
373364
{
374-
rootDir: __dirname,
365+
rootDir: import.meta.dirname,
375366
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
376367
},
377368
logger,
378369
);
379370

380371
const text = await fetchText(`${host}/index.html`);
381372
expectIncludes(text, '<script type="importmap">{</script>');
382-
expect(loggerSpies.warn.callCount).to.equal(1);
383-
const warning = loggerSpies.warn.getCall(0).args[0];
373+
assert.equal(logger.warn.mock.callCount(), 1);
374+
const warning = logger.warn.mock.calls[0].arguments[0];
384375
expectIncludes(warning, 'Failed to parse import map in "');
385376
expectIncludes(warning, `test${path.sep}index.html": `);
386377
server.stop();
@@ -392,7 +383,7 @@ describe('resolving imports', () => {
392383
'/app.js': 'import "foo";\nimport bar from "./bar.js";',
393384
};
394385
const { server, host } = await createTestServer({
395-
rootDir: __dirname,
386+
rootDir: import.meta.dirname,
396387
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
397388
});
398389

0 commit comments

Comments
 (0)