Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 219
LondonAppDev Avatar asked Oct 26 '25 17:10

LondonAppDev


1 Answers

This issue still happens to me even with vitest 2.1.1 (with error related to css extension)

The solution is to set the pool option to use vmThreads or vmForks, like this:

  test: {
    pool: "vmThreads",   
  },

With this option, the following option deps.web.transformCss will be set to true by default, which will resolve the problem

like image 133
MinhTC Avatar answered Oct 28 '25 07:10

MinhTC