I am migrating my project from jest to vitest and I wanna exclude certain files and folders for both test and coverage, I am following the docs but "exclude" does not seem working, whenever I run some test, vitest throws an error that is coming from config folder and I don't have any test files there, inside the config folder I have bunch of config files including setupTests.ts and i18n specific config, the error is coming from the i18n.ts file. I am using vite 3 and Below is my vite config file, How else can I exclude files and folders?
Environment:
Edit: So it turns out the issue is in the setupTests.ts file where I am mocking the react-i18next, typescript was throwing an error when I tried to do "const actual = await vi.importActual(''react-i18next''); return {...actual, ...}" ignoring typescript works.
vi.mock('react-i18next', () => ({
...vi.importActual('react-i18next'), // this didn't work
useTranslation: () => [(key: any) => key],
}));
vi.mock('react-i18next', () => {
const acutal = vi.importActual('react-i18next'), // this didn't work either
return {
...actual,
useTranslation: () => [(key: any) => key],
};
});
vi.mock('react-i18next', async () => {
const actual = await vi.importActual('react-i18next'); // this works
return {
// @ts-ignore // have to put this here as typescript was complaining
...actual,
useTranslation: () => [(key: any) => key],
};
});
vite.config.ts
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react(),
tsconfigPaths(),
],
test: {
globals: true,
environment: 'jsdom',
setupFiles: './src/config/setupTests.ts',
css: true,
mockReset: true,
restoreMocks: true,
clearMocks: true,
include: ['./src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
exclude: [
'**/node_modules/**',
'**/dist/**',
'**/cypress/**',
'**/.{idea,git,cache,output,temp}/**',
'./src/config/**',
],
coverage: {
exclude: ['./src/config'],
},
},
}); ```
We can use exclude
attribute with in test
configuration.
vitest configuartion can be modified in vitest.config.ts
file. If we want to exclude shared folder packages, we could add shared/*
to the exclude
section as shown below
import { configDefaults } from 'vitest/config'
export default defineConfig({
plugins: [react(), tsconfigPaths()],
test: {
exclude:[
...configDefaults.exclude,
'shared/*'
]
},
});
Here configDefaults
contains the excluded default folders like node_modules
which is available to use from vitest/config
package. If we omit the configDefaults.exclude
it will run checks in node_modules
folder too.
More details can be found here on official documentation.
Note: I have tested it as of 14th dec, 22
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With