Skip to content

Commit adc236d

Browse files
committed
Update tests
1 parent 85475de commit adc236d

File tree

4 files changed

+97
-66
lines changed

4 files changed

+97
-66
lines changed

test/_common.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ type ImplementedHooks =
3232
| 'buildStart'
3333
| 'resolveId'
3434

35-
// This config makes for empty include[] and exclude[] patterns.
36-
export const noDepsAtAllOptions: ExternalsOptions = {
37-
packagePath: path.join(__dirname, 'fixtures/no-deps.package.json'),
38-
builtins: false,
39-
deps: false,
40-
devDeps: false,
41-
optDeps: false,
42-
peerDeps: false
43-
}
44-
4535
export async function callHook(plugin: Plugin, hookName: ImplementedHooks, ...args: any[]) {
4636
const hook = plugin[hookName] as ObjectHook<(this: typeof fakePluginContext, ...args: any) => any>
4737
if (typeof hook === 'function')

test/builtins.test.ts

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,55 @@
11
import test from 'ava'
22
import { initPlugin, callHook } from './_common.js'
33

4+
test("Marks Node builtins external by default", async t => {
5+
const { plugin } = await initPlugin()
6+
for (const builtin of [ 'path', 'node:fs' ]) {
7+
t.like(await callHook(plugin, 'resolveId', builtin), {
8+
external: true
9+
})
10+
}
11+
})
12+
13+
test("Does NOT mark Node builtins external when builtins=false", async t => {
14+
const { plugin } = await initPlugin({
15+
builtins: false
16+
})
17+
for (const builtin of [ 'path', 'node:fs' ]) {
18+
t.like(await callHook(plugin, 'resolveId', builtin), {
19+
external: false
20+
})
21+
}
22+
})
23+
24+
test("Marks Node builtins external when builtins=false and implicitly included", async t => {
25+
const { plugin } = await initPlugin({
26+
builtins: false,
27+
include: [ 'path', 'node:fs' ]
28+
})
29+
for (const builtin of [ 'path', 'node:fs' ]) {
30+
t.like(await callHook(plugin, 'resolveId', builtin), {
31+
external: true
32+
})
33+
}
34+
})
35+
36+
test("Does NOT mark Node builtins external when builtins=true implicitly excluded", async t => {
37+
const { plugin } = await initPlugin({
38+
builtins: true,
39+
exclude: [ 'path', 'node:fs' ]
40+
})
41+
for (const builtin of [ 'path', 'node:fs' ]) {
42+
t.like(await callHook(plugin, 'resolveId', builtin), {
43+
external: false
44+
})
45+
}
46+
})
47+
448
test("Adds 'node:' prefix to builtins by default", async t => {
549
const { plugin } = await initPlugin()
650
for (const builtin of [ 'node:path', 'path' ]) {
751
t.like(await callHook(plugin, 'resolveId', builtin), {
8-
id: 'node:path',
9-
external: true
52+
id: 'node:path'
1053
})
1154
}
1255
})
@@ -17,8 +60,7 @@ test("Removes 'node:' prefix when using builtinsPrefix='strip'", async t => {
1760
})
1861
for (const builtin of [ 'node:path', 'path' ]) {
1962
t.like(await callHook(plugin, 'resolveId', builtin), {
20-
id: 'path',
21-
external: true
63+
id: 'path'
2264
})
2365
}
2466
})
@@ -27,12 +69,20 @@ test("Ignores 'node:' prefix when using builtinsPrefix='ignore'", async t => {
2769
const { plugin } = await initPlugin({
2870
builtinsPrefix: 'ignore'
2971
})
30-
t.like(await callHook(plugin, 'resolveId', 'node:path'), {
31-
id: 'node:path',
32-
external: true
33-
})
34-
t.like(await callHook(plugin, 'resolveId', 'path'), {
35-
id: 'path',
36-
external: true
72+
for (const builtin of [ 'node:path', 'path' ]) {
73+
t.like(await callHook(plugin, 'resolveId', builtin), {
74+
id: builtin
75+
})
76+
}
77+
})
78+
79+
test("Does NOT remove 'node:' prefix for specific builtins, even with builtinsPrefix='add'", async t => {
80+
const { plugin } = await initPlugin({
81+
builtinsPrefix: 'strip'
3782
})
83+
for (const builtin of [ 'node:test' ]) {
84+
t.like(await callHook(plugin, 'resolveId', builtin), {
85+
id: builtin
86+
})
87+
}
3888
})

test/fixtures/no-deps.package.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/specifier.test.ts

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,57 @@
11
import test from 'ava'
2-
import { initPlugin, callHook, noDepsAtAllOptions } from './_common.js'
2+
import { initPlugin, callHook } from './_common.js'
33

4-
test("Does NOT filter out relative specifiers by default", async t => {
5-
const relativeSpecifiers = [ './sibling.js', '../parent.js' ]
6-
const { plugin } = await initPlugin(noDepsAtAllOptions)
7-
for (const specifier of relativeSpecifiers) {
8-
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed id: ${specifier}`)
4+
const testSpecifiers = {
5+
virtual: [ '\\0virtual' ],
6+
absolute: [ '/root.js' ],
7+
absoluteWin32: [ '/root.js', '\\root.js', 'C:\\root.js' ],
8+
bare: [ 'bare' ],
9+
relative: [ './sibling.js', '../parent.js' ],
10+
subpath: [ 'lodash', 'lodash/flatten.js' ],
11+
}
12+
13+
test("Always ignores virtual modules", async t => {
14+
const { plugin } = await initPlugin()
15+
for (const specifier of testSpecifiers.virtual) {
16+
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed on: ${specifier}`)
917
}
1018
})
1119

12-
test("Does NOT filter out relative specifiers, even when asked to", async t => {
13-
const relativeSpecifiers = [ './sibling.js', '../parent.js' ]
14-
const { plugin } = await initPlugin({
15-
...noDepsAtAllOptions,
16-
include: relativeSpecifiers
17-
})
18-
for (const specifier of relativeSpecifiers) {
19-
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed id: ${specifier}`)
20+
test("Always ignores absolute specifiers", async t => {
21+
const { plugin } = await initPlugin()
22+
for (const specifier of testSpecifiers[process.platform === 'win32' ? 'absoluteWin32' : 'absolute']) {
23+
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed on: ${specifier}`)
2024
}
2125
})
2226

23-
test("Does NOT filter out absolute specifiers by default", async t => {
24-
const absoluteSpecifiers = [ '/root.js' ]
25-
if (process.platform === 'win32')
26-
absoluteSpecifiers.push('\\root.js', 'C:\\root.js')
27-
const { plugin } = await initPlugin(noDepsAtAllOptions)
28-
for (const specifier of absoluteSpecifiers) {
29-
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed id: ${specifier}`)
27+
test("Always ignores relative specifiers", async t => {
28+
const { plugin } = await initPlugin()
29+
for (const specifier of testSpecifiers.relative) {
30+
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed on: ${specifier}`)
3031
}
3132
})
3233

33-
test("Does NOT filter out absolute specifiers, even when asked to", async t => {
34-
const absoluteSpecifiers = [ '/root.js' ]
35-
if (process.platform === 'win32')
36-
absoluteSpecifiers.push('\\root.js', 'C:\\root.js')
37-
const { plugin } = await initPlugin({
38-
...noDepsAtAllOptions,
39-
include: absoluteSpecifiers
40-
})
41-
for (const specifier of absoluteSpecifiers) {
42-
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed id: ${specifier}`)
34+
test("Does NOT mark bare specifiers external by default", async t => {
35+
const { plugin } = await initPlugin()
36+
for (const specifier of testSpecifiers.bare) {
37+
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed on: ${specifier}`)
4338
}
4439
})
4540

46-
test("Does NOT filter out bare specifiers by default", async t => {
47-
const bareSpecifiers = [ 'dependency' ]
48-
const { plugin } = await initPlugin(noDepsAtAllOptions)
49-
for (const specifier of bareSpecifiers) {
50-
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed id: ${specifier}`)
41+
test("Marks bare specifiers external when asked to", async t => {
42+
const { plugin } = await initPlugin({
43+
include: testSpecifiers.bare
44+
})
45+
for (const specifier of testSpecifiers.bare) {
46+
t.is(await callHook(plugin, 'resolveId', specifier), false, `Failed on: ${specifier}`)
5147
}
5248
})
5349

54-
test("Filters out bare specifiers when asked to", async t => {
55-
const bareSpecifiers = [ 'bare' ]
50+
test("Marks subpath imports external (with regexes)", async t => {
5651
const { plugin } = await initPlugin({
57-
...noDepsAtAllOptions,
58-
include: bareSpecifiers
52+
include: [ /^lodash/ ]
5953
})
60-
for (const specifier of bareSpecifiers) {
61-
t.is(await callHook(plugin, 'resolveId', specifier), false, `Failed id: ${specifier}`)
54+
for (const specifier of testSpecifiers.subpath) {
55+
t.is(await callHook(plugin, 'resolveId', specifier), false, `Failed on: ${specifier}`)
6256
}
6357
})

0 commit comments

Comments
 (0)