nx icon indicating copy to clipboard operation
nx copied to clipboard

Coverage option not working on @nx/vite with vitest 1.3.1

Open daum1820 opened this issue 1 year ago • 3 comments

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

  1. 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']
        }
    }
});

  1. 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"
            }
        },
  1. Run nx run <project>:test --coverage.enabled
  2. 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

daum1820 avatar Feb 26 '24 12:02 daum1820

I've noticed you can get coverage adding the --verbose option - I'm on windows though

Phoscur avatar Feb 28 '24 19:02 Phoscur

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

JanPretzel avatar Mar 05 '24 16:03 JanPretzel

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 avatar Apr 30 '24 09:04 getlarge

@getlarge It should be, if you're willing to open a PR to do this fix that would be very much appreciated!

Coly010 avatar May 10 '24 11:05 Coly010

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)

Lonli-Lokli avatar May 10 '24 12:05 Lonli-Lokli

@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 avatar May 12 '24 06:05 getlarge

@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.

getlarge avatar May 12 '24 12:05 getlarge

@Coly010 It looks like part of the issue was already fixed by #22355.

getlarge avatar May 14 '24 14:05 getlarge

Closing as fixed with: https://github.com/nrwl/nx/pull/22355.

Coly010 avatar May 17 '24 10:05 Coly010

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.

github-actions[bot] avatar Jun 17 '24 00:06 github-actions[bot]