vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Threshold on a Glob Pattern does not show an error message

Open kanteankit opened this issue 2 years ago • 3 comments

Describe the bug

When a global coverage threshold is set in vitest configuration, an error message is shown like this

ERROR: Coverage for functions (0%) does not meet global threshold (100%)

image

But, when threshold for a particular file is set using the glob-pattern option, there is no such error message.

Reproduction

1.Clone this repo: https://github.com/kanteankit/vitest 2. Run npm install 3. Run npm run coverage

Expected behaviour: An error message should show up which says that the threshold for the file AsAsync.vue is not met. Actual behaviour: There is no error message

In vitest.config.ts, a threshold of 100 is set on components/AsAsync.vue. But, it is not throwing any error.

/// <reference types="vitest" />

import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [
    Vue(),
  ],
  test: {
    globals: true,
    environment: 'jsdom',
    coverage: {
      thresholds: {
        // functions: 100, // This shows an error message
        'components/AsAsync.vue': {
          functions: 100 // This does not show an error message
        }
      }
    }
  },
})

Note: Couldn't provide a Stackblitz because it gives an error saying @vitest/coverage-v8 does not work on Stackblitz. Report will be empty.

System Info

System:
    OS: Linux 5.19 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 5.60 GB / 15.48 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 10.2.5 - ~/.nvm/versions/node/v20.9.0/bin/npm
  Browsers:
    Chrome: 123.0.6312.122
  npmPackages:
    @vitejs/plugin-vue: latest => 5.0.4 
    @vitest/coverage-istanbul: ^1.5.0 => 1.5.0 
    @vitest/coverage-v8: ^1.5.0 => 1.5.0 
    vite: latest => 5.2.8 
    vitest: latest => 1.5.0

Used Package Manager

npm

Validations

kanteankit avatar Apr 15 '24 08:04 kanteankit

Does this work?

    coverage: {
      thresholds: {
        // functions: 100, // This shows an error message
-       'components/AsAsync.vue': {
+       '**/components/AsAsync.vue': {
          functions: 100 // This does not show an error message
        }
      }
    }
  • https://www.digitalocean.com/community/tools/glob?comments=true&glob=%2A%2A%2Fcomponents%2FAsAsync.vue&matches=false&tests=%2Fworkspaces%2Fvitest%2Fcomponents%2FAsAsync.vue
  • https://www.digitalocean.com/community/tools/glob?comments=true&glob=components%2FAsAsync.vue&matches=false&tests=%2Fworkspaces%2Fvitest%2Fcomponents%2FAsAsync.vue

AriPerkkio avatar Apr 15 '24 15:04 AriPerkkio

Does this work?

Yeah. It does. Thanks a lot. But, this is a valid bug, right? I provided an actual path of a file and it did not consider it during coverage.

We were using jest before and these full paths were working there.

We had also added coverage for certain folders like this:

'./src/folder-1': {
      statements: 64,
      branches: 61,
      lines: 64,
      functions: 68
    },

This is also not working in vitest. I think as per @AriPerkkio example, we will have to use **/folder-1/** .

kanteankit avatar Apr 16 '24 06:04 kanteankit

Glob patterns are matched against absolute file paths here:

https://github.com/vitest-dev/vitest/blob/c84113f410d76e15fed3cebd83491f628396b5ab/packages/vitest/src/utils/coverage.ts#L146

Instead we should resolve the files relative to project root and match those against the glob patterns.

AriPerkkio avatar Apr 16 '24 08:04 AriPerkkio