Mounting Vue components lets you test them in isolation. It helps you check if they work as expected before using them in your app.
Mounting Vue components in Cypress
import { mount } from '@cypress/vue' import MyComponent from './MyComponent.vue' describe('MyComponent', () => { it('renders properly', () => { mount(MyComponent, { props: { /* optional props here */ }, slots: { /* optional slots here */ }, global: { /* optional global config */ } }) // add assertions here }) })
Use mount() to render the Vue component inside Cypress.
You can pass props, slots, and global config to customize the component during testing.
mount(MyButton)
mount(MyButton, { props: { label: 'Click me' } })mount(MyList, { slots: { default: '<li>Item 1</li><li>Item 2</li>' } })This test mounts the HelloWorld component with a message prop. It then checks if the heading shows the correct text.
import { mount } from '@cypress/vue' import HelloWorld from './HelloWorld.vue' describe('HelloWorld Component', () => { it('shows greeting message', () => { mount(HelloWorld, { props: { msg: 'Hello Cypress' } }) cy.get('h1').should('contain.text', 'Hello Cypress') }) })
Always use semantic selectors like cy.get('h1') or data attributes for stable tests.
Mounting components isolates them, so you can test without interference from other parts of your app.
Use Cypress DevTools to inspect mounted components during test runs.
Mount Vue components in Cypress to test them alone.
Pass props and slots to customize the component during tests.
Use assertions to check if the component behaves as expected.
Practice
Solution
Step 1: Understand mounting in testing context
Mounting means loading a component alone to test its behavior without other app parts.Step 2: Identify the purpose of mounting in Cypress
Cypress mounts Vue components to isolate and test them independently.Final Answer:
To test the component in isolation without the full app -> Option DQuick Check:
Mounting = isolated component test [OK]
- Confusing mounting with deployment
- Thinking mounting creates components
- Mixing mounting with styling
MyButton in Cypress?Solution
Step 1: Recall Cypress Vue mounting syntax
The official Cypress command to mount Vue components iscy.mount().Step 2: Check each option's syntax
Onlycy.mount(MyButton)matches the correct Cypress syntax.Final Answer:
cy.mount(MyButton) -> Option AQuick Check:
Correct mount syntax = cy.mount() [OK]
- Swapping method and object order
- Using non-existent commands like cy.component
- Confusing mount with other Cypress commands
cy.mount(MyButton, { props: { label: 'Click me' } })
cy.get('button').should('contain.text', 'Click me')Solution
Step 1: Understand the mounted component props
The component is mounted with a proplabelset to 'Click me'.Step 2: Analyze the assertion
The test checks if thebuttonelement contains the text 'Click me'.Final Answer:
The button contains the text 'Click me' -> Option AQuick Check:
Prop label sets button text = 'Click me' [OK]
- Confusing text content with CSS classes
- Assuming button is disabled without code
- Ignoring the prop passed to component
cy.mount(MyButton, { props: { label: Click me } })
cy.get('button').should('contain.text', 'Click me')Solution
Step 1: Check prop syntax
The prop valueClick meis missing quotes, so it's treated as an undefined variable.Step 2: Verify other parts
Mount syntax and selector are correct; assertion method is valid.Final Answer:
The prop value Click me is not quoted as a string -> Option BQuick Check:
Prop strings must be quoted [OK]
- Forgetting quotes around string props
- Misusing assertion methods
- Using invalid selectors
AlertBox that accepts a slot for custom message content. Which is the correct way to mount it with a slot containing <p>Warning!</p> in Cypress?Solution
Step 1: Understand slot passing in Cypress mount
Slots are passed using theslotsoption with keys for slot names.Step 2: Check each option
Only cy.mount(AlertBox, { slots: { default: 'Warning!
' } }) correctly usesslots: { default: 'syntax.Warning!
' }Final Answer:
cy.mount(AlertBox, { slots: { default: '<p>Warning!</p>' } }) -> Option CQuick Check:
Slots passed via slots object [OK]
- Passing slot content as props
- Using wrong option names like slot or children
- Not wrapping slot content in slots object
Ready to prove it?
Pass the test and this pattern moves unknown
