Skip to content

Commit 864e9e2

Browse files
authored
fix: return the correct name when stubbing a script setup component (#1783)
1 parent ee716fc commit 864e9e2

File tree

6 files changed

+149
-5
lines changed

6 files changed

+149
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"rollup-plugin-typescript2": "0.34.0",
5353
"tslib": "2.4.0",
5454
"typescript": "4.8.3",
55+
"unplugin-vue-components": "0.22.7",
5556
"vite": "3.1.3",
5657
"vitepress": "0.22.4",
5758
"vitest": "0.22.1",

pnpm-lock.yaml

Lines changed: 114 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/componentName.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ export const getComponentName = (
3838
type: VNodeTypes
3939
): string => {
4040
if (isObjectComponent(type)) {
41-
return getComponentNameInSetup(instance, type) || type.name || ''
41+
return (
42+
// If the component we stub is a script setup component and is automatically
43+
// imported by unplugin-vue-components we can only get its name through
44+
// the `__name` property.
45+
getComponentNameInSetup(instance, type) || type.name || type.__name || ''
46+
)
4247
}
4348

4449
if (isLegacyExtendedComponent(type)) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<ScriptSetup/>
3+
</template>

tests/mountingOptions/global.stubs.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Hello from '../components/Hello.vue'
66
import ComponentWithoutName from '../components/ComponentWithoutName.vue'
77
import ComponentWithSlots from '../components/ComponentWithSlots.vue'
88
import ScriptSetupWithChildren from '../components/ScriptSetupWithChildren.vue'
9+
import AutoImportScriptSetup from '../components/AutoImportScriptSetup.vue'
910

1011
describe('mounting options: stubs', () => {
1112
let configStubsSave = config.global.stubs
@@ -393,6 +394,17 @@ describe('mounting options: stubs', () => {
393394
)
394395
})
395396

397+
it('stubs a script setup component imported by unplugin-vue-components', () => {
398+
const wrapper = mount(AutoImportScriptSetup, {
399+
global: {
400+
stubs: {
401+
ScriptSetup: true
402+
}
403+
}
404+
})
405+
expect(wrapper.html()).toBe(`<script-setup-stub></script-setup-stub>`)
406+
})
407+
396408
describe('transition', () => {
397409
it('stubs transition by default', () => {
398410
const CompStubbedByDefault = {

vitest.config.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ import path from 'path'
22
import { defineConfig } from 'vitest/config'
33
import vue from '@vitejs/plugin-vue'
44
import jsx from '@vitejs/plugin-vue-jsx'
5+
import Components from 'unplugin-vue-components/vite'
56

67
export default defineConfig({
7-
plugins: [vue(), jsx()],
8+
plugins: [
9+
vue(),
10+
jsx(),
11+
// We need this plugin to test for stubbing a script setup component
12+
// imported by it.
13+
// https://github.com/antfu/unplugin-vue-components/issues/429
14+
Components({
15+
dts: false,
16+
include: /AutoImportScriptSetup\.vue$/,
17+
dirs: ['tests/components']
18+
})
19+
],
820
define: {
921
__USE_BUILD__: process.env.NODE_ENV !== 'test-build',
1022
__BROWSER__: true,

0 commit comments

Comments
 (0)