|
| 1 | +import { setTimeout } from 'node:timers/promises' |
| 2 | +import { expect, test } from 'vitest' |
| 3 | +import { editFile, expectWithRetry, isBuild, page } from '~utils' |
| 4 | + |
| 5 | +if (isBuild) { |
| 6 | + test('should render', async () => { |
| 7 | + expect(await page.textContent('h1')).toContain('HMR Full Bundle Mode') |
| 8 | + await expectWithRetry(() => page.textContent('.app')).toBe('hello') |
| 9 | + await expectWithRetry(() => page.textContent('.hmr')).toBe('hello') |
| 10 | + }) |
| 11 | +} else { |
| 12 | + // INITIAL -> BUNDLING -> BUNDLED |
| 13 | + test('show bundling in progress', async () => { |
| 14 | + const reloadPromise = page.waitForEvent('load') |
| 15 | + await expectWithRetry(() => page.textContent('body')).toContain( |
| 16 | + 'Bundling in progress', |
| 17 | + ) |
| 18 | + await reloadPromise // page shown after reload |
| 19 | + await expectWithRetry(() => page.textContent('h1')).toBe( |
| 20 | + 'HMR Full Bundle Mode', |
| 21 | + ) |
| 22 | + await expectWithRetry(() => page.textContent('.app')).toBe('hello') |
| 23 | + }) |
| 24 | + |
| 25 | + // BUNDLED -> GENERATE_HMR_PATCH -> BUNDLING -> BUNDLE_ERROR -> BUNDLING -> BUNDLED |
| 26 | + test('handle bundle error', async () => { |
| 27 | + editFile('main.js', (code) => |
| 28 | + code.replace("text('.app', 'hello')", "text('.app', 'hello'); text("), |
| 29 | + ) |
| 30 | + await expectWithRetry(() => page.isVisible('vite-error-overlay')).toBe(true) |
| 31 | + editFile('main.js', (code) => |
| 32 | + code.replace("text('.app', 'hello'); text(", "text('.app', 'hello')"), |
| 33 | + ) |
| 34 | + await expectWithRetry(() => page.isVisible('vite-error-overlay')).toBe( |
| 35 | + false, |
| 36 | + ) |
| 37 | + await expectWithRetry(() => page.textContent('.app')).toBe('hello') |
| 38 | + }) |
| 39 | + |
| 40 | + // BUNDLED -> GENERATE_HMR_PATCH -> BUNDLING -> BUNDLED |
| 41 | + test('update bundle', async () => { |
| 42 | + editFile('main.js', (code) => |
| 43 | + code.replace("text('.app', 'hello')", "text('.app', 'hello1')"), |
| 44 | + ) |
| 45 | + await expectWithRetry(() => page.textContent('.app')).toBe('hello1') |
| 46 | + |
| 47 | + editFile('main.js', (code) => |
| 48 | + code.replace("text('.app', 'hello1')", "text('.app', 'hello')"), |
| 49 | + ) |
| 50 | + await expectWithRetry(() => page.textContent('.app')).toBe('hello') |
| 51 | + }) |
| 52 | + |
| 53 | + // BUNDLED -> GENERATE_HMR_PATCH -> BUNDLING -> BUNDLING -> BUNDLED |
| 54 | + test('debounce bundle', async () => { |
| 55 | + editFile('main.js', (code) => |
| 56 | + code.replace( |
| 57 | + "text('.app', 'hello')", |
| 58 | + "text('.app', 'hello1')\n" + '// @delay-transform', |
| 59 | + ), |
| 60 | + ) |
| 61 | + await setTimeout(100) |
| 62 | + editFile('main.js', (code) => |
| 63 | + code.replace("text('.app', 'hello1')", "text('.app', 'hello2')"), |
| 64 | + ) |
| 65 | + await expectWithRetry(() => page.textContent('.app')).toBe('hello2') |
| 66 | + |
| 67 | + editFile('main.js', (code) => |
| 68 | + code.replace( |
| 69 | + "text('.app', 'hello2')\n" + '// @delay-transform', |
| 70 | + "text('.app', 'hello')", |
| 71 | + ), |
| 72 | + ) |
| 73 | + await expectWithRetry(() => page.textContent('.app')).toBe('hello') |
| 74 | + }) |
| 75 | + |
| 76 | + // BUNDLED -> GENERATING_HMR_PATCH -> BUNDLED |
| 77 | + test('handle generate hmr patch error', async () => { |
| 78 | + await expectWithRetry(() => page.textContent('.hmr')).toBe('hello') |
| 79 | + editFile('hmr.js', (code) => |
| 80 | + code.replace("const foo = 'hello'", "const foo = 'hello"), |
| 81 | + ) |
| 82 | + await expectWithRetry(() => page.isVisible('vite-error-overlay')).toBe(true) |
| 83 | + |
| 84 | + editFile('hmr.js', (code) => |
| 85 | + code.replace("const foo = 'hello", "const foo = 'hello'"), |
| 86 | + ) |
| 87 | + await expectWithRetry(() => page.isVisible('vite-error-overlay')).toBe( |
| 88 | + false, |
| 89 | + ) |
| 90 | + await expectWithRetry(() => page.textContent('.hmr')).toContain('hello') |
| 91 | + }) |
| 92 | + |
| 93 | + // BUNDLED -> GENERATING_HMR_PATCH -> BUNDLED |
| 94 | + test('generate hmr patch', async () => { |
| 95 | + await expectWithRetry(() => page.textContent('.hmr')).toBe('hello') |
| 96 | + editFile('hmr.js', (code) => |
| 97 | + code.replace("const foo = 'hello'", "const foo = 'hello1'"), |
| 98 | + ) |
| 99 | + await expectWithRetry(() => page.textContent('.hmr')).toBe('hello1') |
| 100 | + |
| 101 | + editFile('hmr.js', (code) => |
| 102 | + code.replace("const foo = 'hello1'", "const foo = 'hello'"), |
| 103 | + ) |
| 104 | + await expectWithRetry(() => page.textContent('.hmr')).toContain('hello') |
| 105 | + }) |
| 106 | + |
| 107 | + // BUNDLED -> GENERATING_HMR_PATCH -> GENERATING_HMR_PATCH -> BUNDLED |
| 108 | + test('continuous generate hmr patch', async () => { |
| 109 | + editFile('hmr.js', (code) => |
| 110 | + code.replace( |
| 111 | + "const foo = 'hello'", |
| 112 | + "const foo = 'hello1'\n" + '// @delay-transform', |
| 113 | + ), |
| 114 | + ) |
| 115 | + await setTimeout(100) |
| 116 | + editFile('hmr.js', (code) => |
| 117 | + code.replace("const foo = 'hello1'", "const foo = 'hello2'"), |
| 118 | + ) |
| 119 | + await expectWithRetry(() => page.textContent('.hmr')).toBe('hello2') |
| 120 | + |
| 121 | + editFile('hmr.js', (code) => |
| 122 | + code.replace( |
| 123 | + "const foo = 'hello2'\n" + '// @delay-transform', |
| 124 | + "const foo = 'hello'", |
| 125 | + ), |
| 126 | + ) |
| 127 | + await expectWithRetry(() => page.textContent('.hmr')).toBe('hello') |
| 128 | + }) |
| 129 | +} |
0 commit comments