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

Supports browser mode for custom provider module #7316

Open
4 tasks done
cenfun opened this issue Jan 21, 2025 · 2 comments
Open
4 tasks done

Supports browser mode for custom provider module #7316

cenfun opened this issue Jan 21, 2025 · 2 comments
Labels
enhancement: pending triage feat: browser Issues and PRs related to the browser runner feat: coverage Issues and PRs related to the coverage feature

Comments

@cenfun
Copy link

cenfun commented Jan 21, 2025

Clear and concise description of the problem

It doesn't automatically switch to browser mode for custom provider module. for example:

export default defineConfig({
    test: {
        include: ['test/*.test.js'],
        coverage: {
            enabled: true,
            include: ['src/**'],
            
            provider: 'custom',
            customProviderModule: 'vitest-monocart-coverage'

        },

        browser: {
            enabled: true,
            headless: true,
            instances: [{
                browser: 'chromium'
            }],
            provider: 'playwright'
        }
    }
});

It works if the browser mode is manually used

...
customProviderModule: 'vitest-monocart-coverage/browser'
...

Related issue: cenfun/vitest-monocart-coverage#8

Suggested solution

For build-in modules 'v8' and 'istanbul', the solution is below

const provider = options.provider

  if (provider === 'v8' || provider === 'istanbul') {
    let builtInModule = CoverageProviderMap[provider]

    if (provider === 'v8' && loader.isBrowser) {
      builtInModule += '/browser'
    }

    const { default: coverageModule } = await loader.executeId(builtInModule)
...

see source

So can we do something similar for custom module?

  let customProviderModule
  
  // do something here?
  let customModule = options.customProviderModule;
  if (loader.isBrowser) {
      customModule += '/browser'
  }

  try {
    customProviderModule = await loader.executeId(customModule)
  }
  catch (error) {
    throw new Error(
      `Failed to load custom CoverageProviderModule from ${options.customProviderModule}`,
      { cause: error },
    )
  }

Possible questions:
1, What if customProviderModule is a file path like ./path-to/my-file.js?
2, What if customProviderModule does not support browser mode?

Alternative

No response

Additional context

If it cannot be supported, should we consider restructuring the custom module architecture, since vitest@3 has just been released and we still have the opportunity?
I personally think we should separate the code for browser mode and Node.js mode. Otherwise, it will lead to very high complexity and is prone to errors.

Validations

@sheremet-va sheremet-va added feat: coverage Issues and PRs related to the coverage feature feat: browser Issues and PRs related to the browser runner labels Jan 23, 2025
@chapmanio
Copy link

I'm interested in this, without the switching we can't (automatically) gather coverage from both sources - and Vitest doesn't allow us to set up separate coverage providers for each workspace project.

@AriPerkkio
Copy link
Member

without the switching we can't (automatically) gather coverage from both sources - and Vitest doesn't allow us to set up separate coverage providers for each workspace project.

Sure you can. Vitest's built-in coverage providers work out-of-the-box - you can have multiple Node and browser projects in you workspace and get coverage report with merged results automatically.

The issue is just about 3rd party coverage provider packages being able to define additional entrypoint for browsers. I'm not yet sure how this could be solved. At the moment they could probably work-around this by having separate bundles for Node and browser, and using dynamic import when loading those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement: pending triage feat: browser Issues and PRs related to the browser runner feat: coverage Issues and PRs related to the coverage feature
Projects
None yet
Development

No branches or pull requests

4 participants