Skip to content

feat: annotation API #7953

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

Merged
merged 60 commits into from
May 30, 2025
Merged

Conversation

sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented May 9, 2025

Description

This PR adds a new annotations field to the TestCase and an annotate method attached to the TestContext. In the reporter, you can get annotations from testCase.annotations() or in onTestCaseAnnotate event. onTestCaseAnnotate is asynchronous and doesn't block test execution.

interface TestCaseAnnotation {
  message: string
  type: string
  location?: {
    line: number
    column: number
  }
  attachment?: {
    contentType?: string
    path?: string
    body?: string | Uint8Array
  }
}

// extend https://vitest.dev/advanced/api/test-case
interface TestCase {
  annotations(): TestCaseAnnotation[]
}

interface Reporter {
  onTestCaseAnnotate(testCase: TestCase, annotation: TestCaseAnnotation): void
}

Reporters display these annotations differently:

  • verbose reporter in non-tty mode prints annotations after every test immediately
  • default reporter prints them if there was an error in the test
  • tap reporter prints them in # comment
  • junit prints them inside properties/attachment
  • github-actions shows the annotation simillar to errors
  • ui reporter prints them in a "Report" tab and inside the test file (inlined)
  • vscode extension will also show these annotations inline

Annotations can also have attachments that will be shown in UI. These attachments are stored in .vitest-attachments folder.

To add an annotation, you call ctx.annotate method:

test('name', ({ annotate }) => {
  // path is relative to the root of the project
  await annotate('hello, world!', 'info', { path: './som-png.png' })
  await annotate('simple message')
  await annotate('this branch shouldn\'t be called, need investigation', 'warning')
})

UI example (WIP):

Screenshot 2025-05-15 at 16 39 18

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

Copy link

netlify bot commented May 9, 2025

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit 503375c
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/68382f018c37a700085d5a76
😎 Deploy Preview https://deploy-preview-7953--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 project configuration.

@sheremet-va sheremet-va marked this pull request as ready for review May 23, 2025 14:51
@sheremet-va
Copy link
Member Author

sheremet-va commented May 23, 2025

For now, UI only shows annotations in test files. In the future we will add a separate UI tab for everything related to the test itself (this is out of scope of 3.2).

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.

Overall looks good! Didn't check tests yet.

@sheremet-va sheremet-va merged commit b03f209 into vitest-dev:main May 30, 2025
13 checks passed
@sheremet-va sheremet-va deleted the feat/anotate-api branch May 30, 2025 16:56
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.

2 participants