Skip to content

Commit ef53aea

Browse files
committed
🐛 Avoid emitting undefined as a return value
Consider an empty emitter function: ```ts @component class MyComponent { @emit public foo(): void {} } ``` We would expect this to emit a `'foo'` event with no arguments (since the return value is `undefined`). However, when testing with `@vue/test-utils`, we have to write: ```ts expect(component.emitted('foo')).to.eql([[undefined]]) ``` But it would be nicer - and more consistent with [`vue-property-decorator`][1] - if we instead called `$emit()` with no arguments in this case to be able to write: ```ts expect(component.emitted('foo')).to.eql([[]]) ``` [1]: https://github.com/kaorun343/vue-property-decorator/blob/e04872349bab357e6d08b01442f83abc9976eb50/src/decorators/Emit.ts#L22
1 parent 53ec0ac commit ef53aea

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/option/emit.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
2828
const proRet = await ret
2929
this.$emit(eventName, proRet)
3030
}
31-
else {
31+
else if (ret === undefined) {
32+
this.$emit(eventName)
33+
} else {
3234
this.$emit(eventName, ret)
3335
}
3436
}

test/option/emit.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { expect } from 'chai';
33
import 'mocha';
44
import { Component, Emit, Base, toNative } from '../../dist'
5+
import {mount} from '@vue/test-utils';
56

67
@Component
78
class Comp extends Base {
@@ -25,6 +26,9 @@ class Comp extends Base {
2526
})
2627
}
2728

29+
@Emit
30+
noReturn() {}
31+
2832
}
2933
const CompContext = toNative(Comp) as any
3034

@@ -73,6 +77,11 @@ describe('decorator Emit',
7377
expect(emitName).to.equal('promiseEmit')
7478
expect(emitValue).to.equal('promiseEmit value')
7579
})
80+
it('has an empty array when emitting void', () => {
81+
const component = mount(CompContext)
82+
component.vm.noReturn()
83+
expect(component.emitted('noReturn')).to.eql([[]])
84+
})
7685
}
7786
)
78-
export default {}
87+
export default {}

0 commit comments

Comments
 (0)