Skip to content

Commit 99b5167

Browse files
committed
Update tests
1 parent 6c27603 commit 99b5167

File tree

6 files changed

+159
-104
lines changed

6 files changed

+159
-104
lines changed

test/builtins.test.ts

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,84 +4,75 @@ import { initPlugin, callHook } from './_common.ts'
44
test("Marks Node builtins external by default", async t => {
55
const { plugin } = await initPlugin()
66
for (const builtin of [ 'path', 'node:fs' ]) {
7-
t.like(await callHook(plugin, 'resolveId', builtin), {
7+
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
88
external: true
99
})
1010
}
1111
})
1212

1313
test("Does NOT mark Node builtins external when builtins=false", async t => {
14-
const { plugin } = await initPlugin({
15-
builtins: false
16-
})
14+
const { plugin } = await initPlugin({ builtins: false })
1715
for (const builtin of [ 'path', 'node:fs' ]) {
18-
t.like(await callHook(plugin, 'resolveId', builtin), {
16+
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
1917
external: false
2018
})
2119
}
2220
})
2321

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-
})
22+
test("Does NOT mark Node builtins external when implicitely excluded", async t => {
23+
const { plugin } = await initPlugin({ exclude: [ 'path', 'node:fs' ]})
2924
for (const builtin of [ 'path', 'node:fs' ]) {
30-
t.like(await callHook(plugin, 'resolveId', builtin), {
31-
external: true
25+
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
26+
external: false
3227
})
3328
}
3429
})
3530

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-
})
31+
test("Marks Node builtins external when builtins=false and implicitly included", async t => {
32+
const { plugin } = await initPlugin({ builtins: false, include: [ 'path', 'node:fs' ] })
4133
for (const builtin of [ 'path', 'node:fs' ]) {
42-
t.like(await callHook(plugin, 'resolveId', builtin), {
43-
external: false
34+
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
35+
external: true
4436
})
4537
}
4638
})
4739

4840
test("Adds 'node:' prefix to builtins by default", async t => {
4941
const { plugin } = await initPlugin()
5042
for (const builtin of [ 'node:path', 'path' ]) {
51-
t.like(await callHook(plugin, 'resolveId', builtin), {
43+
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
5244
id: 'node:path'
5345
})
5446
}
5547
})
5648

5749
test("Removes 'node:' prefix when using builtinsPrefix='strip'", async t => {
58-
const { plugin } = await initPlugin({
59-
builtinsPrefix: 'strip'
60-
})
50+
const { plugin } = await initPlugin({ builtinsPrefix: 'strip' })
6151
for (const builtin of [ 'node:path', 'path' ]) {
62-
t.like(await callHook(plugin, 'resolveId', builtin), {
52+
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
6353
id: 'path'
6454
})
6555
}
6656
})
6757

68-
test("Ignores 'node:' prefix when using builtinsPrefix='ignore'", async t => {
69-
const { plugin } = await initPlugin({
70-
builtinsPrefix: 'ignore'
71-
})
72-
for (const builtin of [ 'node:path', 'path' ]) {
73-
t.like(await callHook(plugin, 'resolveId', builtin), {
58+
test("Does NOT remove 'node:test' prefix even with builtinsPrefix='add'", async t => {
59+
const { plugin } = await initPlugin({ builtinsPrefix: 'strip' })
60+
for (const builtin of [ 'node:test' ]) {
61+
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
7462
id: builtin
7563
})
7664
}
7765
})
7866

79-
test("Does NOT remove 'node:' prefix for specific builtins, even with builtinsPrefix='add'", async t => {
80-
const { plugin } = await initPlugin({
81-
builtinsPrefix: 'strip'
82-
})
83-
for (const builtin of [ 'node:test' ]) {
84-
t.like(await callHook(plugin, 'resolveId', builtin), {
67+
test("Does not recognize 'test' as a Node builtin", async t => {
68+
const { plugin } = await initPlugin()
69+
t.is(await callHook(plugin, 'resolveId', 'node', 'index.js'), null)
70+
})
71+
72+
test("Ignores 'node:' prefix when using builtinsPrefix='ignore'", async t => {
73+
const { plugin } = await initPlugin({ builtinsPrefix: 'ignore' })
74+
for (const builtin of [ 'node:path', 'path' ]) {
75+
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
8576
id: builtin
8677
})
8778
}

test/fixtures/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
},
55
"devDependencies": {
66
"test-dev-dep": "*"
7+
},
8+
"peerDependencies": {
9+
"test-peer-dep": "*"
10+
},
11+
"optionalDependencies": {
12+
"test-opt-dep": "*"
713
}
814
}

test/monorepo.test.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,47 @@ test.serial('git monorepo usage', async t => {
77
await fs.mkdir(fixture('01_monorepo/.git'), { recursive: true })
88
process.chdir(fixture('01_monorepo/one'))
99

10+
// Should gather dependencies up to ./test/fixtures/01_monorepo
1011
const { plugin } = await initPlugin()
1112

1213
// Should be external
1314
for (const dependency of [
14-
'moment', // 01_monorepo/one/package.json
15-
'chalk' // 01_monorepo/package.json
15+
'moment', // dependency in ./test/fixtures/01_monorepo/one/package.json (picked)
16+
'chalk' // dependency in ./test/fixtures/01_monorepo/package.json (picked)
1617
]) {
17-
t.false(await callHook(plugin, 'resolveId', dependency))
18+
t.false(await callHook(plugin, 'resolveId', dependency, 'index.js'))
1819
}
1920

2021
// Should be ignored
2122
for (const dependency of [
22-
'react', // 01_monorepo/two/package.json
23-
'test-dep' // ./package.json
23+
'react', // dependency in ./test/fixtures/01_monorepo/two/package.json (not picked)
24+
'test-dep' // dependency in ./test/fixtures/package.json (not picked)
2425
]) {
25-
t.is(await callHook(plugin, 'resolveId', dependency), null)
26+
t.is(await callHook(plugin, 'resolveId', dependency, 'index.js'), null)
2627
}
2728
})
2829

29-
test.serial('no-git monorepo usage', async t => {
30+
test.serial('non-git monorepo usage', async t => {
3031
await fs.rmdir(fixture('01_monorepo/.git'))
3132
process.chdir(fixture('01_monorepo/one'))
3233

34+
// Should gather dependencies up to . !
3335
const { plugin } = await initPlugin()
3436

3537
// Should be external
3638
for (const dependency of [
37-
'moment', // 01_monorepo/one/package.json
38-
'chalk', // 01_monorepo/package.json
39-
'test-dep' // ./package.json
39+
'moment', // dependency in ./test/fixtures/01_monorepo/one/package.json (picked)
40+
'chalk', // dependency in ./test/fixtures/01_monorepo/package.json (picked)
41+
'test-dep', // dependency in ./test/fixtures/package.json (picked)
42+
'rollup', // peer dependency in ./package.json (picked !)
4043
]) {
41-
t.false(await callHook(plugin, 'resolveId', dependency))
44+
t.false(await callHook(plugin, 'resolveId', dependency, 'index.js'))
4245
}
4346

4447
// Should be ignored
45-
t.is(await callHook(plugin, 'resolveId', 'react'), null)
48+
for (const dependency of [
49+
'react' // dependency in ./test/fixtures/01_monorepo/two/package.json (not picked)
50+
]) {
51+
t.is(await callHook(plugin, 'resolveId', dependency, 'index.js'), null)
52+
}
4653
})

test/options.test.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,11 @@ test.serial("Warns when given invalid include or exclude entry", async t => {
4848
t.is(warnings[0], `Ignoring wrong entry type #1 in 'include' option: ${JSON.stringify(notOkay)}`)
4949
})
5050

51-
test('Marks dependencies as external by default', async t => {
52-
const { plugin } = await initPlugin()
53-
t.false(await callHook(plugin, 'resolveId', 'test-dep'))
54-
})
55-
56-
test('Does NOT mark devDependencies as external by default', async t => {
57-
const { plugin } = await initPlugin()
58-
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep'), null)
59-
})
60-
61-
test('Does mark devDependencies as external when using devDeps=true', async t => {
62-
const { plugin } = await initPlugin({
63-
devDeps: true
64-
})
65-
t.false(await callHook(plugin, 'resolveId', 'test-dev-dep'))
66-
})
67-
6851
test("Obeys 'packagePath' option (single file name)", async t => {
6952
const { plugin } = await initPlugin({
7053
packagePath: '00_simple/package.json'
7154
})
72-
t.false(await callHook(plugin, 'resolveId', 'simple-dep'))
55+
t.false(await callHook(plugin, 'resolveId', 'simple-dep', 'index.js'))
7356
})
7457

7558
test("Obeys 'packagePath' option (multiple file names)", async t => {
@@ -85,6 +68,6 @@ test("Obeys 'packagePath' option (multiple file names)", async t => {
8568
'simple-dep', // 00_simple/package.json
8669
'chalk', // 01_monorepo/package.json
8770
]) {
88-
t.false(await callHook(plugin, 'resolveId', dependency))
71+
t.false(await callHook(plugin, 'resolveId', dependency, 'index.js'))
8972
}
9073
})

test/specifier.test.ts

Lines changed: 99 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,125 @@
11
import test from 'ava'
2-
import { initPlugin, callHook } from './_common.ts'
2+
import { initPlugin, callHook, fixture } from './_common.ts'
33

4-
const testSpecifiers = {
4+
const specifiers = {
55
virtual: [ '\\0virtual' ],
6-
absolute: [ '/root.js' ],
6+
absolutePosix: [ '/root.js' ],
77
absoluteWin32: [ '/root.js', '\\root.js', 'C:\\root.js' ],
8-
bare: [ 'bare' ],
8+
bare: [ 'foo', 'bar' ],
99
relative: [ './sibling.js', '../parent.js' ],
10-
subpath: [ 'lodash', 'lodash/flatten.js' ],
10+
subpath: [ 'lodash', 'lodash/flatten' ],
1111
}
1212

13-
test("Always ignores virtual modules", async t => {
13+
test("Always ignores bundle entry point", async t => {
1414
const { plugin } = await initPlugin()
15-
for (const specifier of testSpecifiers.virtual) {
16-
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed on: ${specifier}`)
17-
}
15+
t.is(await callHook(plugin, 'resolveId', './path/to/entry.js', undefined), null)
16+
})
17+
18+
test("Always ignores virtual modules from other plugins", async t => {
19+
const { plugin } = await initPlugin()
20+
t.is(await callHook(plugin, 'resolveId', '\\0virtual', undefined), null, `Failed without importer`)
21+
t.is(await callHook(plugin, 'resolveId', '\\0virtual', 'file.js'), null, `Failed with importer`)
1822
})
1923

2024
test("Always ignores absolute specifiers", async t => {
2125
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}`)
26+
for (const specifier of specifiers[process.platform === 'win32' ? 'absoluteWin32' : 'absolutePosix']) {
27+
t.is(await callHook(plugin, 'resolveId', specifier, undefined), null, `Failed on: ${specifier} without importer`)
28+
t.is(await callHook(plugin, 'resolveId', specifier, 'file.js'), null, `Failed on: ${specifier} with importer`)
2429
}
2530
})
2631

2732
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}`)
33+
const { plugin } = await initPlugin({ include: specifiers.relative })
34+
for (const specifier of specifiers.relative) {
35+
t.is(await callHook(plugin, 'resolveId', specifier, undefined), null, `Failed on: ${specifier} without importer`)
36+
t.is(await callHook(plugin, 'resolveId', specifier, 'file.js'), null, `Failed on: ${specifier} with importer`)
3137
}
3238
})
3339

34-
test("Does NOT mark bare specifiers external by default", async t => {
40+
test("Marks dependencies external by default", async t => {
41+
process.chdir(fixture())
3542
const { plugin } = await initPlugin()
36-
for (const specifier of testSpecifiers.bare) {
37-
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed on: ${specifier}`)
38-
}
43+
t.false(await callHook(plugin, 'resolveId', 'test-dep', 'index.js'))
3944
})
4045

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}`)
47-
}
46+
test("Does NOT mark dependencies external when deps=false", async t => {
47+
process.chdir(fixture())
48+
const { plugin } = await initPlugin({ deps: false })
49+
t.is(await callHook(plugin, 'resolveId', 'test-dep', 'index.js'), null)
50+
})
51+
52+
test("Does NOT mark excluded dependencies external", async t => {
53+
process.chdir(fixture())
54+
const { plugin } = await initPlugin({ exclude: 'test-dep' })
55+
t.is(await callHook(plugin, 'resolveId', 'test-dep', 'index.js'), null)
56+
})
57+
58+
test("Marks peerDependencies external by default", async t => {
59+
process.chdir(fixture())
60+
const { plugin } = await initPlugin()
61+
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
62+
})
63+
64+
test("Does NOT mark peerDependencies external when peerDeps=false", async t => {
65+
process.chdir(fixture())
66+
const { plugin } = await initPlugin({ peerDeps: false })
67+
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
68+
})
69+
70+
test("Does NOT mark excluded peerDependencies external", async t => {
71+
process.chdir(fixture())
72+
const { plugin } = await initPlugin({ exclude: 'test-peer-dep' })
73+
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
74+
})
75+
76+
test("Marks optionalDependencies external by default", async t => {
77+
process.chdir(fixture())
78+
const { plugin } = await initPlugin()
79+
t.false(await callHook(plugin, 'resolveId', 'test-opt-dep', 'index.js'))
80+
})
81+
82+
test("Does NOT mark optionalDependencies external when optDeps=false", async t => {
83+
process.chdir(fixture())
84+
const { plugin } = await initPlugin({ optDeps: false })
85+
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
86+
})
87+
88+
test("Does NOT mark excluded optionalDependencies external", async t => {
89+
process.chdir(fixture())
90+
const { plugin } = await initPlugin({ exclude: 'test-opt-dep' })
91+
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
92+
})
93+
94+
test("Does NOT mark devDependencies external by default", async t => {
95+
process.chdir(fixture())
96+
const { plugin } = await initPlugin()
97+
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
98+
})
99+
100+
test("Marks devDependencies external when devDeps=true", async t => {
101+
process.chdir(fixture())
102+
const { plugin } = await initPlugin({ devDeps: true })
103+
t.false(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'))
104+
})
105+
106+
test("Marks included devDependencies external", async t => {
107+
process.chdir(fixture())
108+
const { plugin } = await initPlugin({ include: 'test-dev-dep' })
109+
t.false(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'))
110+
})
111+
112+
test("Marks dependencies/peerDependencies/optionalDependencies subpath imports external", async t => {
113+
process.chdir(fixture())
114+
const { plugin } = await initPlugin()
115+
t.is(await callHook(plugin, 'resolveId', 'test-dep/sub', 'index.js'), false)
116+
t.is(await callHook(plugin, 'resolveId', 'test-peer-dep/sub', 'index.js'), false)
117+
t.is(await callHook(plugin, 'resolveId', 'test-opt-dep/sub', 'index.js'), false)
48118
})
49119

50120
test("Marks subpath imports external (with regexes)", async t => {
51-
const { plugin } = await initPlugin({
52-
include: [ /^lodash/ ]
53-
})
54-
for (const specifier of testSpecifiers.subpath) {
55-
t.is(await callHook(plugin, 'resolveId', specifier), false, `Failed on: ${specifier}`)
56-
}
121+
process.chdir(fixture())
122+
const { plugin } = await initPlugin({ include: /^test-dev-dep/ })
123+
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), false)
124+
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep/sub', 'index.js'), false)
57125
})

0 commit comments

Comments
 (0)