Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/dev-server-import-maps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
},
"scripts": {
"build": "tsc",
"test": "mocha \"test/**/*.test.ts\" --require ts-node/register",
"test:browser": "node ../test-runner/dist/bin.js test-browser/test/**/*.test.{js,html} --config test-browser/web-test-runner.config.mjs",
"test:watch": "mocha \"test/**/*.test.ts\" --require ts-node/register --watch --watch-files src,test"
"test:node": "node --experimental-strip-types --test --test-force-exit test/**/*.test.ts",
"test:watch": "node --experimental-strip-types --test --test-force-exit --watch test/**/*.test.ts"
},
"files": [
"*.d.ts",
Expand Down
17 changes: 9 additions & 8 deletions packages/dev-server-import-maps/test/injection.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { it } from 'node:test';
import { createTestServer, expectNotIncludes } from '@web/dev-server-core/test-helpers';
import { fetchText, expectIncludes, virtualFilesPlugin } from '@web/dev-server-core/test-helpers';

import { importMapsPlugin } from '../src/importMapsPlugin.js';
import { importMapsPlugin } from '../dist/importMapsPlugin.js';

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

it('can use an include pattern', async () => {
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [
virtualFilesPlugin({
'/foo/a.html': '<html><body></body></html>',
Expand Down Expand Up @@ -58,7 +59,7 @@ it('can use an include pattern', async () => {

it('can use an exclude pattern', async () => {
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [
virtualFilesPlugin({
'/foo/a.html': '<html><body></body></html>',
Expand Down Expand Up @@ -86,7 +87,7 @@ it('can use an exclude pattern', async () => {

it('treats directory paths with an implicit index.html file', async () => {
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [
{
name: 'test',
Expand Down Expand Up @@ -116,7 +117,7 @@ it('treats directory paths with an implicit index.html file', async () => {

it('merges with an existing import map', async () => {
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [
virtualFilesPlugin({
'/index.html':
Expand Down Expand Up @@ -146,7 +147,7 @@ it('merges with an existing import map', async () => {

it('merges import map scopes', async () => {
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [
virtualFilesPlugin({
'/index.html':
Expand Down Expand Up @@ -176,7 +177,7 @@ it('merges import map scopes', async () => {

it('the import map in the HTML file takes priority over the injected import map', async () => {
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [
virtualFilesPlugin({
'/index.html':
Expand Down
71 changes: 32 additions & 39 deletions packages/dev-server-import-maps/test/resolving.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { describe, it, mock } from 'node:test';
import assert from 'node:assert/strict';
import { fetchText, expectIncludes, virtualFilesPlugin } from '@web/dev-server-core/test-helpers';
import { createTestServer } from '@web/dev-server-core/test-helpers';
import { expect } from 'chai';
import { spy } from 'hanbi';
import path from 'path';

import { importMapsPlugin } from '../src/importMapsPlugin.js';
import { IMPORT_MAP_PARAM } from '../src/utils.js';
import { importMapsPlugin } from '../dist/importMapsPlugin.js';
import { IMPORT_MAP_PARAM } from '../dist/utils.js';

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

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

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

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

Expand All @@ -98,7 +98,7 @@ describe('applies import map id', () => {
'/app.js': 'import "bar";',
};
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [
{
name: 'test',
Expand All @@ -124,7 +124,7 @@ describe('applies import map id', () => {
'/app.js': 'import "bar";',
};
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [
{
name: 'test',
Expand All @@ -150,7 +150,7 @@ describe('applies import map id', () => {
'/app.js': 'import "bar?foo=bar";',
};
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
});

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

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

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

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

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

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

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

const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [
{
name: 'test',
Expand Down Expand Up @@ -329,7 +329,7 @@ describe('resolving imports', () => {
'/index.html': '<html><body><script src="./app.js"></script></body></html>',
};
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
});

Expand All @@ -351,36 +351,29 @@ describe('resolving imports', () => {
</body>
</html>`,
};
const loggerSpies = {
log: spy(),
debug: spy(),
error: spy(),
warn: spy(),
group: spy(),
groupEnd: spy(),
logSyntaxError: spy(),
};
const warnFn = mock.fn();
const noopFn = mock.fn();
const logger = {
log: loggerSpies.log.handler,
debug: loggerSpies.debug.handler,
error: loggerSpies.error.handler,
warn: loggerSpies.warn.handler,
group: loggerSpies.group.handler,
groupEnd: loggerSpies.groupEnd.handler,
logSyntaxError: loggerSpies.logSyntaxError.handler,
log: noopFn,
debug: noopFn,
error: noopFn,
warn: warnFn,
group: noopFn,
groupEnd: noopFn,
logSyntaxError: noopFn,
};
const { server, host } = await createTestServer(
{
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
},
logger,
);

const text = await fetchText(`${host}/index.html`);
expectIncludes(text, '<script type="importmap">{</script>');
expect(loggerSpies.warn.callCount).to.equal(1);
const warning = loggerSpies.warn.getCall(0).args[0];
assert.equal(warnFn.mock.callCount(), 1);
const warning = warnFn.mock.calls[0].arguments[0];
expectIncludes(warning, 'Failed to parse import map in "');
expectIncludes(warning, `test${path.sep}index.html": `);
server.stop();
Expand All @@ -392,7 +385,7 @@ describe('resolving imports', () => {
'/app.js': 'import "foo";\nimport bar from "./bar.js";',
};
const { server, host } = await createTestServer({
rootDir: __dirname,
rootDir: import.meta.dirname,
plugins: [virtualFilesPlugin(files), importMapsPlugin()],
});

Expand Down
Loading