nx
nx copied to clipboard
Coverage option not working on @nx/vite with vitest 1.3.1
Current Behavior
When using https://nx.dev/nx-api/vite/executors/test, is not possible to enabled coverage using cli command --coverag.enabled
which is the new expected way to do it according to vitest docs: https://vitest.dev/config/#coverage
Internally, the vitest executor has been called like a object as coverage: { enabled: true}
which ends up of passing the cli options to vitest as --coverage={"enabled":true}
My Package.json:
...
"test": "nx run-many -t test --coverage.enabled"
...
Output:
With additional flags:
--coverage={"enabled":true}
And no coverage was run.
Expected Behavior
When using vitest
and nx/vite
coverage should be enabled when passing --coverage.enabled
as cli options
GitHub Repo
No response
Steps to Reproduce
- Have a vite.config.ts with coverage disabled per default:
export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['dot'],
coverage: {
reportsDirectory: '../../coverage/',
thresholds: {
100: true
},
exclude: ['**/test', '**/index.ts', '**/*types.ts', '**/constants.ts']
}
}
});
- set up a project to use this vite configuration
"test": {
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"configFile": "some-path/vite.config.mts",
"reportsDirectory": "../../coverage"
}
},
- Run
nx run <project>:test --coverage.enabled
- Coverage should run but it actually not trigerred.
Nx Report
Node : 18.19.0
OS : darwin-arm64
npm : 10.2.3
nx : 18.0.5
@nx/js : 18.0.5
@nx/linter : 18.0.5
@nx/eslint : 18.0.5
@nx/workspace : 18.0.5
@nx/cypress : 18.0.5
@nx/devkit : 18.0.5
@nx/eslint-plugin : 18.0.5
@nx/react : 18.0.5
@nrwl/tao : 18.0.5
@nx/vite : 18.0.5
@nx/web : 18.0.5
@nx/webpack : 18.0.5
typescript : 5.3.3
---------------------------------------
Community plugins:
@jscutlery/semver : 5.1.1
ngx-deploy-npm : 8.0.1
Failure Logs
No response
Package Manager Version
No response
Operating System
- [X] macOS
- [ ] Linux
- [ ] Windows
- [ ] Other (Please specify)
Additional Information
No response
I've noticed you can get coverage adding the --verbose
option - I'm on windows though
Same issue here, running with --coverage
or --coverage=true
no longer works (it did before). Running vitest
directly from the project works perfectly fine with --coverage
This also happens with Vitest 1.4.0 .
After digging into the implementation of Vitest executor, we noticed with @BioPhoton that the resolved options contain the CLI arguments into a separate options
object where --coverage
is correctly interpolated as:
{
//..
options: {
'--': [],
color: true,
watch: false,
configFile: 'path/to/vite.config.ts',
coverage: { enabled: true },
w: false
},
// ...
However to work correctly, the coverage
should be merged in the object's root.
I suppose when knowing the dictionary of available CLI options we could iterate over the properties and try to merge the options parsed by getOptions
.
@AgentEnder Would this be an acceptable fix?
@getlarge It should be, if you're willing to open a PR to do this fix that would be very much appreciated!
I would also say that the approach with root vitest.workspace.ts does not work too, because coverage should be enabled via workspace file but it's not picked by Nx when running via nx run project:test.
Also Vitest does not work well with current Nx setup (eg without separate vitest.config.ts)
@getlarge It should be, if you're willing to open a PR to do this fix that would be very much appreciated!
Yes I will give it a try.
@getlarge It should be, if you're willing to open a PR to do this fix that would be very much appreciated!
Yes I will give it a try.
@Coly010 There are currently no unit tests for the vitest executor implementation. I will start to address that in the first PR.
@Coly010 It looks like part of the issue was already fixed by #22355.
Closing as fixed with: https://github.com/nrwl/nx/pull/22355.
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.