The `coverage.autoUpdate` rewrite strips intended newlines from config files
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
- [x] Follow our Code of Conduct
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue 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 or join our Discord Chat Server.
- [x] The provided reproduction is a minimal reproducible example of the bug.
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
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:
- 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.
-
Handling the final newline within Vitest itself, for example by wrapping
generate()inparseConfigModuleand 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.
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. 🤔
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.