2023-12-14 17:11:08 +00:00
|
|
|
import { describe, expect, it, vi } from 'vitest'
|
2023-06-23 23:01:17 +00:00
|
|
|
import { parse } from 'acorn'
|
2023-06-19 23:00:03 +00:00
|
|
|
|
|
|
|
import { RemovePluginMetadataPlugin, extractMetadata } from '../src/core/plugins/plugin-metadata'
|
2023-12-14 17:11:08 +00:00
|
|
|
import { checkForCircularDependencies } from '../src/core/app'
|
2023-06-19 23:00:03 +00:00
|
|
|
|
|
|
|
describe('plugin-metadata', () => {
|
2023-06-23 23:01:17 +00:00
|
|
|
it('should extract metadata from object-syntax plugins', async () => {
|
2023-06-19 23:00:03 +00:00
|
|
|
const properties = Object.entries({
|
|
|
|
name: 'test',
|
|
|
|
enforce: 'post',
|
|
|
|
hooks: { 'app:mounted': () => {} },
|
2024-03-13 22:50:15 +00:00
|
|
|
setup: () => { return { provide: { jsx: '[JSX]' } } },
|
2024-04-05 18:08:32 +00:00
|
|
|
order: 1,
|
2023-06-19 23:00:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for (const item of properties) {
|
|
|
|
const obj = [...properties.filter(([key]) => key !== item[0]), item]
|
|
|
|
|
2023-06-23 23:01:17 +00:00
|
|
|
const meta = await extractMetadata([
|
2023-06-19 23:00:03 +00:00
|
|
|
'export default defineNuxtPlugin({',
|
2024-03-13 22:50:15 +00:00
|
|
|
...obj.map(([key, value]) => `${key}: ${typeof value === 'function' ? value.toString().replace('"[JSX]"', '() => <span>JSX</span>') : JSON.stringify(value)},`),
|
2024-04-05 18:08:32 +00:00
|
|
|
'})',
|
2024-03-17 16:57:11 +00:00
|
|
|
].join('\n'), 'tsx')
|
2023-06-19 23:00:03 +00:00
|
|
|
|
2024-09-13 11:54:52 +00:00
|
|
|
expect(meta).toEqual({
|
|
|
|
'name': 'test',
|
|
|
|
'order': 1,
|
|
|
|
})
|
2023-06-19 23:00:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const transformPlugin: any = RemovePluginMetadataPlugin({
|
|
|
|
options: { sourcemap: { client: true } },
|
2024-04-05 18:08:32 +00:00
|
|
|
apps: { default: { plugins: [{ src: 'my-plugin.mjs', order: 10 }] } },
|
2023-06-19 23:00:03 +00:00
|
|
|
} as any).raw({}, {} as any)
|
|
|
|
|
|
|
|
it('should overwrite invalid plugins', () => {
|
|
|
|
const invalidPlugins = [
|
|
|
|
'export const plugin = {}',
|
|
|
|
]
|
|
|
|
for (const plugin of invalidPlugins) {
|
2023-06-23 23:01:17 +00:00
|
|
|
expect(transformPlugin.transform.call({ parse }, plugin, 'my-plugin.mjs').code).toBe('export default () => {}')
|
2023-06-19 23:00:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove order/name properties from object-syntax plugins', () => {
|
|
|
|
const plugin = `
|
|
|
|
export default defineNuxtPlugin({
|
|
|
|
name: 'test',
|
|
|
|
enforce: 'post',
|
|
|
|
setup: () => {},
|
|
|
|
}, { order: 10, name: test })
|
|
|
|
`
|
2023-06-23 23:01:17 +00:00
|
|
|
expect(transformPlugin.transform.call({ parse }, plugin, 'my-plugin.mjs').code).toMatchInlineSnapshot(`
|
2023-06-19 23:00:03 +00:00
|
|
|
"
|
|
|
|
export default defineNuxtPlugin({
|
|
|
|
setup: () => {},
|
|
|
|
}, { })
|
|
|
|
"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
})
|
2023-12-14 17:11:08 +00:00
|
|
|
|
|
|
|
describe('plugin sanity checking', () => {
|
|
|
|
it('non-existent depends are warned', () => {
|
|
|
|
vi.spyOn(console, 'error')
|
|
|
|
checkForCircularDependencies([
|
|
|
|
{
|
|
|
|
name: 'A',
|
2024-04-05 18:08:32 +00:00
|
|
|
src: '',
|
2023-12-14 17:11:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'B',
|
|
|
|
dependsOn: ['D'],
|
2024-04-05 18:08:32 +00:00
|
|
|
src: '',
|
2023-12-14 17:11:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'C',
|
2024-04-05 18:08:32 +00:00
|
|
|
src: '',
|
|
|
|
},
|
2023-12-14 17:11:08 +00:00
|
|
|
])
|
|
|
|
expect(console.error).toBeCalledWith('Plugin `B` depends on `D` but they are not registered.')
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('circular dependencies are warned', () => {
|
|
|
|
vi.spyOn(console, 'error')
|
|
|
|
checkForCircularDependencies([
|
|
|
|
{
|
|
|
|
name: 'A',
|
|
|
|
dependsOn: ['B'],
|
2024-04-05 18:08:32 +00:00
|
|
|
src: '',
|
2023-12-14 17:11:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'B',
|
|
|
|
dependsOn: ['C'],
|
2024-04-05 18:08:32 +00:00
|
|
|
src: '',
|
2023-12-14 17:11:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'C',
|
|
|
|
dependsOn: ['A'],
|
2024-04-05 18:08:32 +00:00
|
|
|
src: '',
|
|
|
|
},
|
2023-12-14 17:11:08 +00:00
|
|
|
])
|
|
|
|
expect(console.error).toBeCalledWith('Circular dependency detected in plugins: A -> B -> C -> A')
|
|
|
|
expect(console.error).toBeCalledWith('Circular dependency detected in plugins: B -> C -> A -> B')
|
|
|
|
expect(console.error).toBeCalledWith('Circular dependency detected in plugins: C -> A -> B -> C')
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
})
|
|
|
|
})
|