Skip to content

Commit 16bce09

Browse files
authored
Merge pull request #3089 from modernweb-dev/migrate/test-runner-module-mocking-node-test
refactor(test-runner-module-mocking): migrate tests to node:test
2 parents 20dcd7f + 633fab0 commit 16bce09

2 files changed

Lines changed: 28 additions & 31 deletions

File tree

packages/test-runner-module-mocking/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
},
2525
"scripts": {
2626
"build": "tsc",
27-
"test:node": "mocha test/**/*.test.ts --loader=ts-node/esm --reporter dot",
28-
"test:watch": "mocha test/**/*.test.ts --loader ts-node/esm --watch --watch-files src,test"
27+
"test:node": "node --experimental-strip-types --test --test-force-exit test/**/*.test.ts",
28+
"test:watch": "node --experimental-strip-types --test --test-force-exit --watch test/**/*.test.ts"
2929
},
3030
"files": [
3131
"*.d.ts",
Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert/strict';
13
import path from 'path';
2-
import { fileURLToPath } from 'url';
34
import { runTests } from '@web/test-runner-core/test-helpers';
45
import { chromeLauncher } from '@web/test-runner-chrome';
56
import { nodeResolvePlugin } from '@web/dev-server';
67

7-
import { moduleMockingPlugin } from '../src/moduleMockingPlugin.js';
8-
import { expect } from 'chai';
9-
10-
const dirname = fileURLToPath(new URL('.', import.meta.url));
11-
12-
describe('moduleMockingPlugin', function test() {
13-
this.timeout(20000);
8+
import { moduleMockingPlugin } from '../dist/moduleMockingPlugin.js';
149

10+
describe('moduleMockingPlugin', { timeout: 20000 }, () => {
1511
it('can intercept server relative modules', async () => {
1612
await runTests({
17-
files: [path.join(dirname, 'fixtures', 'server-relative', 'browser-test.js')],
13+
files: [path.join(import.meta.dirname, 'fixtures', 'server-relative', 'browser-test.js')],
1814
browsers: [chromeLauncher()],
1915
plugins: [moduleMockingPlugin(), nodeResolvePlugin('', false, {})],
2016
});
2117
});
2218

2319
it('can intercept bare modules', async () => {
24-
const rootDir = path.resolve(dirname, 'fixtures', 'bare', 'fixture');
20+
const rootDir = path.resolve(import.meta.dirname, 'fixtures', 'bare', 'fixture');
2521
// Define the bare module as duped to force nodeResolve to use the passed rootDir instead of the cwd
2622
const dedupe = (importee: string) => importee === 'time-library/hour';
2723

2824
await runTests({
29-
files: [path.join(dirname, 'fixtures', 'bare', 'browser-test.js')],
25+
files: [path.join(import.meta.dirname, 'fixtures', 'bare', 'browser-test.js')],
3026
browsers: [chromeLauncher()],
3127
plugins: [moduleMockingPlugin(), nodeResolvePlugin(rootDir, false, { dedupe })],
3228
});
@@ -35,56 +31,57 @@ describe('moduleMockingPlugin', function test() {
3531
it('throws when trying to intercept without the plugin', async () => {
3632
const { sessions } = await runTests(
3733
{
38-
files: [path.join(dirname, 'fixtures', 'server-relative', 'browser-test.js')],
34+
files: [path.join(import.meta.dirname, 'fixtures', 'server-relative', 'browser-test.js')],
3935
browsers: [chromeLauncher()],
4036
plugins: [nodeResolvePlugin('', false, {})],
4137
},
4238
[],
4339
{ allowFailure: true, reportErrors: false },
4440
);
4541

46-
expect(sessions.length).to.equal(1);
47-
expect(sessions[0].passed).to.equal(false);
48-
expect(sessions[0].errors.length).to.equal(1);
49-
expect(sessions[0].logs[0][0]).to.match(/Error: Module interception is not active./);
50-
expect(sessions[0].errors[0].message).to.match(/Could not import your test module./);
42+
assert.equal(sessions.length, 1);
43+
assert.equal(sessions[0].passed, false);
44+
assert.equal(sessions[0].errors.length, 1);
45+
assert.match(sessions[0].logs[0][0], /Error: Module interception is not active./);
46+
assert.match(sessions[0].errors[0].message, /Could not import your test module./);
5147
});
5248

5349
it('throws when trying to intercept an inexistent module', async () => {
5450
const { sessions } = await runTests(
5551
{
56-
files: [path.join(dirname, 'fixtures', 'inexistent', 'browser-test.js')],
52+
files: [path.join(import.meta.dirname, 'fixtures', 'inexistent', 'browser-test.js')],
5753
browsers: [chromeLauncher()],
5854
plugins: [moduleMockingPlugin(), nodeResolvePlugin('', false, {})],
5955
},
6056
[],
6157
{ allowFailure: true, reportErrors: false },
6258
);
6359

64-
expect(sessions.length).to.equal(1);
65-
expect(sessions[0].passed).to.equal(false);
66-
expect(sessions[0].errors.length).to.equal(1);
67-
expect(sessions[0].logs[0][0]).to.match(/Error: Could not resolve "\/inexistent-module.js"./);
68-
expect(sessions[0].errors[0].message).to.match(/Could not import your test module./);
60+
assert.equal(sessions.length, 1);
61+
assert.equal(sessions[0].passed, false);
62+
assert.equal(sessions[0].errors.length, 1);
63+
assert.match(sessions[0].logs[0][0], /Error: Could not resolve "\/inexistent-module.js"./);
64+
assert.match(sessions[0].errors[0].message, /Could not import your test module./);
6965
});
7066

7167
it('throws when trying to intercept a relative module', async () => {
7268
const { sessions } = await runTests(
7369
{
74-
files: [path.join(dirname, 'fixtures', 'relative', 'browser-test.js')],
70+
files: [path.join(import.meta.dirname, 'fixtures', 'relative', 'browser-test.js')],
7571
browsers: [chromeLauncher()],
7672
plugins: [moduleMockingPlugin(), nodeResolvePlugin('', false, {})],
7773
},
7874
[],
7975
{ allowFailure: true, reportErrors: false },
8076
);
8177

82-
expect(sessions.length).to.equal(1);
83-
expect(sessions[0].passed).to.equal(false);
84-
expect(sessions[0].errors.length).to.equal(1);
85-
expect(sessions[0].logs[0][0]).to.match(
78+
assert.equal(sessions.length, 1);
79+
assert.equal(sessions[0].passed, false);
80+
assert.equal(sessions[0].errors.length, 1);
81+
assert.match(
82+
sessions[0].logs[0][0],
8683
/Error: Parameter `moduleName` \('.\/file\.js'\) contains a relative reference./,
8784
);
88-
expect(sessions[0].errors[0].message).to.match(/Could not import your test module./);
85+
assert.match(sessions[0].errors[0].message, /Could not import your test module./);
8986
});
9087
});

0 commit comments

Comments
 (0)