test: slightly improve coverage

This commit is contained in:
Daniel Roe 2025-02-12 10:26:05 +00:00
parent 3854cb737c
commit 47427adff9
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 27 additions and 4 deletions

View File

@ -18,6 +18,7 @@ export const ComponentNamePlugin = (options: NameDevPluginOptions) => createUnpl
name: 'nuxt:component-name-plugin', name: 'nuxt:component-name-plugin',
enforce: 'post', enforce: 'post',
transformInclude (id) { transformInclude (id) {
/* v8 ignore next 2 */
return isVue(id) || !!id.match(SX_RE) return isVue(id) || !!id.match(SX_RE)
}, },
transform (code, id) { transform (code, id) {
@ -53,6 +54,7 @@ export const ComponentNamePlugin = (options: NameDevPluginOptions) => createUnpl
return { return {
code: s.toString(), code: s.toString(),
map: options.sourcemap map: options.sourcemap
/* v8 ignore next */
? s.generateMap({ hires: true }) ? s.generateMap({ hires: true })
: undefined, : undefined,
} }

View File

@ -6,13 +6,34 @@ import * as Parser from 'acorn'
import { ComponentNamePlugin } from '../src/components/plugins/component-names' import { ComponentNamePlugin } from '../src/components/plugins/component-names'
describe('component names', () => { describe('component names', () => {
const components = [{ const components = [
filePath: 'test.ts', {
pascalName: 'TestMe', filePath: 'test.ts',
}] as [Component] pascalName: 'TestMe',
},
{
filePath: 'test.vue',
pascalName: 'TestMe',
},
] as [Component, Component]
const transformPlugin = ComponentNamePlugin({ sourcemap: false, getComponents: () => components }).raw({}, {} as any) as { transform: (code: string, id: string) => { code: string } | null } const transformPlugin = ComponentNamePlugin({ sourcemap: false, getComponents: () => components }).raw({}, {} as any) as { transform: (code: string, id: string) => { code: string } | null }
it('should ignore files without extension', () => {
const res = transformPlugin.transform('export default {}', 'test')
expect(res?.code).toBeUndefined()
})
it('should ignore files that are not components ', () => {
const res = transformPlugin.transform('export default {}', 'some-other-file.ts')
expect(res?.code).toBeUndefined()
})
it('should process simple default exports', () => {
const res = transformPlugin.transform('export default {}', 'test.vue')
expect(res?.code).toMatchInlineSnapshot(`"export default Object.assign({}, { __name: "TestMe" })"`)
})
it('should add correct default component names', () => { it('should add correct default component names', () => {
const sfc = ` const sfc = `
<script setup> <script setup>