vitest icon indicating copy to clipboard operation
vitest copied to clipboard

The `coverage.autoUpdate` rewrite strips intended newlines from config files

Open kaspar-p opened this issue 1 month ago • 1 comments

Describe the bug

Using coverage.autoUpdate in a configuration to update the thresholds will strip intended whitespace/newlines from the bottom of configuration files.

This goes against common linting rules like eslint/prettier-prettier that enforce newlines at the end of files, meaning that on-update the build will generally fail CI builds, if linting is enabled there.

The workaround is to ignore the role in the second-to-last line:

import { configDefaults, defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,

    coverage: {
      enabled: true,
      provider: 'istanbul',
      thresholds: {
        autoUpdate: (newThreshold: number): number =>
          newThreshold >= Math.floor(newThreshold) + 0.2 ? Math.floor(newThreshold) : Math.floor(newThreshold) - 1,
        statements: 56,
        branches: 47,
        functions: 58,
        lines: 56,
      },
      reporter: ['json', 'json-summary', 'text', 'cobertura', 'html'],
      include: ['lib/**/*.ts'],
    },

    exclude: [...configDefaults.exclude, '**/dist/**/*'],
  },
  // eslint-disable-next-line prettier/prettier
});

but it'd be nice if only the thresholds part of the file was replaced, preferably just the numbers. In general it's hard to conform to all linters, since something like 56.<many digits>1 may want to wrap to a new line:

lines:
  56.0000000000000000000000000000000000001

so maybe the real solution is to offer a post-update script that could be run to format. Appending to our "test" script to format is ugly and also doesn't work in Monorepos that lint/format from the top-level, not individually in each package.

Reproduction

Configure eslint with the prettier plugin.

Run tests, the coverage doesn't even have to change.

See that the newline is stripped off of vitest.config.ts.

System Info

System:
    OS: Linux 5.10 Amazon Linux 2
    CPU: (32) arm64 unknown
    Memory: 101.18 GB / 123.55 GB
    Container: Yes
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 20.18.0 - ~/.local/share/mise/installs/node/20/bin/node
    npm: 10.8.2 - ~/.local/share/mise/installs/node/20/bin/npm
  npmPackages:
    vitest: ^3 => 3.2.4

Used Package Manager

npm

Validations

kaspar-p avatar Dec 10 '25 17:12 kaspar-p

magicast seems to have bunch of format options. We need to check if using these fixes the issue. https://github.com/unjs/magicast/blob/main/src/format.ts

AriPerkkio avatar Dec 11 '25 07:12 AriPerkkio

I took a closer look at the formatting options exposed by the code generator currently used by Vitest (via Vite/magicast), and it appears that only the following CodeFormatOptions are available:

interface CodeFormatOptions {
  tabWidth?: number;
  useTabs?: boolean;
  wrapColumn?: number;
  quote?: "single" | "double";
  trailingComma?: boolean;
  arrayBracketSpacing?: boolean;
  objectCurlySpacing?: boolean;
  arrowParensAlways?: boolean;
  useSemi?: boolean;
}

From what I can tell, there doesn’t seem to be an option to preserve or explicitly insert a final newline at EOF.

Given this, I was wondering if there might be a couple of possible directions to address this issue:

  1. Updating the magicast dependency, if a newer version provides support for preserving a final newline, though this might have a broader impact on config generation.
  2. Handling the final newline within Vitest itself, for example by wrapping generate() in parseConfigModule and appending a newline when the generated code does not already end with one.

The second approach seems like it could be a minimal and self-contained way to avoid breaking common linting rules, but I’d appreciate feedback on which direction would be preferred.

nami8824 avatar Dec 13 '25 14:12 nami8824

Looking at recast (used by magicast), trailing new lines should already be preserved:

  • https://github.com/benjamn/recast/issues/199

No idea why it doesn't work here. 🤔

AriPerkkio avatar Dec 13 '25 20:12 AriPerkkio

Could this be related to the behavior described here? https://github.com/facebook/jscodeshift/issues/28#issuecomment-126752199

From that discussion, it seems that trailing whitespace (including the final newline at EOF) is preserved only when working with the File AST.

nami8824 avatar Dec 14 '25 00:12 nami8824