j.d*_*doe 17 reactjs react-testing-library vite vitest
我正在将我的项目从 jest 迁移到 vitest,我想排除某些文件和文件夹以进行测试和覆盖,我正在遵循文档,但“排除”似乎不起作用,每当我运行一些测试时,vitest 都会抛出一个即将到来的错误从 config 文件夹中,我那里没有任何测试文件,在 config 文件夹中我有一堆配置文件,包括 setupTests.ts 和 i18n 特定配置,错误来自 i18n.ts 文件。我正在使用 vite 3,下面是我的 vite 配置文件,我还能如何排除文件和文件夹?
环境:
编辑:所以事实证明问题出在 setupTests.ts 文件中,我在其中嘲笑react-i18next,当我尝试执行“const effective = wait vi.importActual(''react-i18next''”时,打字稿抛出错误); return {...actual, ...}" 忽略打字稿作品。
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],
};
});
Run Code Online (Sandbox Code Playgroud)
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'],
},
},
}); ```
Run Code Online (Sandbox Code Playgroud)
Kev*_*vin 17
我们可以在配置exclude中使用属性test。
vitest配置可以在文件中修改vitest.config.ts。如果我们想排除共享文件夹包,我们可以添加shared/*到该exclude部分,如下所示
import { configDefaults } from 'vitest/config'
export default defineConfig({
plugins: [react(), tsconfigPaths()],
test: {
exclude:[
...configDefaults.exclude,
'shared/*'
]
},
});
Run Code Online (Sandbox Code Playgroud)
这里configDefaults包含排除的默认文件夹,例如node_modules可以从vitest/config包中使用的文件夹。如果我们省略它,configDefaults.exclude它也会在文件夹中运行检查node_modules。
更多详细信息可以在官方文档中找到。
注:我已于 12 月 14 日、22 日测试过
小智 6
I managed to remove test folder from tests this way.
import { configDefaults, defineConfig } from 'vitest/config'
import path from 'path'
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@test': path.resolve(__dirname, './test'),
},
},
test: {
exclude: [...configDefaults.exclude, '**/test/**']
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23396 次 |
| 最近记录: |