メンタルゴリラの開発ブログ

ジュニアレベルのエンジニアがノーマルレベルになるための過程を残していくブログです。

【Jest】ケバブケースのとき、emitの指定どうすんだっけ?

testしたいmethods

methods: {
  submit(): void {
    this.$emit('submit-post', this.body)
  }
}

普通なら

expect(wrapper.emitted.submit[0])

とかで指定できる。 でもケバブケースの場合どうやってemitのテストしたらいいの?

answer

describe('submit', () => {
  it('親コンポーネントにデータが渡されること', () => {
    const wrapper = shallowMount(Target)
    wrapper.setData({
      body: {
        text: 'a'
      },
    })
    wrapper.vm.submit()
    expect(wrapper.emitted("submit-post")[0][0]).toEqual({ "text": "a"})
  })
})

emitの中で文字列で渡せば良いらしい。解決。