Skip to content

Commit 620327d

Browse files
baiwusanyu-cLinusBorg
andauthoredNov 25, 2022
fix(runtime-core): ensure prop type validation warning shows custom class names (#7198)
* fix(runtime-core): * fix(runtime-core): update * fix(runtime-core): update reg * test(runtime-core): add test case for warnings about prop type mismatches Co-authored-by: Thorsten Luenborg <t.luenborg@googlemail.com>
1 parent f3e4f03 commit 620327d

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
 

‎packages/runtime-core/__tests__/componentProps.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,42 @@ describe('component props', () => {
321321
expect(`Missing required prop: "num"`).toHaveBeenWarned()
322322
})
323323

324+
test('warn on type mismatch', () => {
325+
class MyClass {
326+
327+
}
328+
const Comp = {
329+
props: {
330+
bool: { type: Boolean },
331+
str: { type: String },
332+
num: { type: Number },
333+
arr: { type: Array },
334+
obj: { type: Object },
335+
cls: { type: MyClass },
336+
fn: { type: Function },
337+
},
338+
setup() {
339+
return () => null
340+
}
341+
}
342+
render(h(Comp, {
343+
bool: 'true',
344+
str: 100,
345+
num: '100',
346+
arr: {},
347+
obj: 'false',
348+
cls: {},
349+
fn: true,
350+
}), nodeOps.createElement('div'))
351+
expect(`Invalid prop: type check failed for prop "bool". Expected Boolean, got String`).toHaveBeenWarned()
352+
expect(`Invalid prop: type check failed for prop "str". Expected String with value "100", got Number with value 100.`).toHaveBeenWarned()
353+
expect(`Invalid prop: type check failed for prop "num". Expected Number with value 100, got String with value "100".`).toHaveBeenWarned()
354+
expect(`Invalid prop: type check failed for prop "arr". Expected Array, got Object`).toHaveBeenWarned()
355+
expect(`Invalid prop: type check failed for prop "obj". Expected Object, got String with value "false"`).toHaveBeenWarned()
356+
expect(`Invalid prop: type check failed for prop "fn". Expected Function, got Boolean with value true.`).toHaveBeenWarned()
357+
expect(`Invalid prop: type check failed for prop "cls". Expected MyClass, got Object`).toHaveBeenWarned()
358+
})
359+
324360
// #3495
325361
test('should not warn required props using kebab-case', async () => {
326362
const Comp = {

‎packages/runtime-core/src/componentProps.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,8 @@ function validatePropName(key: string) {
557557
// use function string name to check type constructors
558558
// so that it works across vms / iframes.
559559
function getType(ctor: Prop<any>): string {
560-
const match = ctor && ctor.toString().match(/^\s*function (\w+)/)
561-
return match ? match[1] : ctor === null ? 'null' : ''
560+
const match = ctor && ctor.toString().match(/^\s*(function|class) (\w+)/)
561+
return match ? match[2] : ctor === null ? 'null' : ''
562562
}
563563

564564
function isSameType(a: Prop<any>, b: Prop<any>): boolean {

0 commit comments

Comments
 (0)
Please sign in to comment.