Search code examples
typescriptvue.jsvuejs3vitevitest

Register custom plugins globally in vitest


I have a lot of vittest files and i want to register the plugins globally for all test files. Currently most of the test files are mounting the VueWrapper as follows.

  const wrapper = shallowMount(MyComponent, {
    global: {
      plugins: [i18n, MyCustomPlugin, [MyCustomLoggerPlugin, 'MyCustomParameter']],
    },
    props: {
    },
  })

Is there a way that i can remove the plugin installation from the mount function and do this globally? I18n should stay in the test files as the localizations are different for each component.


Solution

  • Solved it by creating a vitetest.setup.ts

    import { config } from '@vue/test-utils'
    import MyCustomPlugin, { MyCustomLoggerPlugin} from 'xyz'
    
    config.global.plugins = [
      MyCustomPlugin,
      [MyCustomLoggerPlugin, 'MyCustomParameter'],
    ]
    

    this file has to be registered in vite.config.ts.

    test: {
        setupFiles: ['vitest.setup.ts'],
      },
    

    More information can be found in the documentation.