|
| 1 | +/// <reference types="cypress" /> |
| 2 | + |
| 3 | +describe('Preview tests panel', () => { |
| 4 | + const storyPath = 'src/components/VitestMocking.story.vue' |
| 5 | + const hmrInsertionMarker = ' // HMR_TEST_INSERTION_POINT' |
| 6 | + let originalStorySource = '' |
| 7 | + |
| 8 | + const getIframeBody = () => cy.get('iframe[data-test-id="preview-iframe"]') |
| 9 | + .its('0.contentDocument.body') |
| 10 | + .should('not.be.empty') |
| 11 | + .then(cy.wrap) |
| 12 | + |
| 13 | + const assertMockedGreeting = () => { |
| 14 | + getIframeBody().find('iframe').should('have.length', 0) |
| 15 | + getIframeBody().contains('Mocked by Vitest for Vitest browser mode', { |
| 16 | + timeout: 20000, |
| 17 | + }) |
| 18 | + getIframeBody().should('not.contain', 'Failed to resolve vitest:mocks:resolveMock in time') |
| 19 | + } |
| 20 | + |
| 21 | + function openVitestStory() { |
| 22 | + cy.get('[data-test-id="story-list-item"]').contains('Vitest Mocking').click() |
| 23 | + cy.location('search').should('include', 'variantId=src-components-vitestmocking-story-vue-0') |
| 24 | + } |
| 25 | + |
| 26 | + function openTestsPanel() { |
| 27 | + cy.get('[data-test-id="story-tests-tab"]:visible').click() |
| 28 | + cy.get('[data-test-id="story-side-panel"]').should('be.visible') |
| 29 | + cy.get('[data-test-id="story-side-panel"]').contains('Loading...').should('not.exist') |
| 30 | + } |
| 31 | + |
| 32 | + function assertCollectedDefinitions(count) { |
| 33 | + cy.get('iframe[data-test-id="preview-iframe"]') |
| 34 | + .its('0.contentWindow.__HST_TEST_DEFINITIONS__') |
| 35 | + .should((definitions) => { |
| 36 | + expect(Array.isArray(definitions)).to.equal(true) |
| 37 | + expect(definitions).to.have.length(count) |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + function assertTestsTabCount(count) { |
| 42 | + cy.get('[data-test-id="story-tests-tab-count"]:visible') |
| 43 | + .should('have.text', `${count}`) |
| 44 | + } |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + cy.readFile(storyPath).then((source) => { |
| 48 | + originalStorySource = source |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + afterEach(() => { |
| 53 | + if (!originalStorySource) { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + cy.writeFile(storyPath, originalStorySource) |
| 58 | + }) |
| 59 | + |
| 60 | + it('collects story tests registered from a single onTest callback', () => { |
| 61 | + cy.viewport(1600, 1000) |
| 62 | + cy.visit('/') |
| 63 | + |
| 64 | + openVitestStory() |
| 65 | + assertCollectedDefinitions(3) |
| 66 | + assertTestsTabCount(3) |
| 67 | + assertMockedGreeting() |
| 68 | + |
| 69 | + openTestsPanel() |
| 70 | + cy.contains('button', 'Run tests').should('be.visible') |
| 71 | + cy.get('[data-test-id="story-test-row"]').should('have.length', 3) |
| 72 | + cy.get('[data-test-id="story-test-row"]').each(($row) => { |
| 73 | + cy.wrap($row).contains('Not run') |
| 74 | + }) |
| 75 | + |
| 76 | + cy.contains('button', 'Run tests').click() |
| 77 | + cy.get('[data-test-id="story-test-row"]').should('have.length', 3) |
| 78 | + cy.contains('[data-test-id="story-test-row"]', 'renders the mocked dependency output').contains('passed') |
| 79 | + cy.contains('[data-test-id="story-test-row"]', 'tracks calls through the mocked module function').contains('passed') |
| 80 | + cy.contains('[data-test-id="story-test-row"]', 'fails').as('failedRow') |
| 81 | + cy.get('@failedRow').contains('failed') |
| 82 | + cy.get('@failedRow').contains('This test is expected to fail') |
| 83 | + }) |
| 84 | + |
| 85 | + it('refreshes mocked story tests after hot updates', () => { |
| 86 | + cy.viewport(1600, 1000) |
| 87 | + cy.visit('/') |
| 88 | + |
| 89 | + openVitestStory() |
| 90 | + openTestsPanel() |
| 91 | + assertCollectedDefinitions(3) |
| 92 | + assertTestsTabCount(3) |
| 93 | + cy.get('[data-test-id="story-test-row"]').should('have.length', 3) |
| 94 | + assertMockedGreeting() |
| 95 | + |
| 96 | + cy.then(() => { |
| 97 | + const updatedStorySource = originalStorySource.replace(hmrInsertionMarker, ` it('updates the tests panel after hot reload', () => { |
| 98 | + expect(canvas.textContent).toContain('Mocked by Vitest for Vitest browser mode') |
| 99 | + }) |
| 100 | +${hmrInsertionMarker}`) |
| 101 | + |
| 102 | + expect(updatedStorySource).not.to.equal(originalStorySource) |
| 103 | + cy.writeFile(storyPath, updatedStorySource) |
| 104 | + }) |
| 105 | + |
| 106 | + assertCollectedDefinitions(4) |
| 107 | + assertTestsTabCount(4) |
| 108 | + cy.get('[data-test-id="story-test-row"]', { |
| 109 | + timeout: 20000, |
| 110 | + }).should('have.length', 4) |
| 111 | + assertMockedGreeting() |
| 112 | + |
| 113 | + cy.writeFile(storyPath, originalStorySource) |
| 114 | + |
| 115 | + assertCollectedDefinitions(3) |
| 116 | + assertTestsTabCount(3) |
| 117 | + cy.get('[data-test-id="story-test-row"]', { |
| 118 | + timeout: 20000, |
| 119 | + }).should('have.length', 3) |
| 120 | + assertMockedGreeting() |
| 121 | + }) |
| 122 | +}) |
0 commit comments