Nuxt/packages/nuxt/test/import-protection.test.ts

56 lines
2.3 KiB
TypeScript
Raw Normal View History

import { normalize } from 'pathe'
import { describe, expect, it } from 'vitest'
import { ImpoundPlugin } from 'impound'
import { createImportProtectionPatterns } from '../src/core/plugins/import-protection'
import type { NuxtOptions } from '../schema'
const testsToTriggerOn = [
['~/nuxt.config', 'app.vue', true],
['./nuxt.config', 'app.vue', true],
['./nuxt.config.ts', 'app.vue', true],
['nuxt.config.ts', 'app.vue', true],
['./.nuxt/nuxt.config', 'app.vue', false],
['.nuxt/nuxt.config', 'app.vue', false],
['nuxt', 'components/Component.vue', true],
['nuxt3', 'components/Component.vue', true],
['nuxt-nightly', 'components/Component.vue', true],
['/root/node_modules/@vue/composition-api', 'components/Component.vue', true],
['@vue/composition-api', 'components/Component.vue', true],
['@nuxt/kit', 'components/Component.vue', true],
['nuxt/config', 'components/Component.vue', true],
['nuxt/kit', 'components/Component.vue', true],
['nuxt/schema', 'components/Component.vue', true],
['/root/node_modules/@nuxt/kit', 'components/Component.vue', true],
['some-nuxt-module', 'components/Component.vue', true],
['/root/src/server/api/test.ts', 'components/Component.vue', true],
['src/server/api/test.ts', 'components/Component.vue', true],
2024-11-19 18:56:33 +00:00
['node_modules/nitropack/node_modules/crossws/dist/adapters/bun.mjs', 'node_modules/nitropack/dist/presets/bun/runtime/bun.mjs', false],
] as const
describe('import protection', () => {
it.each(testsToTriggerOn)('should protect %s', async (id, importer, isProtected) => {
const result = await transformWithImportProtection(id, importer, 'nuxt-app')
if (!isProtected) {
expect(result).toBeNull()
} else {
expect(result).toBeDefined()
expect(normalize(result)).contains('unenv/runtime/mock/proxy')
}
})
})
const transformWithImportProtection = (id: string, importer: string, context: 'nitro-app' | 'nuxt-app' | 'shared') => {
const plugin = ImpoundPlugin.rollup({
cwd: '/root',
patterns: createImportProtectionPatterns({
options: {
modules: ['some-nuxt-module'],
srcDir: '/root/src/',
serverDir: '/root/src/server',
} satisfies Partial<NuxtOptions> as NuxtOptions,
}, { context }),
})
return (plugin as any).resolveId.call({ error: () => {} }, id, importer)
}