-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathmoduleMockingPlugin.test.ts
More file actions
87 lines (77 loc) · 3.41 KB
/
Copy pathmoduleMockingPlugin.test.ts
File metadata and controls
87 lines (77 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import path from 'path';
import { runTests } from '@web/test-runner-core/test-helpers';
import { chromeLauncher } from '@web/test-runner-chrome';
import { nodeResolvePlugin } from '@web/dev-server';
import { moduleMockingPlugin } from '../dist/moduleMockingPlugin.js';
describe('moduleMockingPlugin', { timeout: 20000 }, () => {
it('can intercept server relative modules', async () => {
await runTests({
files: [path.join(import.meta.dirname, 'fixtures', 'server-relative', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [moduleMockingPlugin(), nodeResolvePlugin('', false, {})],
});
});
it('can intercept bare modules', async () => {
const rootDir = path.resolve(import.meta.dirname, 'fixtures', 'bare', 'fixture');
// Define the bare module as duped to force nodeResolve to use the passed rootDir instead of the cwd
const dedupe = (importee: string) => importee === 'time-library/hour';
await runTests({
files: [path.join(import.meta.dirname, 'fixtures', 'bare', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [moduleMockingPlugin(), nodeResolvePlugin(rootDir, false, { dedupe })],
});
});
it('throws when trying to intercept without the plugin', async () => {
const { sessions } = await runTests(
{
files: [path.join(import.meta.dirname, 'fixtures', 'server-relative', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [nodeResolvePlugin('', false, {})],
},
[],
{ allowFailure: true, reportErrors: false },
);
assert.equal(sessions.length, 1);
assert.equal(sessions[0].passed, false);
assert.equal(sessions[0].errors.length, 1);
assert.match(sessions[0].logs[0][0], /Error: Module interception is not active./);
assert.match(sessions[0].errors[0].message, /Could not import your test module./);
});
it('throws when trying to intercept an inexistent module', async () => {
const { sessions } = await runTests(
{
files: [path.join(import.meta.dirname, 'fixtures', 'inexistent', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [moduleMockingPlugin(), nodeResolvePlugin('', false, {})],
},
[],
{ allowFailure: true, reportErrors: false },
);
assert.equal(sessions.length, 1);
assert.equal(sessions[0].passed, false);
assert.equal(sessions[0].errors.length, 1);
assert.match(sessions[0].logs[0][0], /Error: Could not resolve "\/inexistent-module.js"./);
assert.match(sessions[0].errors[0].message, /Could not import your test module./);
});
it('throws when trying to intercept a relative module', async () => {
const { sessions } = await runTests(
{
files: [path.join(import.meta.dirname, 'fixtures', 'relative', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [moduleMockingPlugin(), nodeResolvePlugin('', false, {})],
},
[],
{ allowFailure: true, reportErrors: false },
);
assert.equal(sessions.length, 1);
assert.equal(sessions[0].passed, false);
assert.equal(sessions[0].errors.length, 1);
assert.match(
sessions[0].logs[0][0],
/Error: Parameter `moduleName` \('.\/file\.js'\) contains a relative reference./,
);
assert.match(sessions[0].errors[0].message, /Could not import your test module./);
});
});