0

A BootstrapVue b-modal component in a custom Vue component loads correctly in the browser. However, when testing using mocha+mochapack, it generates a Vue warning that the b-modal element is not registered. The test is using a localVue object that has BootstrapVue registered. All other bootstrap custom elements seem to be loading correctly, and do not generate any warnings.

I tried various things, including importing BModal from 'bootstrap-vue' and registering it as a component directly, but still got the same error.

import {mount, createLocalVue} from "@vue/test-utils"
import MyCustomModal from '../js/MyCustomModal';

const localVue = createLocalVue();

import BootstrapVue from 'bootstrap-vue'
localVue.use(BootstrapVue);

describe('MyCustomModal', () => {
    let wrapper = mount(MyCustomModal,{
            localVue
        });

    it('the content is "this is the content"', () => {
        expect(wrapper.find(".modal-content").text()).toEqual('this is the content');
    });


});

The custom Vue component:

<template>
    <b-modal>
        <div class="modal-content">this is the content</div>
        <b-form>
           my form
        </b-form>
    </b-modal>
</template>

<script>
    export default {
        data(){
            return {};
        }
    }
</script>

The tests run correctly and pass, but it outputs the Vue warning for the b-modal element. It doesn't output the warning for b-form.

1
  • Do you try use shallowMount instead mount from vue-utlis? Commented Jun 7, 2019 at 19:27

3 Answers 3

2

If only shallowMount not work. You can try stub your bootstrap's components individually.

Like this:

import {shallowMount} from "@vue/test-utils";
import { BModal, BForm } from 'bootstrap-vue';
import MyCustomModal from '../js/MyCustomModal';

describe('MyCustomModal', () => {
    let wrapper = shallowMount(MyCustomModal,{
            stubs: {
                "b-modal": BModal,
              "b-form": BForm
            }
        });

    it('the content is "this is the content"', () => {
        expect(wrapper.find(".modal-content").text()).toEqual('this is the content');
    });

});
Sign up to request clarification or add additional context in comments.

8 Comments

I tried stubbing the components this way, but I got an error: Error: [vue-test-utils]: options.stub values must be passed a string or component
Yeah, because that, I imported the components { BModal, BForm } individually from bootstrap-vue. Do you have imported components above?
If no working, maybe try stub with just string with component name like this: stubs: { "b-modal", "b-form" }
Yeah, I imported the components BModal and BForm, didn't work. Stubbing them with strings allowed me to hide the error, which is good. However, now I can't test that the modal is actually working (meaning as a popup).
Yeah, @CENT1PEDE you right. The right syntax must be stubs: { "b-modal": true, "b-form": true } or the explicit components instead true. Thanks for catch. Doc stubs: vue-test-utils.vuejs.org/api/options.html#stubs
|
0

You need to set the attachToDocument: true flag when you mount b-modal (or your test component/app). It needs reference to the document/body in order for it to open (needs to add classes, etc to <body> as well as a few listeners.

2 Comments

options.attachToDocument is deprecated in favor of options.attachTo
0
import Vue from 'vue';
import {mount} from "@vue/test-utils"
import MyCustomModal from '../js/MyCustomModal';
    
import BootstrapVue from 'bootstrap-vue'
Vue.use(BootstrapVue);

describe('MyCustomModal', () => {
    let wrapper = mount(MyCustomModal);

    it('the content is "this is the content"', () => {
        expect(wrapper.find(".modal-content").text()).toEqual('this is the content');
    });


});

Try that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.