|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 2 | +import { getSupportFileRelativePath, waitUntilUrlReady } from '../src/waitForSupportFile' |
| 3 | + |
| 4 | +describe('waitForSupportFile', () => { |
| 5 | + describe('getSupportFileRelativePath', () => { |
| 6 | + it('builds path matching client logic when devServerPublicPathRoute is set', () => { |
| 7 | + const cypressConfig = { |
| 8 | + projectRoot: '/users/proj', |
| 9 | + supportFile: '/users/proj/cypress/support/component.ts', |
| 10 | + devServerPublicPathRoute: '/__cypress/src', |
| 11 | + platform: 'darwin', |
| 12 | + } as Cypress.PluginConfigOptions |
| 13 | + |
| 14 | + expect(getSupportFileRelativePath(cypressConfig)).toBe('/__cypress/src/cypress/support/component.ts') |
| 15 | + }) |
| 16 | + |
| 17 | + it('returns empty string when supportFile is not set', () => { |
| 18 | + const cypressConfig = { |
| 19 | + projectRoot: '/users/proj', |
| 20 | + supportFile: undefined, |
| 21 | + devServerPublicPathRoute: '/__cypress/src', |
| 22 | + platform: 'darwin', |
| 23 | + } as Cypress.PluginConfigOptions |
| 24 | + |
| 25 | + expect(getSupportFileRelativePath(cypressConfig)).toBe('') |
| 26 | + }) |
| 27 | + |
| 28 | + it('handles win32 paths with backslashes', () => { |
| 29 | + const cypressConfig = { |
| 30 | + projectRoot: 'C:\\users\\proj', |
| 31 | + supportFile: 'C:\\users\\proj\\cypress\\support\\component.ts', |
| 32 | + devServerPublicPathRoute: '/__cypress/src', |
| 33 | + platform: 'win32', |
| 34 | + } as Cypress.PluginConfigOptions |
| 35 | + |
| 36 | + expect(getSupportFileRelativePath(cypressConfig)).toBe('/__cypress/src/cypress/support/component.ts') |
| 37 | + }) |
| 38 | + |
| 39 | + it('uses relative path when devServerPublicPathRoute is empty', () => { |
| 40 | + const cypressConfig = { |
| 41 | + projectRoot: '/users/proj', |
| 42 | + supportFile: '/users/proj/cypress/support/component.ts', |
| 43 | + devServerPublicPathRoute: '', |
| 44 | + platform: 'darwin', |
| 45 | + } as Cypress.PluginConfigOptions |
| 46 | + |
| 47 | + expect(getSupportFileRelativePath(cypressConfig)).toBe('./cypress/support/component.ts') |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + describe('waitUntilUrlReady', () => { |
| 52 | + beforeEach(() => { |
| 53 | + vi.stubGlobal('fetch', vi.fn()) |
| 54 | + }) |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + vi.unstubAllGlobals() |
| 58 | + }) |
| 59 | + |
| 60 | + it('resolves when URL returns 200', async () => { |
| 61 | + const fetchMock = vi.mocked(fetch) |
| 62 | + |
| 63 | + fetchMock.mockResolvedValue({ ok: true } as Response) |
| 64 | + |
| 65 | + await expect(waitUntilUrlReady('http://127.0.0.1:5173/__cypress/src/cypress/support/component.ts')).resolves.toBeUndefined() |
| 66 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 67 | + }) |
| 68 | + |
| 69 | + it('retries until 200 then resolves', async () => { |
| 70 | + const fetchMock = vi.mocked(fetch) |
| 71 | + |
| 72 | + fetchMock |
| 73 | + .mockResolvedValueOnce({ ok: false, status: 503 } as Response) |
| 74 | + .mockResolvedValueOnce({ ok: true } as Response) |
| 75 | + |
| 76 | + await expect( |
| 77 | + waitUntilUrlReady('http://127.0.0.1:5173/ready', { maxAttempts: 3, delayMs: 1 }), |
| 78 | + ).resolves.toBeUndefined() |
| 79 | + |
| 80 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 81 | + }) |
| 82 | + |
| 83 | + it('throws after maxAttempts if URL never returns 2xx', async () => { |
| 84 | + const fetchMock = vi.mocked(fetch) |
| 85 | + |
| 86 | + fetchMock.mockResolvedValue({ ok: false, status: 404 } as Response) |
| 87 | + |
| 88 | + await expect( |
| 89 | + waitUntilUrlReady('http://127.0.0.1:5173/missing', { maxAttempts: 2, delayMs: 1 }), |
| 90 | + ).rejects.toThrow(/did not become ready in time/) |
| 91 | + |
| 92 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
0 commit comments