Skip to content

Commit cb4cf5e

Browse files
committed
Fix tests
1 parent a958739 commit cb4cf5e

File tree

3 files changed

+56
-38
lines changed

3 files changed

+56
-38
lines changed

test/_common.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
11
import path from 'node:path'
22
import { fileURLToPath } from 'node:url'
3-
import type { Plugin, RollupError, PluginContextMeta, NormalizedInputOptions } from 'rollup'
3+
import {
4+
VERSION,
5+
type Plugin, type RollupError, type PluginContextMeta, type NormalizedInputOptions
6+
} from 'rollup'
47
import { nodeExternals, type ExternalsOptions } from '../source/index.ts'
58

69
const __dirname = path.dirname(fileURLToPath(import.meta.url))
710

811
class MockPluginContext {
9-
private readonly externals: Plugin
12+
private readonly plugin: Plugin
1013
readonly warnings: string[]
1114
readonly meta: PluginContextMeta
1215

13-
constructor(externals: Plugin) {
14-
this.externals = externals
16+
constructor(plugin: Plugin) {
17+
this.plugin = plugin
1518
this.warnings = []
1619
this.meta = {
17-
rollupVersion: '4.9.6',
20+
rollupVersion: VERSION,
1821
watchMode: false
1922
}
2023
}
2124

2225
async buildStart() {
23-
let { buildStart } = this.externals
24-
if (typeof buildStart === 'object')
25-
buildStart = buildStart.handler
26-
if (typeof buildStart === 'function')
27-
return await buildStart.call(this as any, {} as NormalizedInputOptions)
26+
let { buildStart: hook } = this.plugin
27+
if (typeof hook === 'object')
28+
hook = hook.handler
29+
if (typeof hook === 'function')
30+
return await hook.call(this as any, {} as NormalizedInputOptions)
2831
throw new Error('Oops')
2932
}
3033

31-
async resolveId(specifier: string, importer: string | undefined) {
32-
let { resolveId } = this.externals
33-
if (typeof resolveId === 'object')
34-
resolveId = resolveId.handler
35-
if (typeof resolveId === 'function')
36-
return await resolveId.call(this as any, specifier, importer, { attributes: {}, isEntry: typeof importer === 'string' ? false : true })
34+
async resolveId(specifier: string, importer?: string | undefined) {
35+
let { resolveId: hook } = this.plugin
36+
if (typeof hook === 'object')
37+
hook = hook.handler
38+
if (typeof hook === 'function')
39+
return await hook.call(this as any, specifier, importer, { attributes: {}, isEntry: typeof importer === 'string' ? false : true })
3740
throw new Error('Oops')
3841
}
3942

test/fixtures/04_dual/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2-
"devDependencies": {
2+
"//": "Purposely marked both as a normal and a dev dependency",
3+
"dependencies": {
34
"dual-dep": "*"
45
},
5-
"peerDependencies": {
6+
"devDependencies": {
67
"dual-dep": "*"
78
}
89
}

test/specifier.test.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ process.chdir(fixture())
1515

1616
test("Always ignores bundle entry point", async t => {
1717
const context = await initPlugin()
18-
t.is(await context.resolveId('./path/to/entry.js', undefined), null)
18+
t.is(await context.resolveId('./path/to/entry.js'), null)
1919
})
2020

2121
test("Always ignores virtual modules from other plugins", async t => {
2222
const context = await initPlugin()
23-
t.is(await context.resolveId('\\0virtual', undefined), null, `Failed without importer`)
24-
t.is(await context.resolveId('\\0virtual', 'file.js'), null, `Failed with importer`)
23+
t.is(await context.resolveId(specifiers.virtual[0], undefined), null, `Failed without importer`)
24+
t.is(await context.resolveId(specifiers.virtual[0], 'file.js'), null, `Failed with importer`)
2525
})
2626

2727
test("Always ignores absolute specifiers", async t => {
2828
const context = await initPlugin()
29-
for (const specifier of specifiers[process.platform === 'win32' ? 'absoluteWin32' : 'absolutePosix']) {
29+
for (const specifier of (process.platform === 'win32' ? specifiers.absoluteWin32 : specifiers.absolutePosix)) {
3030
t.is(await context.resolveId(specifier, undefined), null, `Failed on: ${specifier} without importer`)
3131
t.is(await context.resolveId(specifier, 'file.js'), null, `Failed on: ${specifier} with importer`)
3232
}
@@ -47,7 +47,7 @@ test("Always ignores bare specifiers that are not dependencies", async t => {
4747

4848
test("Marks dependencies external by default", async t => {
4949
const context = await initPlugin()
50-
t.false(await context.resolveId('test-dep', 'index.js'))
50+
t.is(await context.resolveId('test-dep', 'index.js'), false)
5151
})
5252

5353
test("Does NOT mark dependencies external when deps=false", async t => {
@@ -62,32 +62,32 @@ test("Does NOT mark excluded dependencies external", async t => {
6262

6363
test("Marks peerDependencies external by default", async t => {
6464
const context = await initPlugin()
65-
t.is(await context.resolveId('test-dev-dep', 'index.js'), null)
65+
t.is(await context.resolveId('test-peer-dep', 'index.js'), false)
6666
})
6767

6868
test("Does NOT mark peerDependencies external when peerDeps=false", async t => {
6969
const context = await initPlugin({ peerDeps: false })
70-
t.is(await context.resolveId('test-dev-dep', 'index.js'), null)
70+
t.is(await context.resolveId('test-peer-dep', 'index.js'), null)
7171
})
7272

7373
test("Does NOT mark excluded peerDependencies external", async t => {
7474
const context = await initPlugin({ exclude: 'test-peer-dep' })
75-
t.is(await context.resolveId('test-dev-dep', 'index.js'), null)
75+
t.is(await context.resolveId('test-peer-dep', 'index.js'), null)
7676
})
7777

7878
test("Marks optionalDependencies external by default", async t => {
7979
const context = await initPlugin()
80-
t.false(await context.resolveId('test-opt-dep', 'index.js'))
80+
t.is(await context.resolveId('test-opt-dep', 'index.js'), false)
8181
})
8282

8383
test("Does NOT mark optionalDependencies external when optDeps=false", async t => {
8484
const context = await initPlugin({ optDeps: false })
85-
t.is(await context.resolveId('test-dev-dep', 'index.js'), null)
85+
t.is(await context.resolveId('test-opt-dep', 'index.js'), null)
8686
})
8787

8888
test("Does NOT mark excluded optionalDependencies external", async t => {
8989
const context = await initPlugin({ exclude: 'test-opt-dep' })
90-
t.is(await context.resolveId('test-dev-dep', 'index.js'), null)
90+
t.is(await context.resolveId('test-opt-dep', 'index.js'), null)
9191
})
9292

9393
test("Does NOT mark devDependencies external by default", async t => {
@@ -97,30 +97,44 @@ test("Does NOT mark devDependencies external by default", async t => {
9797

9898
test("Marks devDependencies external when devDeps=true", async t => {
9999
const context = await initPlugin({ devDeps: true })
100-
t.false(await context.resolveId('test-dev-dep', 'index.js'))
100+
t.is(await context.resolveId('test-dev-dep', 'index.js'), false)
101101
})
102102

103103
test("Marks included devDependencies external", async t => {
104104
const context = await initPlugin({ include: 'test-dev-dep' })
105-
t.false(await context.resolveId('test-dev-dep', 'index.js'))
105+
t.is(await context.resolveId('test-dev-dep', 'index.js'), false)
106106
})
107107

108-
test("Marks dependencies/peerDependencies/optionalDependencies subpath imports external", async t => {
108+
test("Subpath imports do not prevent dependencies/peerDependencies/optionalDependencies from being marked external", async t => {
109109
const context = await initPlugin()
110110
t.is(await context.resolveId('test-dep/sub', 'index.js'), false)
111111
t.is(await context.resolveId('test-peer-dep/sub', 'index.js'), false)
112112
t.is(await context.resolveId('test-opt-dep/sub', 'index.js'), false)
113113
})
114114

115-
test("Marks subpath imports external (with regexes)", async t => {
115+
test("Marks both dependency and dependency/subpath as external (with regex)", async t => {
116116
const context = await initPlugin({ include: /^test-dev-dep/ })
117117
t.is(await context.resolveId('test-dev-dep', 'index.js'), false)
118118
t.is(await context.resolveId('test-dev-dep/sub', 'index.js'), false)
119119
})
120120

121-
test("External dependencies have precedence over devDependencies", async t => {
122-
const context = await initPlugin({
123-
packagePath: '04_dual/package.json'
124-
})
125-
t.false(await context.resolveId('dual-dep', 'index.js'))
121+
test("exclude has precedence over include (builtins)", async t => {
122+
const context = await initPlugin({ include: 'node:fs', exclude: 'node:fs' })
123+
t.like(await context.resolveId('node:fs', 'index.js'), { external: false })
124+
})
125+
126+
test("exclude has precedence over include (dependencies)", async t => {
127+
const context = await initPlugin({ include: 'test-dep', exclude: 'test-dep' })
128+
t.is(await context.resolveId('test-dep', 'index.js'), null)
129+
})
130+
131+
test("exclude has precedence over include (with regexes)", async t => {
132+
const context = await initPlugin({ devDeps: true, exclude: /^test-dev-dep\/sub/ })
133+
t.is(await context.resolveId('test-dev-dep', 'index.js'), false)
134+
t.is(await context.resolveId('test-dev-dep/sub', 'index.js'), null)
135+
})
136+
137+
test("Normal dependencies have precedence over devDependencies", async t => {
138+
const context = await initPlugin({ packagePath: '04_dual/package.json' })
139+
t.is(await context.resolveId('dual-dep', 'index.js'), false)
126140
})

0 commit comments

Comments
 (0)