【问题标题】:vitest test coverage does not fail when threshold is not met未达到阈值时 vitest 测试覆盖率不会失败
【发布时间】:2022-11-11 14:34:25
【问题描述】:

如果未达到阈值,我希望我的测试覆盖失败

export default defineConfig({
  plugins: [vue()],
  test: {
    environment: "happy-dom",
    exclude: [...configDefaults.exclude, "**/tests/e2e/*"],
    coverage: {
      reporter: ['text', 'json', 'html'],
      lines: 80,
      functions: 80,
      branches: 80,
      statements: 80,
    }
  },

即使我收到错误消息,它仍然显示通过

File                    | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------------|---------|----------|---------|---------|-------------------
All files               |     100 |    33.33 |     100 |     100 |                   
 components/base/button |     100 |    33.33 |     100 |     100 |                   
  Button.vue            |     100 |    33.33 |     100 |     100 | 36-53             
 config                 |     100 |      100 |     100 |     100 |                   
  index.ts              |     100 |      100 |     100 |     100 |                   
------------------------|---------|----------|---------|---------|-------------------
ERROR: Coverage for branches (33.33%) does not meet global threshold (80%)

 PASS  Waiting for file changes...
       press h to show help, press q to quit

有什么帮助吗?

【问题讨论】:

    标签: javascript typescript vuejs3 vite vitest


    【解决方案1】:

    您正在监视模式下运行测试(等待文件更改...)

    使用此命令在无监视模式下运行:vitest run

    或者,如果您想在手表模式下运行使用:vitest watch

    【讨论】:

    • 是的,当我在没有手表模式的情况下运行时,通过指示器不再存在,但是如果没有达到阈值,有没有办法使测试覆盖率失败?喜欢有故障指示器
    • 我认为您要实现的目标没有多大意义。监视模式适用于开发人员试图修复失败测试的开发。如果在监视模式下任何测试失败时测试都会失败(退出),这意味着只是一个糟糕的 DX。
    【解决方案2】:

    我认为我的作品,我正在分享我的 vite 配置文件,希望它可以帮助你:

    /// <reference types="vitest" />
    import { fileURLToPath, URL } from 'url';
    import { defineConfig, loadEnv } from 'vite';
    import vue from '@vitejs/plugin-vue';
    import type { UserConfig as VitestUserConfigInterface } from 'vitest/config';
    
    const vitestConfig: VitestUserConfigInterface = {
      test: {
        watch: false,
        include: ['**/tests/unit/**/*.spec.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
        globals: true,
        environment: 'jsdom',
        reporters: ['verbose'],
        coverage: {
          include: ['src/**/*'],
          exclude: [
            'src/main.ts',
            'src/plugins/',
            'src/App.vue',
            'src/shims-vue.d.ts',
            'src/router/*',
            'src/stores/*',
            'src/types/*',
            'src/components/icons/*'
          ],
          reporter: ['text', 'json', 'html'],
          all: true,
          lines: 80,
          functions: 80,
          branches: 80,
          statements: 80
        }
      }
    };
    
    export default defineConfig(({ mode }) => {
      // https://github.com/vitejs/vite/issues/1149#issuecomment-857686209
      const env = loadEnv(mode, process.cwd());
      const envWithProcessPrefix = Object.entries(env).reduce(
        (prev, [key, val]) => {
          const [, keyNoVite] = key.split('VITE_');
          return {
            ...prev,
            ['process.env.' + key]: `"${val}"`,
            ['process.env.' + keyNoVite]: `"${val}"`
          };
        },
        {}
      );
    
      return {
        define: envWithProcessPrefix,
        test: vitestConfig.test,
        resolve: {
          alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) }
        },
        plugins: [vue()]
      };
    });
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-29
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      相关资源
      最近更新 更多