Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
/frameworkPublic archive

feat(test-utils): allow mounting single component for testing - #5723

Merged
danielroe merged 11 commits into
mainfrom
feat/component-test
Apr 6, 2023
Merged

feat(test-utils): allow mounting single component for testing#5723
danielroe merged 11 commits into
mainfrom
feat/component-test

Conversation

@antfu

@antfuantfu commented Jul 5, 2022

Copy link
Copy Markdown
Member

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

This PR enables a special route /__nuxt_component_test__/ to mount a single component from everywhere while preserving the Nuxt's pipeline and context. Also added $fetchComponent and componentTestUrl util to @nuxt/test-utils to construct the URL easier.

import{createPage,$fetch,$fetchComponent,componentTestUrl}from'@nuxt/test-utils'describe('component testing',()=>{it('should work',async()=>{constcomp1=await$fetchComponent('components/SugarCounter.vue',{count: 42})expect(comp1).toContain('42 x 2 = 84')constcomp2=await$fetchComponent('components/SugarCounter.vue',{count: 100})expect(comp2).toContain('100 x 2 = 200')constpage=awaitcreatePage(componentTestUrl('components/SugarCounter.vue',{count: 42})// ....})})

Caveat

Since it uses dynamic imports from strings, it will only work on dev mode with Vite + viteNode: true

📝 Checklist

  • Basic functionality
  • Mount with user app.vue (with an option?)
  • Add tests with the browser interactions
  • Throw error on non Vite Node env
  • Throw error when the component does not exist
  • Test with Vue Router (should we support it by mocking the router, or we say it would be an anti-pattern to directly use router in components?)
  • Interactive playground (adjust props with UI etc) like Storybook? Could be part of DevTools

@netlify

netlifyBot commented Jul 5, 2022

Copy link
Copy Markdown

Deploy Preview for nuxt3-docs canceled.

NameLink
🔨 Latest commit47b5baa
🔍 Latest deploy loghttps://app.netlify.com/sites/nuxt3-docs/deploys/632be8ee3133d400096aa6c7

@antfu
antfu marked this pull request as draft July 5, 2022 06:06
@antfu
antfu marked this pull request as ready for review July 7, 2022 09:55
@antfu
antfu marked this pull request as draft July 7, 2022 09:55
@antfu

antfu commented Jul 8, 2022

Copy link
Copy Markdown
MemberAuthor

💡 Ideas from @danielroe

  • we import and initialize app.vue to have the side-effects and context but not render it
  • or we use a test-only transform to inject the slot.

@pi0

pi0 commented Jul 8, 2022

Copy link
Copy Markdown
Member

Introducing the side-effects of app.vue is probably not the best idea. It makes implementation more fragile and less consistant with selective-ssr components.

@danielroedanielroe added enhancement New feature or request test-utils labels Jul 11, 2022
@antfu
antfu marked this pull request as ready for review July 15, 2022 08:51
@antfu

Copy link
Copy Markdown
MemberAuthor

If we are on hold for App.vue side-effects, I think the first MVP is ready for review/discussion and then we can iterate in future PRs.

@pi0

pi0 commented Jul 15, 2022

Copy link
Copy Markdown
Member

I'm waiting for a review on this PR for #5689 standardizing universal isolated component rendering API.

App.vue side-effects, I think the first MVP is ready for review/discussion and then we can iterate in future PRs.

Sure. I think generally it is a bad decision but if we want to try can be done in another future PR.

@pi0
pi0force-pushed the main branch 2 times, most recently from 247e18b to c98e5c7CompareSeptember 14, 2022 10:41

@danielroedanielroe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Updated to latest main - wdyt @antfu?

@antfu

antfu commented Feb 4, 2023

Copy link
Copy Markdown
MemberAuthor

LGTM, thanks!

@danielroe
danielroe requested a review from pi0February 7, 2023 16:34
@antfu

Copy link
Copy Markdown
MemberAuthor

With nuxt-vitest, this is probably no longer needed?

@tobiasdiez

Copy link
Copy Markdown
Contributor

Providing a way to render a single component using the nuxt pipeline is also helpful in other scenarios like storybook or cypress integration, see eg nuxt/nuxt#18596 and cypress-io/cypress#23619.

@danielroedanielroe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've moved this to a subpath - @nuxt/test-utils/experimental that will either allow us to iterate more quickly on the remaining TODO items, or possibly take an alternative path (if @antfu prefers) akin to the work on https://github.com/danielroe/nuxt-vitest.

Thank you so much for your work on this.

@danielroe
danielroe merged commit 72ba53e into mainApr 6, 2023
@danielroe
danielroe deleted the feat/component-test branch April 6, 2023 12:07
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

3.xenhancementNew feature or requesttest-utils

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@antfu@pi0@tobiasdiez@danielroe