javascriptvue.jsvuejs3vitevitest

Vitest with element plus unplugin unknown extension for scss


I'm trying to run tests using Vitest on a Vue.js app that uses Element Plus registered as a plugin.

If I use mount on a component that contains an Element Plus component, I get the following error:

TypeError: Unknown file extension ".scss" for /home/projects/vitejs-vite-zcdxhn/node_modules/element-plus/theme-chalk/src/button.scss

The issue can be replicated on this StackBlitz.

My vite.config.js file looks like this:

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import ElementPlus from 'unplugin-element-plus/vite';

export default defineConfig({
  plugins: [vue(), ElementPlus({ useSource: true })],
});

My HelloWorld.vue component looks like this:

<script setup>
import { ElButton } from 'element-plus';
</script>

<template>
  <el-button type="primary">Hello</el-button>
</template>

My HelloWorld.spec.js looks like this:

import { test, expect } from 'vitest';
import HelloWorld from '../HelloWorld.vue';
import { mount } from '@vue/test-utils';

test('hello world test', async () => {
  const wrapper = mount(HelloWorld);
  expect(wrapper.text()).toContain('Hello');
});

The seems to be specifically related to the ElementPlus({ useSource: true })] "unplugin" in plugins in vite.config.js because when I remove that, the problem goes away.

I've reviewed the docs for the various tools (Element Plus, Vite, Vitest), but I've not been able to find how to get this working.

Is there a custom test config that needs to be applied?


Solution

  • In my situation, it fixed by add deps.inline: ['element-plus'] in vitest config

    References:

    Unknown file extension ".css" when using with unplugin-vue-components · Issue #1388 · vitest-dev/vitest · GitHub

    Configuring Vitest | Vitest