Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Septh committed Jan 31, 2024
1 parent 6c27603 commit 99b5167
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 104 deletions.
63 changes: 27 additions & 36 deletions test/builtins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,75 @@ import { initPlugin, callHook } from './_common.ts'
test("Marks Node builtins external by default", async t => {
const { plugin } = await initPlugin()
for (const builtin of [ 'path', 'node:fs' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
external: true
})
}
})

test("Does NOT mark Node builtins external when builtins=false", async t => {
const { plugin } = await initPlugin({
builtins: false
})
const { plugin } = await initPlugin({ builtins: false })
for (const builtin of [ 'path', 'node:fs' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
external: false
})
}
})

test("Marks Node builtins external when builtins=false and implicitly included", async t => {
const { plugin } = await initPlugin({
builtins: false,
include: [ 'path', 'node:fs' ]
})
test("Does NOT mark Node builtins external when implicitely excluded", async t => {
const { plugin } = await initPlugin({ exclude: [ 'path', 'node:fs' ]})
for (const builtin of [ 'path', 'node:fs' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
external: true
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
external: false
})
}
})

test("Does NOT mark Node builtins external when builtins=true implicitly excluded", async t => {
const { plugin } = await initPlugin({
builtins: true,
exclude: [ 'path', 'node:fs' ]
})
test("Marks Node builtins external when builtins=false and implicitly included", async t => {
const { plugin } = await initPlugin({ builtins: false, include: [ 'path', 'node:fs' ] })
for (const builtin of [ 'path', 'node:fs' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
external: false
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
external: true
})
}
})

test("Adds 'node:' prefix to builtins by default", async t => {
const { plugin } = await initPlugin()
for (const builtin of [ 'node:path', 'path' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
id: 'node:path'
})
}
})

test("Removes 'node:' prefix when using builtinsPrefix='strip'", async t => {
const { plugin } = await initPlugin({
builtinsPrefix: 'strip'
})
const { plugin } = await initPlugin({ builtinsPrefix: 'strip' })
for (const builtin of [ 'node:path', 'path' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
id: 'path'
})
}
})

test("Ignores 'node:' prefix when using builtinsPrefix='ignore'", async t => {
const { plugin } = await initPlugin({
builtinsPrefix: 'ignore'
})
for (const builtin of [ 'node:path', 'path' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
test("Does NOT remove 'node:test' prefix even with builtinsPrefix='add'", async t => {
const { plugin } = await initPlugin({ builtinsPrefix: 'strip' })
for (const builtin of [ 'node:test' ]) {
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
id: builtin
})
}
})

test("Does NOT remove 'node:' prefix for specific builtins, even with builtinsPrefix='add'", async t => {
const { plugin } = await initPlugin({
builtinsPrefix: 'strip'
})
for (const builtin of [ 'node:test' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
test("Does not recognize 'test' as a Node builtin", async t => {
const { plugin } = await initPlugin()
t.is(await callHook(plugin, 'resolveId', 'node', 'index.js'), null)
})

test("Ignores 'node:' prefix when using builtinsPrefix='ignore'", async t => {
const { plugin } = await initPlugin({ builtinsPrefix: 'ignore' })
for (const builtin of [ 'node:path', 'path' ]) {
t.like(await callHook(plugin, 'resolveId', builtin, 'index.js'), {
id: builtin
})
}
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
},
"devDependencies": {
"test-dev-dep": "*"
},
"peerDependencies": {
"test-peer-dep": "*"
},
"optionalDependencies": {
"test-opt-dep": "*"
}
}
31 changes: 19 additions & 12 deletions test/monorepo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,47 @@ test.serial('git monorepo usage', async t => {
await fs.mkdir(fixture('01_monorepo/.git'), { recursive: true })
process.chdir(fixture('01_monorepo/one'))

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

// Should be external
for (const dependency of [
'moment', // 01_monorepo/one/package.json
'chalk' // 01_monorepo/package.json
'moment', // dependency in ./test/fixtures/01_monorepo/one/package.json (picked)
'chalk' // dependency in ./test/fixtures/01_monorepo/package.json (picked)
]) {
t.false(await callHook(plugin, 'resolveId', dependency))
t.false(await callHook(plugin, 'resolveId', dependency, 'index.js'))
}

// Should be ignored
for (const dependency of [
'react', // 01_monorepo/two/package.json
'test-dep' // ./package.json
'react', // dependency in ./test/fixtures/01_monorepo/two/package.json (not picked)
'test-dep' // dependency in ./test/fixtures/package.json (not picked)
]) {
t.is(await callHook(plugin, 'resolveId', dependency), null)
t.is(await callHook(plugin, 'resolveId', dependency, 'index.js'), null)
}
})

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

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

// Should be external
for (const dependency of [
'moment', // 01_monorepo/one/package.json
'chalk', // 01_monorepo/package.json
'test-dep' // ./package.json
'moment', // dependency in ./test/fixtures/01_monorepo/one/package.json (picked)
'chalk', // dependency in ./test/fixtures/01_monorepo/package.json (picked)
'test-dep', // dependency in ./test/fixtures/package.json (picked)
'rollup', // peer dependency in ./package.json (picked !)
]) {
t.false(await callHook(plugin, 'resolveId', dependency))
t.false(await callHook(plugin, 'resolveId', dependency, 'index.js'))
}

// Should be ignored
t.is(await callHook(plugin, 'resolveId', 'react'), null)
for (const dependency of [
'react' // dependency in ./test/fixtures/01_monorepo/two/package.json (not picked)
]) {
t.is(await callHook(plugin, 'resolveId', dependency, 'index.js'), null)
}
})
21 changes: 2 additions & 19 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,11 @@ test.serial("Warns when given invalid include or exclude entry", async t => {
t.is(warnings[0], `Ignoring wrong entry type #1 in 'include' option: ${JSON.stringify(notOkay)}`)
})

test('Marks dependencies as external by default', async t => {
const { plugin } = await initPlugin()
t.false(await callHook(plugin, 'resolveId', 'test-dep'))
})

test('Does NOT mark devDependencies as external by default', async t => {
const { plugin } = await initPlugin()
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep'), null)
})

test('Does mark devDependencies as external when using devDeps=true', async t => {
const { plugin } = await initPlugin({
devDeps: true
})
t.false(await callHook(plugin, 'resolveId', 'test-dev-dep'))
})

test("Obeys 'packagePath' option (single file name)", async t => {
const { plugin } = await initPlugin({
packagePath: '00_simple/package.json'
})
t.false(await callHook(plugin, 'resolveId', 'simple-dep'))
t.false(await callHook(plugin, 'resolveId', 'simple-dep', 'index.js'))
})

test("Obeys 'packagePath' option (multiple file names)", async t => {
Expand All @@ -85,6 +68,6 @@ test("Obeys 'packagePath' option (multiple file names)", async t => {
'simple-dep', // 00_simple/package.json
'chalk', // 01_monorepo/package.json
]) {
t.false(await callHook(plugin, 'resolveId', dependency))
t.false(await callHook(plugin, 'resolveId', dependency, 'index.js'))
}
})
130 changes: 99 additions & 31 deletions test/specifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,125 @@
import test from 'ava'
import { initPlugin, callHook } from './_common.ts'
import { initPlugin, callHook, fixture } from './_common.ts'

const testSpecifiers = {
const specifiers = {
virtual: [ '\\0virtual' ],
absolute: [ '/root.js' ],
absolutePosix: [ '/root.js' ],
absoluteWin32: [ '/root.js', '\\root.js', 'C:\\root.js' ],
bare: [ 'bare' ],
bare: [ 'foo', 'bar' ],
relative: [ './sibling.js', '../parent.js' ],
subpath: [ 'lodash', 'lodash/flatten.js' ],
subpath: [ 'lodash', 'lodash/flatten' ],
}

test("Always ignores virtual modules", async t => {
test("Always ignores bundle entry point", async t => {
const { plugin } = await initPlugin()
for (const specifier of testSpecifiers.virtual) {
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed on: ${specifier}`)
}
t.is(await callHook(plugin, 'resolveId', './path/to/entry.js', undefined), null)
})

test("Always ignores virtual modules from other plugins", async t => {
const { plugin } = await initPlugin()
t.is(await callHook(plugin, 'resolveId', '\\0virtual', undefined), null, `Failed without importer`)
t.is(await callHook(plugin, 'resolveId', '\\0virtual', 'file.js'), null, `Failed with importer`)
})

test("Always ignores absolute specifiers", async t => {
const { plugin } = await initPlugin()
for (const specifier of testSpecifiers[process.platform === 'win32' ? 'absoluteWin32' : 'absolute']) {
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed on: ${specifier}`)
for (const specifier of specifiers[process.platform === 'win32' ? 'absoluteWin32' : 'absolutePosix']) {
t.is(await callHook(plugin, 'resolveId', specifier, undefined), null, `Failed on: ${specifier} without importer`)
t.is(await callHook(plugin, 'resolveId', specifier, 'file.js'), null, `Failed on: ${specifier} with importer`)
}
})

test("Always ignores relative specifiers", async t => {
const { plugin } = await initPlugin()
for (const specifier of testSpecifiers.relative) {
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed on: ${specifier}`)
const { plugin } = await initPlugin({ include: specifiers.relative })
for (const specifier of specifiers.relative) {
t.is(await callHook(plugin, 'resolveId', specifier, undefined), null, `Failed on: ${specifier} without importer`)
t.is(await callHook(plugin, 'resolveId', specifier, 'file.js'), null, `Failed on: ${specifier} with importer`)
}
})

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

test("Marks bare specifiers external when asked to", async t => {
const { plugin } = await initPlugin({
include: testSpecifiers.bare
})
for (const specifier of testSpecifiers.bare) {
t.is(await callHook(plugin, 'resolveId', specifier), false, `Failed on: ${specifier}`)
}
test("Does NOT mark dependencies external when deps=false", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin({ deps: false })
t.is(await callHook(plugin, 'resolveId', 'test-dep', 'index.js'), null)
})

test("Does NOT mark excluded dependencies external", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin({ exclude: 'test-dep' })
t.is(await callHook(plugin, 'resolveId', 'test-dep', 'index.js'), null)
})

test("Marks peerDependencies external by default", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin()
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
})

test("Does NOT mark peerDependencies external when peerDeps=false", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin({ peerDeps: false })
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
})

test("Does NOT mark excluded peerDependencies external", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin({ exclude: 'test-peer-dep' })
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
})

test("Marks optionalDependencies external by default", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin()
t.false(await callHook(plugin, 'resolveId', 'test-opt-dep', 'index.js'))
})

test("Does NOT mark optionalDependencies external when optDeps=false", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin({ optDeps: false })
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
})

test("Does NOT mark excluded optionalDependencies external", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin({ exclude: 'test-opt-dep' })
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
})

test("Does NOT mark devDependencies external by default", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin()
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), null)
})

test("Marks devDependencies external when devDeps=true", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin({ devDeps: true })
t.false(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'))
})

test("Marks included devDependencies external", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin({ include: 'test-dev-dep' })
t.false(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'))
})

test("Marks dependencies/peerDependencies/optionalDependencies subpath imports external", async t => {
process.chdir(fixture())
const { plugin } = await initPlugin()
t.is(await callHook(plugin, 'resolveId', 'test-dep/sub', 'index.js'), false)
t.is(await callHook(plugin, 'resolveId', 'test-peer-dep/sub', 'index.js'), false)
t.is(await callHook(plugin, 'resolveId', 'test-opt-dep/sub', 'index.js'), false)
})

test("Marks subpath imports external (with regexes)", async t => {
const { plugin } = await initPlugin({
include: [ /^lodash/ ]
})
for (const specifier of testSpecifiers.subpath) {
t.is(await callHook(plugin, 'resolveId', specifier), false, `Failed on: ${specifier}`)
}
process.chdir(fixture())
const { plugin } = await initPlugin({ include: /^test-dev-dep/ })
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep', 'index.js'), false)
t.is(await callHook(plugin, 'resolveId', 'test-dev-dep/sub', 'index.js'), false)
})
Loading

0 comments on commit 99b5167

Please sign in to comment.