vue-final / vue-final-modal

🍕Vue Final Modal is a tiny, renderless, mobile-friendly, feature-rich modal component for Vue.js.
https://vue-final-modal.org
MIT License
862 stars 95 forks source link

How to config with jest #417

Open rock288 opened 6 months ago

rock288 commented 6 months ago

Version

vue-final-modal: 4.4.6 vue: 3.2.13 @vue/test-utils: 2.4.3

OS

Windows

Reproduction Link

Steps to reproduce

How to config with jest, use @vue/test-utils

What is Expected?

What is actually happening?

Can not config with jest

hunterliu1003 commented 6 months ago

@rock288

In the docs of @vue/test-utils, you can use Applying Global Plugins and Mixins.

For vue-final-modal, it might be like:

import { createLocalVue, mount } from '@vue/test-utils'
import { createVfm } from 'vue-final-modal'

// create an extended `Vue` constructor
const localVue = createLocalVue()

// install plugins as normal
const vfm = createVfm()
localVue.use(vfm)

// pass the `localVue` to the mount options
mount(Component, {
  localVue
})
rock288 commented 6 months ago

Hi @hunterliu1003 I am using @vue/test-utils: 2.4.3 with vue 3. Docs https://test-utils.vuejs.org/

hunterliu1003 commented 6 months ago

@rock288 Maybe like this?

test('global.plugins', () => {
  const vfm = createVfm()
  mount(App, {
    global: {
      plugins: [vfm],
      stubs: { transition: false },
    },
  })
})
rock288 commented 6 months ago

Hi @hunterliu1003 I encountered this error TypeError: (0 , vue_final_modal_1.createVfm) is not a function when following the steps above. It seems like there might be an issue with Jest configuration. Could you please help me create a Vue CLI repository and configure Jest to work with the vue-final-modal library?

vittoriaThinkst commented 3 months ago

Hi, did anybody manage to make a test working? Unfortunately I always get only an empty teleport. Not sure what I'm doing wrong

` describe('BaseModal.vue', () => { let wrapper;

beforeEach(() => {
    const vfm = createVfm();
    wrapper = mount(BaseModal, {
        props: { title: 'Test', hasBackButton: true },
        global: { plugins: [vfm], stubs: { teleport: true, FontAwesomeIcon } },
    });
   console.log(wrapper.html());
});

it('to see the modal!', async () => {
    const container = wrapper.find('.vfm__content');
    expect(container.element).toHaveClass('modal');
});`

the console.log for wrapper gives back

`<teleport-stub to="body" disabled="false">

`

My BaseModal component is quite simple

<VueFinalModal> <slot> </slot> </VueFinalModal>

Any idea anybody?