Skip to content

Commit d991cd3

Browse files
authored
Merge pull request #3096 from modernweb-dev/migrate/dev-server-rollup-node-test
test(dev-server-rollup): migrate tests from mocha/chai to node:test
2 parents 8406afb + 6144898 commit d991cd3

10 files changed

Lines changed: 78 additions & 68 deletions

File tree

packages/dev-server-rollup/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"node": ">=22.0.0"
2626
},
2727
"scripts": {
28-
"test:node": "mocha \"test/node/**/*.test.ts\" --require ts-node/register --exit --reporter dot",
29-
"test:watch": "mocha \"test/node/**/*.test.ts\" --require ts-node/register --watch --watch-files src,test"
28+
"test:node": "node --experimental-strip-types --test --test-force-exit 'test/node/**/*.test.ts'",
29+
"test:watch": "node --experimental-strip-types --test --test-force-exit --watch 'test/node/**/*.test.ts'"
3030
},
3131
"files": [
3232
"*.d.ts",
@@ -65,8 +65,6 @@
6565
"@types/whatwg-url": "^11.0.0",
6666
"@web/test-runner-chrome": "^0.18.0",
6767
"@web/test-runner-core": "^0.13.0",
68-
"chai": "^4.2.0",
69-
"mocha": "^10.8.2",
7068
"postcss": "^8.5.14",
7169
"rollup-plugin-postcss": "^4.0.2"
7270
}

packages/dev-server-rollup/test/node/plugins/alias.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { describe, it } from 'node:test';
12
import rollupAlias from '@rollup/plugin-alias';
23

3-
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.js';
4-
import { fromRollup } from '../../../src/fromRollup.js';
4+
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.ts';
5+
import { fromRollup } from '../../../dist/fromRollup.js';
56

67
const alias = fromRollup(rollupAlias);
78

packages/dev-server-rollup/test/node/plugins/babel.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/// <reference types="../../../types/rollup__plugin-babel" />
2+
import { describe, it } from 'node:test';
3+
import { createRequire } from 'node:module';
24
import rollupBabel from '@rollup/plugin-babel';
35

4-
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.js';
5-
import { fromRollup } from '../../../src/index.js';
6+
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.ts';
7+
import { fromRollup } from '../../../dist/index.js';
8+
9+
const require = createRequire(import.meta.url);
610

711
const babel = fromRollup(rollupBabel);
812

packages/dev-server-rollup/test/node/plugins/commonjs.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { describe, it } from 'node:test';
12
import rollupCommonjs from '@rollup/plugin-commonjs';
23
import { runTests } from '@web/test-runner-core/test-helpers';
34
import { resolve } from 'path';
45
import { chromeLauncher } from '@web/test-runner-chrome';
56

67
import * as path from 'path';
7-
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.js';
8-
import { fromRollup } from '../../../src/index.js';
8+
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.ts';
9+
import { fromRollup } from '../../../dist/index.js';
910
import { nodeResolvePlugin } from '@web/dev-server';
1011

1112
const commonjs = fromRollup(rollupCommonjs);
@@ -140,7 +141,7 @@ exports.default = _default;`;
140141
});
141142

142143
it('can transform modules which require node-resolved modules', async () => {
143-
const rootDir = path.resolve(__dirname, '..', 'fixtures', 'basic');
144+
const rootDir = path.resolve(import.meta.dirname, '..', 'fixtures', 'basic');
144145
const { server, host } = await createTestServer({
145146
plugins: [
146147
{
@@ -199,11 +200,11 @@ exports.default = _default;`;
199200
}
200201
});
201202

202-
it('passes the in-browser tests', async function () {
203-
this.timeout(40000);
204-
203+
it('passes the in-browser tests', { timeout: 40000 }, async () => {
205204
await runTests({
206-
files: [resolve(__dirname, '..', 'fixtures', 'commonjs', 'commonjs-browser-test.js')],
205+
files: [
206+
resolve(import.meta.dirname, '..', 'fixtures', 'commonjs', 'commonjs-browser-test.js'),
207+
],
207208
browsers: [chromeLauncher({ launchOptions: { devtools: false } })],
208209
plugins: [
209210
fromRollup(rollupCommonjs)({

packages/dev-server-rollup/test/node/plugins/node-resolve.test.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert/strict';
13
import path from 'path';
24
import rollupNodeResolve from '@rollup/plugin-node-resolve';
35
import rollupCommonjs from '@rollup/plugin-commonjs';
46

5-
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.js';
6-
import { fromRollup } from '../../../src/index.js';
7-
import { expect } from 'chai';
7+
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.ts';
8+
import { fromRollup } from '../../../dist/index.js';
89

910
const nodeResolve = fromRollup(rollupNodeResolve, {}, { throwOnUnresolvedImport: true });
1011
const commonjs = fromRollup(rollupCommonjs);
@@ -61,7 +62,7 @@ describe('@rollup/plugin-node-resolve', () => {
6162

6263
it('can resolve private imports in inline scripts', async () => {
6364
const { server, host } = await createTestServer({
64-
rootDir: path.resolve(__dirname, '..', 'fixtures', 'private-imports'),
65+
rootDir: path.resolve(import.meta.dirname, '..', 'fixtures', 'private-imports'),
6566
plugins: [nodeResolve()],
6667
});
6768

@@ -76,13 +77,13 @@ describe('@rollup/plugin-node-resolve', () => {
7677

7778
it('throws when trying to access files from the package directly if they are not exposed in the export map', async () => {
7879
const { server, host } = await createTestServer({
79-
rootDir: path.resolve(__dirname, '..', 'fixtures', 'private-imports'),
80+
rootDir: path.resolve(import.meta.dirname, '..', 'fixtures', 'private-imports'),
8081
plugins: [nodeResolve()],
8182
});
8283

8384
try {
8485
const response = await fetch(`${host}/import-private-directly.html`);
85-
expect(response.status).to.equal(500);
86+
assert.equal(response.status, 500);
8687
} finally {
8788
server.stop();
8889
}
@@ -105,7 +106,7 @@ describe('@rollup/plugin-node-resolve', () => {
105106

106107
try {
107108
const response = await fetch(`${host}/test-app.js`);
108-
expect(response.status).to.equal(500);
109+
assert.equal(response.status, 500);
109110
} finally {
110111
server.stop();
111112
}
@@ -128,15 +129,15 @@ describe('@rollup/plugin-node-resolve', () => {
128129

129130
try {
130131
const text = await fetchText(`${host}/test-app.js`);
131-
expect(text).to.equal('import "/non-existing.js"; import "./src/non-existing.js";');
132+
assert.equal(text, 'import "/non-existing.js"; import "./src/non-existing.js";');
132133
} finally {
133134
server.stop();
134135
}
135136
});
136137

137138
it('node modules resolved outside root directory with matching basename via symlink are rewritten', async () => {
138139
const { server, host } = await createTestServer({
139-
rootDir: path.resolve(__dirname, '..', 'fixtures', 'resolve-outside-dir'),
140+
rootDir: path.resolve(import.meta.dirname, '..', 'fixtures', 'resolve-outside-dir'),
140141
plugins: [nodeResolve()],
141142
});
142143

@@ -153,7 +154,7 @@ describe('@rollup/plugin-node-resolve', () => {
153154

154155
it('node modules resolved outside root directory are rewritten', async () => {
155156
const { server, host } = await createTestServer({
156-
rootDir: path.resolve(__dirname, '..', 'fixtures', 'resolve-outside-dir', 'src'),
157+
rootDir: path.resolve(import.meta.dirname, '..', 'fixtures', 'resolve-outside-dir', 'src'),
157158
plugins: [nodeResolve()],
158159
});
159160

@@ -170,7 +171,7 @@ describe('@rollup/plugin-node-resolve', () => {
170171

171172
it('node modules resolved outside root directory are rewritten with commonjs', async () => {
172173
const { server, host } = await createTestServer({
173-
rootDir: path.resolve(__dirname, '..', 'fixtures', 'resolve-outside-dir', 'src'),
174+
rootDir: path.resolve(import.meta.dirname, '..', 'fixtures', 'resolve-outside-dir', 'src'),
174175
plugins: [commonjs(), nodeResolve()],
175176
});
176177

packages/dev-server-rollup/test/node/plugins/postcss.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/// <reference types="../../../types/rollup-plugin-postcss" />
2+
import { describe, it } from 'node:test';
23
import rollupPostcss from 'rollup-plugin-postcss';
34
import { chromeLauncher } from '@web/test-runner-chrome';
45
import { runTests } from '@web/test-runner-core/test-helpers';
56
import { resolve } from 'path';
67

7-
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.js';
8-
import { fromRollup } from '../../../src/index.js';
8+
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.ts';
9+
import { fromRollup } from '../../../dist/index.js';
910

1011
const postcss = fromRollup(rollupPostcss);
1112

1213
describe('@rollup/plugin-postcss', () => {
1314
it('can run postcss on imported css files', async () => {
1415
const { server, host } = await createTestServer({
15-
rootDir: resolve(__dirname, '..', '..', '..', '..', '..'),
16+
rootDir: resolve(import.meta.dirname, '..', '..', '..', '..', '..'),
1617
mimeTypes: {
1718
'**/*.css': 'js',
1819
},
@@ -57,10 +58,9 @@ html {
5758
}
5859
});
5960

60-
it('passes the in-browser tests', async function () {
61-
this.timeout(40000);
61+
it('passes the in-browser tests', { timeout: 40000 }, async () => {
6262
await runTests({
63-
files: [resolve(__dirname, '..', 'fixtures', 'postcss', 'postcss-browser-test.js')],
63+
files: [resolve(import.meta.dirname, '..', 'fixtures', 'postcss', 'postcss-browser-test.js')],
6464
browsers: [chromeLauncher()],
6565
mimeTypes: {
6666
'**/*.css': 'js',

packages/dev-server-rollup/test/node/plugins/replace.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { describe, it } from 'node:test';
12
import rollupReplace from '@rollup/plugin-replace';
23

3-
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.js';
4-
import { fromRollup } from '../../../src/index.js';
4+
import { createTestServer, fetchText, expectIncludes } from '../test-helpers.ts';
5+
import { fromRollup } from '../../../dist/index.js';
56

67
const replace = fromRollup(rollupReplace as any);
78

packages/dev-server-rollup/test/node/rollupBundlePlugin.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { rollupBundlePlugin } from '../../src/rollupBundlePlugin.js';
1+
import { describe, it } from 'node:test';
2+
import { rollupBundlePlugin } from '../../dist/rollupBundlePlugin.js';
23
import path from 'path';
3-
import { createTestServer, fetchText, expectIncludes } from './test-helpers.js';
4+
import { createTestServer, fetchText, expectIncludes } from './test-helpers.ts';
45

56
describe('rollupBundlePlugin', () => {
67
it('can bundle a single entrypoint', async () => {
78
const { server, host } = await createTestServer({
8-
rootDir: path.join(__dirname, 'fixtures', 'bundle-basic'),
9+
rootDir: path.join(import.meta.dirname, 'fixtures', 'bundle-basic'),
910
plugins: [
1011
rollupBundlePlugin({
1112
rollupConfig: {
12-
input: path.join(__dirname, 'fixtures', 'bundle-basic', 'a.js'),
13+
input: path.join(import.meta.dirname, 'fixtures', 'bundle-basic', 'a.js'),
1314
},
1415
}),
1516
],
@@ -28,14 +29,14 @@ describe('rollupBundlePlugin', () => {
2829

2930
it('can bundle multiple entrypoint', async () => {
3031
const { server, host } = await createTestServer({
31-
rootDir: path.join(__dirname, 'fixtures', 'bundle-multi'),
32+
rootDir: path.join(import.meta.dirname, 'fixtures', 'bundle-multi'),
3233
plugins: [
3334
rollupBundlePlugin({
3435
rollupConfig: {
3536
input: [
36-
path.join(__dirname, 'fixtures', 'bundle-multi', 'a1.js'),
37-
path.join(__dirname, 'fixtures', 'bundle-multi', 'a2.js'),
38-
path.join(__dirname, 'fixtures', 'bundle-multi', 'a3.js'),
37+
path.join(import.meta.dirname, 'fixtures', 'bundle-multi', 'a1.js'),
38+
path.join(import.meta.dirname, 'fixtures', 'bundle-multi', 'a2.js'),
39+
path.join(import.meta.dirname, 'fixtures', 'bundle-multi', 'a3.js'),
3940
],
4041
output: {
4142
chunkFileNames: '[name].js',
@@ -73,11 +74,11 @@ describe('rollupBundlePlugin', () => {
7374

7475
it('can serve regular files not bundled by rollup', async () => {
7576
const { server, host } = await createTestServer({
76-
rootDir: path.join(__dirname, 'fixtures', 'bundle-basic'),
77+
rootDir: path.join(import.meta.dirname, 'fixtures', 'bundle-basic'),
7778
plugins: [
7879
rollupBundlePlugin({
7980
rollupConfig: {
80-
input: path.join(__dirname, 'fixtures', 'bundle-basic', 'a.js'),
81+
input: path.join(import.meta.dirname, 'fixtures', 'bundle-basic', 'a.js'),
8182
},
8283
}),
8384
],
@@ -94,11 +95,11 @@ describe('rollupBundlePlugin', () => {
9495

9596
it('can serve files emitted by a rollup plugin', async () => {
9697
const { server, host } = await createTestServer({
97-
rootDir: path.join(__dirname, 'fixtures', 'bundle-basic'),
98+
rootDir: path.join(import.meta.dirname, 'fixtures', 'bundle-basic'),
9899
plugins: [
99100
rollupBundlePlugin({
100101
rollupConfig: {
101-
input: path.join(__dirname, 'fixtures', 'bundle-basic', 'a.js'),
102+
input: path.join(import.meta.dirname, 'fixtures', 'bundle-basic', 'a.js'),
102103
plugins: [
103104
{
104105
name: 'file-plugin',

packages/dev-server-rollup/test/node/test-helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import {
55
fetchText,
66
expectIncludes,
77
} from '@web/dev-server-core/test-helpers';
8-
import { DevServerCoreConfig, Logger } from '@web/dev-server-core';
8+
import type { DevServerCoreConfig, Logger } from '@web/dev-server-core';
99

1010
export function createTestServer(config: Partial<DevServerCoreConfig> = {}, mockLogger?: Logger) {
1111
return originalCreateTestServer(
1212
{
13-
rootDir: path.resolve(__dirname, 'fixtures', 'basic'),
13+
rootDir: path.resolve(import.meta.dirname, 'fixtures', 'basic'),
1414
...config,
1515
},
1616
mockLogger,

0 commit comments

Comments
 (0)