Skip to content

jest in vue-cli4 with typescript #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kunfan96 opened this issue Apr 26, 2020 · 1 comment
Closed

jest in vue-cli4 with typescript #7

kunfan96 opened this issue Apr 26, 2020 · 1 comment

Comments

@kunfan96
Copy link

kunfan96 commented Apr 26, 2020

when i want to test a vue components in components.ecpect.ts

import About from '@/components/About.vue'
import { shallowMount } from '@vue/test-utils'

describe('测试事件', () => {
  test('测试事件1', () => {
    const wrapper = shallowMount(About)
    console.log(wrapper.vm)
    wrapper.vm.toggleShow()
    // 执行组件事件
    expect(wrapper.element.style.display).toBe('none')
  })
})

it reports

Test suite failed to run

    TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):

    src/components/__tests__/About.spec.ts:7:16 - error TS2339: Property 'toggleShow' does not exist on type 'CombinedVueInstance<Vue, object, object, object, Record<never, any>>'.
@cmauf
Copy link

cmauf commented Mar 6, 2022

Problem is that ts-jest uses the Vue prototype, not the instance. In order to avoid the error, use (wrapper.vm as any).toggleShow() instead of wrapper.vm.showToggle() (see this issue for reference)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants