Skip to content

Comments

feat: add "configureVitest" plugin hook#7349

Merged
sheremet-va merged 10 commits intovitest-dev:mainfrom
sheremet-va:feat/hook-plugin
Mar 19, 2025
Merged

feat: add "configureVitest" plugin hook#7349
sheremet-va merged 10 commits intovitest-dev:mainfrom
sheremet-va:feat/hook-plugin

Conversation

@sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented Jan 23, 2025

Description

See discussion: #7104

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify
Copy link

netlify bot commented Feb 10, 2025

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit bd66b9d
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/67d9ce8ec6c6f900080c94d7
😎 Deploy Preview https://deploy-preview-7349--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@sheremet-va sheremet-va marked this pull request as ready for review February 19, 2025 15:39
@sheremet-va sheremet-va added this to the 3.1.0 milestone Feb 19, 2025
@sheremet-va sheremet-va changed the title feat: add "configureVitest" plugin feat: add "configureVitest" plugin hook Mar 6, 2025
Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good, but I'm not familiar with use case. For example, the purpose of force flag doesn't look obvious to me.

If this serves storybook's need, I'm good to proceed with this.

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, great work! Once tests have been added this should be ready to go as is.

I tested this with two real-world use cases that developers building on top of Vitest have asked before:

Check if coverage is enabled via coverage.enabled or --coverage (or resolved by both)

import { defineConfig } from "vitest/config";
import type { Vite, VitestPluginContext } from "vitest/node";

export default defineConfig({
  plugins: [
    {
      name: "vitest:is-coverage-enabled",
      configureVitest(context: VitestPluginContext) {
        console.log({ coverage: context.vitest.config.coverage.enabled });
      },
    } satisfies Vite.Plugin,
  ],
});

Configure coverage.excludes from workspace project's plugins

import { defineConfig } from "vitest/config";
import type { VitestPluginContext } from "vitest/node";

export default defineConfig({
  test: {
    workspace: [
      {
        test: {
          name: "Headless unit tests",
          include: ["test/*.test.ts"],
        },
      },
      {
        test: {
          name: "Stories",
          include: ["src/**.stories.*ts*"],
        },
        plugins: [
          {
            name: "vitest:configure-coverage-for-storybook",
            configureVitest(context: VitestPluginContext) {
              // At this point we can assume `config.coverage` is resolved and defined to non-nullable
              if (context.project.config.coverage.enabled) {
                // No need to check if `coverage.exclude` is array, just push.
                // No need to care about default values like "**/node_modules/**", push doesnt overwrite anything
                context.project.config.coverage.exclude.push("**/*.stories.*");
              }
            },
          },
        ],
      },
    ],
  },
});

@sheremet-va sheremet-va merged commit 20a5d4b into vitest-dev:main Mar 19, 2025
13 checks passed
@sheremet-va sheremet-va deleted the feat/hook-plugin branch March 19, 2025 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants