|
| 1 | +import { |
| 2 | + isBuildWatch, |
| 3 | + getEl, |
| 4 | + getText, |
| 5 | + editFileAndWaitForBuildWatchComplete, |
| 6 | + hmrCount, |
| 7 | + untilMatches, |
| 8 | + sleep, |
| 9 | + getColor, |
| 10 | + browserLogs, |
| 11 | + e2eServer |
| 12 | +} from '~utils'; |
| 13 | + |
| 14 | +import * as vite from 'vite'; |
| 15 | +// @ts-ignore |
| 16 | +const isRolldownVite = !!vite.rolldownVersion; |
| 17 | + |
| 18 | +describe.runIf(isBuildWatch)('build-watch', () => { |
| 19 | + test('should render App', async () => { |
| 20 | + expect(await getText('#app-header')).toBe('Test-App'); |
| 21 | + }); |
| 22 | + |
| 23 | + test('should render static import', async () => { |
| 24 | + expect(await getText('#static-import .label')).toBe('static-import'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('should render dependency import', async () => { |
| 28 | + expect(await getText('#dependency-import .label')).toBe('dependency-import'); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should render dynamic import', async () => { |
| 32 | + expect(await getEl('#dynamic-import')).toBe(null); |
| 33 | + const dynamicImportButton = await getEl('#button-import-dynamic'); |
| 34 | + expect(dynamicImportButton).toBeDefined(); |
| 35 | + await dynamicImportButton.click(); |
| 36 | + await untilMatches( |
| 37 | + () => getText('#dynamic-import .label'), |
| 38 | + 'dynamic-import', |
| 39 | + 'dynamic import loaded after click' |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + test('should not have failed requests', async () => { |
| 44 | + browserLogs.forEach((msg) => { |
| 45 | + expect(msg).not.toMatch('404'); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + test('should respect transforms', async () => { |
| 50 | + expect(await getText('#js-transform')).toBe('Hello world'); |
| 51 | + expect(await getColor('#css-transform')).toBe('red'); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('edit files', () => { |
| 55 | + const updateHmrTest = editFileAndWaitForBuildWatchComplete.bind( |
| 56 | + null, |
| 57 | + 'src/components/HmrTest.svelte' |
| 58 | + ); |
| 59 | + const updateModuleContext = editFileAndWaitForBuildWatchComplete.bind( |
| 60 | + null, |
| 61 | + 'src/components/partial-hmr/ModuleContext.svelte' |
| 62 | + ); |
| 63 | + const updateApp = editFileAndWaitForBuildWatchComplete.bind(null, 'src/App.svelte'); |
| 64 | + const updateStore = editFileAndWaitForBuildWatchComplete.bind(null, 'src/stores/hmr-stores.js'); |
| 65 | + |
| 66 | + const getWatchErrors = () => |
| 67 | + isRolldownVite |
| 68 | + ? e2eServer.logs.watch.err.filter( |
| 69 | + (m) => |
| 70 | + ![ |
| 71 | + 'Support for rolldown-vite in vite-plugin-svelte is experimental', |
| 72 | + 'See https://github.com/sveltejs/vite-plugin-svelte/issues/1143' |
| 73 | + ].some((s) => m.includes(s)) |
| 74 | + ) |
| 75 | + : e2eServer.logs.watch.err; |
| 76 | + |
| 77 | + test('should have expected initial state', async () => { |
| 78 | + // initial state, both counters 0, both labels red |
| 79 | + expect(await getText('#hmr-test-1 .counter')).toBe('0'); |
| 80 | + expect(await getText('#hmr-test-2 .counter')).toBe('0'); |
| 81 | + expect(await getText('#hmr-test-1 .label')).toBe('hmr-test'); |
| 82 | + expect(await getText('#hmr-test-2 .label')).toBe('hmr-test'); |
| 83 | + expect(await getColor('#hmr-test-1 .label')).toBe('red'); |
| 84 | + expect(await getColor('#hmr-test-2 .label')).toBe('red'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('should have working increment button', async () => { |
| 88 | + // increment counter of one instance to have local state to verify after build updates |
| 89 | + await (await getEl('#hmr-test-1 .increment')).click(); |
| 90 | + await sleep(50); |
| 91 | + |
| 92 | + // counter1 = 1, counter2 = 0 |
| 93 | + expect(await getText('#hmr-test-1 .counter')).toBe('1'); |
| 94 | + expect(await getText('#hmr-test-2 .counter')).toBe('0'); |
| 95 | + }); |
| 96 | + |
| 97 | + test('should apply css changes in HmrTest.svelte', async () => { |
| 98 | + // update style, change label color from red to green |
| 99 | + await updateHmrTest((content) => content.replace('color: red', 'color: green')); |
| 100 | + |
| 101 | + // color should have changed |
| 102 | + expect(await getColor('#hmr-test-1 .label')).toBe('green'); |
| 103 | + expect(await getColor('#hmr-test-2 .label')).toBe('green'); |
| 104 | + expect(getWatchErrors(), 'error log of `build --watch` is not empty').toEqual([]); |
| 105 | + }); |
| 106 | + |
| 107 | + test('should apply js change in HmrTest.svelte ', async () => { |
| 108 | + // update script, change label value |
| 109 | + await updateHmrTest((content) => |
| 110 | + content.replace("const label = 'hmr-test'", "const label = 'hmr-test-updated'") |
| 111 | + ); |
| 112 | + expect(await getText('#hmr-test-1 .label')).toBe('hmr-test-updated'); |
| 113 | + expect(await getText('#hmr-test-2 .label')).toBe('hmr-test-updated'); |
| 114 | + expect(getWatchErrors(), 'error log of `build --watch` is not empty').toEqual([]); |
| 115 | + }); |
| 116 | + |
| 117 | + test('should reset state of external store used by HmrTest.svelte when editing App.svelte', async () => { |
| 118 | + // update App, add a new instance of HmrTest |
| 119 | + await updateApp((content) => |
| 120 | + content.replace( |
| 121 | + '<!-- HMR-TEMPLATE-INJECT -->', |
| 122 | + '<HmrTest id="hmr-test-3"/>\n<!-- HMR-TEMPLATE-INJECT -->' |
| 123 | + ) |
| 124 | + ); |
| 125 | + // counter state is reset |
| 126 | + expect(await getText('#hmr-test-1 .counter')).toBe('0'); |
| 127 | + expect(await getText('#hmr-test-2 .counter')).toBe('0'); |
| 128 | + // a third instance has been added |
| 129 | + expect(await getText('#hmr-test-3 .counter')).toBe('0'); |
| 130 | + expect(getWatchErrors(), 'error log of `build --watch` is not empty').toEqual([]); |
| 131 | + }); |
| 132 | + |
| 133 | + test('should reset state of store when editing hmr-stores.js', async () => { |
| 134 | + // change state |
| 135 | + await (await getEl('#hmr-test-2 .increment')).click(); |
| 136 | + await sleep(50); |
| 137 | + expect(await getText('#hmr-test-2 .counter')).toBe('1'); |
| 138 | + await updateStore((content) => `${content}\n/*trigger change*/\n`); |
| 139 | + // counter state is reset |
| 140 | + expect(await getText('#hmr-test-2 .counter')).toBe('0'); |
| 141 | + expect(getWatchErrors(), 'error log of `build --watch` is not empty').toEqual([]); |
| 142 | + }); |
| 143 | + |
| 144 | + test('should work when editing script context="module"', async () => { |
| 145 | + expect(await getText('#hmr-with-context')).toContain('x=0 y=1 slot=1'); |
| 146 | + expect(await getText('#hmr-without-context')).toContain('x=0 y=1 slot='); |
| 147 | + expect(hmrCount('UsingNamed.svelte'), 'updates for UsingNamed.svelte').toBe(0); |
| 148 | + expect(hmrCount('UsingDefault.svelte'), 'updates for UsingDefault.svelte').toBe(0); |
| 149 | + await updateModuleContext((content) => content.replace('y = 1', 'y = 2')); |
| 150 | + expect(await getText('#hmr-with-context')).toContain('x=0 y=2 slot=2'); |
| 151 | + expect(await getText('#hmr-without-context')).toContain('x=0 y=2 slot='); |
| 152 | + expect(hmrCount('UsingNamed.svelte'), 'updates for UsingNamed.svelte').toBe(0); |
| 153 | + expect(hmrCount('UsingDefault.svelte'), 'updates for UsingDefault.svelte').toBe(0); |
| 154 | + expect(getWatchErrors(), 'error log of `build --watch` is not empty').toEqual([]); |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments