Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Septh committed Jun 2, 2023
1 parent 85475de commit adc236d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 66 deletions.
10 changes: 0 additions & 10 deletions test/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ type ImplementedHooks =
| 'buildStart'
| 'resolveId'

// This config makes for empty include[] and exclude[] patterns.
export const noDepsAtAllOptions: ExternalsOptions = {
packagePath: path.join(__dirname, 'fixtures/no-deps.package.json'),
builtins: false,
deps: false,
devDeps: false,
optDeps: false,
peerDeps: false
}

export async function callHook(plugin: Plugin, hookName: ImplementedHooks, ...args: any[]) {
const hook = plugin[hookName] as ObjectHook<(this: typeof fakePluginContext, ...args: any) => any>
if (typeof hook === 'function')
Expand Down
72 changes: 61 additions & 11 deletions test/builtins.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
import test from 'ava'
import { initPlugin, callHook } from './_common.js'

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), {
external: true
})
}
})

test("Does NOT mark Node builtins external when builtins=false", async t => {
const { plugin } = await initPlugin({
builtins: false
})
for (const builtin of [ 'path', 'node:fs' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
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' ]
})
for (const builtin of [ 'path', 'node:fs' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
external: true
})
}
})

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

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), {
id: 'node:path',
external: true
id: 'node:path'
})
}
})
Expand All @@ -17,8 +60,7 @@ test("Removes 'node:' prefix when using builtinsPrefix='strip'", async t => {
})
for (const builtin of [ 'node:path', 'path' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
id: 'path',
external: true
id: 'path'
})
}
})
Expand All @@ -27,12 +69,20 @@ test("Ignores 'node:' prefix when using builtinsPrefix='ignore'", async t => {
const { plugin } = await initPlugin({
builtinsPrefix: 'ignore'
})
t.like(await callHook(plugin, 'resolveId', 'node:path'), {
id: 'node:path',
external: true
})
t.like(await callHook(plugin, 'resolveId', 'path'), {
id: 'path',
external: true
for (const builtin of [ 'node:path', 'path' ]) {
t.like(await callHook(plugin, 'resolveId', builtin), {
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), {
id: builtin
})
}
})
3 changes: 0 additions & 3 deletions test/fixtures/no-deps.package.json

This file was deleted.

78 changes: 36 additions & 42 deletions test/specifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,57 @@
import test from 'ava'
import { initPlugin, callHook, noDepsAtAllOptions } from './_common.js'
import { initPlugin, callHook } from './_common.js'

test("Does NOT filter out relative specifiers by default", async t => {
const relativeSpecifiers = [ './sibling.js', '../parent.js' ]
const { plugin } = await initPlugin(noDepsAtAllOptions)
for (const specifier of relativeSpecifiers) {
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed id: ${specifier}`)
const testSpecifiers = {
virtual: [ '\\0virtual' ],
absolute: [ '/root.js' ],
absoluteWin32: [ '/root.js', '\\root.js', 'C:\\root.js' ],
bare: [ 'bare' ],
relative: [ './sibling.js', '../parent.js' ],
subpath: [ 'lodash', 'lodash/flatten.js' ],
}

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

test("Does NOT filter out relative specifiers, even when asked to", async t => {
const relativeSpecifiers = [ './sibling.js', '../parent.js' ]
const { plugin } = await initPlugin({
...noDepsAtAllOptions,
include: relativeSpecifiers
})
for (const specifier of relativeSpecifiers) {
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed id: ${specifier}`)
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}`)
}
})

test("Does NOT filter out absolute specifiers by default", async t => {
const absoluteSpecifiers = [ '/root.js' ]
if (process.platform === 'win32')
absoluteSpecifiers.push('\\root.js', 'C:\\root.js')
const { plugin } = await initPlugin(noDepsAtAllOptions)
for (const specifier of absoluteSpecifiers) {
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed id: ${specifier}`)
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}`)
}
})

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

test("Does NOT filter out bare specifiers by default", async t => {
const bareSpecifiers = [ 'dependency' ]
const { plugin } = await initPlugin(noDepsAtAllOptions)
for (const specifier of bareSpecifiers) {
t.is(await callHook(plugin, 'resolveId', specifier), null, `Failed id: ${specifier}`)
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("Filters out bare specifiers when asked to", async t => {
const bareSpecifiers = [ 'bare' ]
test("Marks subpath imports external (with regexes)", async t => {
const { plugin } = await initPlugin({
...noDepsAtAllOptions,
include: bareSpecifiers
include: [ /^lodash/ ]
})
for (const specifier of bareSpecifiers) {
t.is(await callHook(plugin, 'resolveId', specifier), false, `Failed id: ${specifier}`)
for (const specifier of testSpecifiers.subpath) {
t.is(await callHook(plugin, 'resolveId', specifier), false, `Failed on: ${specifier}`)
}
})

0 comments on commit adc236d

Please sign in to comment.