1

I have a project in Nuxt.js and I am trying to test it using Vue Test Util which works effectively except on files where I use BootstrapVue custom html elements.

When I run npm test this error displays for every BootsrapVue html element.

 console.error
      [Vue warn]: Unknown custom element: <b-container> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
      
      found in
      
      ---> <Anonymous>
             <Root>

How can I prevent this?

1 Answer 1

1

You need to setup bootstrap-vue in your Jest enviroment, using a Jest setup file.

Create the setup file:

// @/jest-setup.js
import Vue from 'vue'
import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Vue.use(BootstrapVue)
Vue.use(IconsPlugin)

Then configure Jest's setupFiles to load it:

// @/jest.config.js
module.exports = {
  setupFiles: ['./jest-setup.js'],
}
Sign up to request clarification or add additional context in comments.

5 Comments

Well since this is a Nuxt project I don't have a main.js file, is there a way to solve this without it?
Ok, then you'd still need to setup bootstrap-vue in your Jest environment, and the same steps would apply other than having to edit main.js.
Then I get this error: SyntaxError: Unexpected token ':' 5 | import 'bootstrap-vue/dist/bootstrap-vue.css' 6 | > 7 | Vue.use(BootstrapVue) | ^ 8 | Vue.use(IconsPlugin) 9 | 10 | at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14) at Object.<anonymous> (jest-setup.js:7:1)
That implies you don't have Babel setup properly for Jest. You could workaroudn that by replacing import with require statements (e.g., import Vue from 'vue' becomes const Vue = require('vue')).
It works! In my case using Cypress to test components. But bootstrap components not rendering, I added this code to cypress/support which can work, thanks a lot.

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.