Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code coverage on workspace includes node_modules despite exclusions #7203

Closed
mhuggins opened this issue Jan 9, 2025 · 5 comments
Closed

Comments

@mhuggins
Copy link

mhuggins commented Jan 9, 2025

Describe the bug

I've got a pnpm workspace with packages under apps/* and packages/*. Specifically, one package in each sub-folder has a vitest.config.ts file, containing the following:

import path from "node:path";
import { fileURLToPath } from "node:url";
import {
  coverageConfigDefaults,
  defaultExclude,
  defaultInclude,
  defineConfig,
} from "vitest/config";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default defineConfig({
  test: {
    include: defaultInclude,
    exclude: [...defaultExclude, "**/*.spec.*"],
    coverage: {
      exclude: ["**/node_modules/**", ...coverageConfigDefaults.exclude],
    },
  },
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});

I've tried multiple iterations of defining a vitest.workspace.ts file in my project root.

Most basic approach:

export default ["apps/*", "packages/*"];

Most complex approach:

import { defineWorkspace } from "vitest/config";

const exclude = [
  "**/node_modules/**",
  "**/dist/**",
  "**/cypress/**",
  "**/.{idea,git,cache,output,temp}/**",
  "**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*",
  "**/*.spec.*",
];

export default defineWorkspace([
  {
    extends: "./apps/myapp/vitest.config",
    test: {
      include: ["src/**/*.test.{ts,js}"],
      exclude,
    },
  },
  {
    extends: "./packages/mypackage/vitest.config",
    test: {
      include: ["src/**/*.test.{ts,js}"],
      exclude,
    },
  },
]);

When running vitest run --coverage, the correct limited set of tests are being run. However, the code coverage is a very large dump, including coverage of files found in node_modules within each of the individual package folders.

Am I doing something wrong with my configuration? I would not expect node_modules to be included by default or with it being explicitly excluded.

Reproduction

  1. Create a pnpm workspace with a pnpm-workspace.yaml file:

    packages:
      - "packages/*"
      - "apps/*"
  2. Create a package under each of these folders, and define vitest configs as outlined previously.

  3. Run vitest run --coverage.

System Info

System:
    OS: macOS 15.1.1
    CPU: (12) arm64 Apple M3 Pro
    Memory: 2.87 GB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.7.0 - ~/.nvm/versions/node/v22.7.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v22.7.0/bin/npm
    pnpm: 9.15.1 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 131.0.6778.205
    Safari: 18.1.1
  npmPackages:
    @vitest/coverage-v8: 2.1.3 => 2.1.3 
    vitest: 2.1.3 => 2.1.3 


### Used Package Manager

pnpm

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide/).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
@nickm-oua
Copy link

The workaround I found was to add this setting to the configuration.

    coverage: {
      excludeAfterRemap: true,
    },

@mhuggins
Copy link
Author

excludeAfterRemap: true

@nickm-oua Hmm, I tried this with both v8 & istanbul, but I'm still seeing the same result where files in node_modules are included.

@hi-ogawa
Copy link
Contributor

You probably need a root vitest.config.ts along side vitest.workspace.ts since coverage config cannot be specified for each workspace project individually https://vitest.dev/guide/workspace.html#configuration

Some of the configuration options are not allowed in a project config. Most notably:
coverage: coverage is done for the whole workspace

Copy link

Hello @mhuggins. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with needs reproduction will be closed if they have no activity within 3 days.

@mhuggins
Copy link
Author

Thanks @hi-ogawa, that helped me to get it working! All I needed to do was:

  1. Create a vitest.config.ts in my project root containing the following:

    import { defineConfig } from "vitest/config";
    
    export default defineConfig({
      test: {
        coverage: {
          excludeAfterRemap: true,
        },
      },
    });
  2. Revert my vitest.workspace.ts to what I originally had:

    export default ["apps/*", "packages/*"];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants