LondonAppDev
LondonAppDev

Reputation: 9653

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?

Upvotes: 4

Views: 6778

Answers (3)

MinhTC
MinhTC

Reputation: 475

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

Upvotes: 3

违章动物
违章动物

Reputation: 21

vitest: '^0.34.3'

Vitest "deps.inline" is deprecated. If you rely on vite-node directly, use "server.deps.inline" instead. Otherwise, consider using "deps.optimizer.web.include"

deps: {
        optimizer: {
          web: {
            include: ['element-plus']
          }
        }
      }

It's works for me

Upvotes: 1

takosu
takosu

Reputation: 26

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

Upvotes: 1

Related Questions