From adc236d1d2a275f77d96ad3faf08fcee3d055ab7 Mon Sep 17 00:00:00 2001 From: Stephan Schreiber Date: Sat, 3 Jun 2023 01:10:04 +0200 Subject: [PATCH] Update tests --- test/_common.ts | 10 ---- test/builtins.test.ts | 72 ++++++++++++++++++++++----- test/fixtures/no-deps.package.json | 3 -- test/specifier.test.ts | 78 ++++++++++++++---------------- 4 files changed, 97 insertions(+), 66 deletions(-) delete mode 100644 test/fixtures/no-deps.package.json diff --git a/test/_common.ts b/test/_common.ts index a4f4428..5e0b79e 100644 --- a/test/_common.ts +++ b/test/_common.ts @@ -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') diff --git a/test/builtins.test.ts b/test/builtins.test.ts index 6db5ae2..3ccbd08 100644 --- a/test/builtins.test.ts +++ b/test/builtins.test.ts @@ -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' }) } }) @@ -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' }) } }) @@ -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 + }) + } }) diff --git a/test/fixtures/no-deps.package.json b/test/fixtures/no-deps.package.json deleted file mode 100644 index d6c9ba6..0000000 --- a/test/fixtures/no-deps.package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "workspaces": [] -} diff --git a/test/specifier.test.ts b/test/specifier.test.ts index acae1f4..679f121 100644 --- a/test/specifier.test.ts +++ b/test/specifier.test.ts @@ -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}`) } })