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

Custom UI results messages #7581

Open
4 tasks done
bombillazo opened this issue Feb 27, 2025 · 5 comments
Open
4 tasks done

Custom UI results messages #7581

bombillazo opened this issue Feb 27, 2025 · 5 comments
Labels
enhancement: pending triage p2-to-be-discussed Enhancement under consideration (priority)

Comments

@bombillazo
Copy link

Clear and concise description of the problem

Hello, we'd like a way to show custom HTML messages in the test results sections in vitest UI. For example, we dynamically generate some artifacts in the tests and would like to reference them with a link from the UI test results.

PS: We love Vitest, especially the UI reporter with static file generation. Thanks for the fantastic work.

Suggested solution

Add some parameter or configuration option to the tests to inject these messages into the latest UI results.

Alternative

No response

Additional context

No response

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Feb 28, 2025

Can you be more specific about the use case? I think it would help if you can provide an example code and ui with (hypothetical) API as a proposal to understand Vitest side requirement better, then we can brush it up as needed.

@bombillazo
Copy link
Author

Hi, thanks for the quick response!

So our use case is that we generate some files based on the tests we run, which are stored locally in the same directory the html UI reporter is being generated. Our goal is to be able to output a message to this window per test:

Image

The HTML would be links to the generated files.

and the test would be something like

  test(
    '-- site is runnning',
    async () => {
      const response = await axios.get(SITE_URL);
      expect(response.status).toBe(200);
      // creates files in HTML UI dir
      await createFiles();
    },
    {
      successMessage: `
<h1>Success!</h1>
<p>You file are found here</p>
<a href="file1.html">File 1</a>
<a href="file2.html">File 2</a>
<a href="file3.html">File 3</a>`,
    }
  );

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Feb 28, 2025

The feature sounds too specific to html reporter, so I'm not sure what's the right direction. One idea came to my mind is that to reuse task.meta feature https://vitest.dev/advanced/metadata.html#task-metadata. Probably it's not possible to patch html output on user land, so we probably need to establish some convention specific to html reporter, so it picks up something like task.meta.__html automatically 🤔

  test(
    '-- site is runnning',
    async (ctx) => {
      ...
      
      // custom message as task.meta
      ctx.task.meta.__htmlSuccessMessage = `
<h1>Success!</h1>
<p>You file are found here</p>
<a href="file1.html">File 1</a>
<a href="file2.html">File 2</a>
<a href="file3.html">File 3</a>
`
    }
  )

@hi-ogawa hi-ogawa moved this to P2 - 2 in Team Board Feb 28, 2025
@hi-ogawa hi-ogawa added the p2-to-be-discussed Enhancement under consideration (priority) label Feb 28, 2025
@bombillazo
Copy link
Author

That could work! using meta would be super easy and will work with JSON reporter which we're already using.

@sheremet-va
Copy link
Member

sheremet-va commented Feb 28, 2025

I've been meaning to add something similar to attachments from Playwright. If they have the same structure (like, title/link/body), we can print them both in CLI and display in HTML reporter.

test(
  '-- site is runnning',
  { attachments: [{ title: 'file 1', link: 'file1.html' }] },
  async () => { 
     // ...
  },
)

Or

test(
  '-- site is runnning',
  async ({ attachments }) => { 
     attachments.add({ title: 'file 1', link: 'file1.html' })
  },
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement: pending triage p2-to-be-discussed Enhancement under consideration (priority)
Projects
Status: P2 - 2
Development

No branches or pull requests

3 participants