Skip to content

Commit 80d448e

Browse files
committed
wip: add tests for dependency watching
1 parent 5695439 commit 80d448e

File tree

6 files changed

+117
-90
lines changed

6 files changed

+117
-90
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import {
2+
isBuild,
3+
getEl,
4+
getText,
5+
editFileAndWaitForHmrComplete,
6+
untilUpdated,
7+
sleep,
8+
getColor
9+
} from '../../testUtils';
10+
11+
test('should render App', async () => {
12+
expect(await getText('h1')).toBe(`I'm blue`);
13+
expect(await getColor('h1')).toBe('blue');
14+
expect(await getText('h2')).toBe(`I'm red`);
15+
expect(await getColor('h2')).toBe('red');
16+
expect(await getText('p')).toBe(`I'm green`);
17+
expect(await getColor('p')).toBe('green');
18+
expect(await getText('span')).toBe(`I'm orangered`);
19+
expect(await getColor('span')).toBe('orangered');
20+
});
21+
22+
test('should not have failed requests', async () => {
23+
browserLogs.forEach((msg) => {
24+
expect(msg).not.toMatch('404');
25+
});
26+
});
27+
28+
if (!isBuild) {
29+
describe('hmr', () => {
30+
test('should apply updates when editing App.svelte', async () => {
31+
expect(await getText('span')).toBe(`I'm orangered`);
32+
await editFileAndWaitForHmrComplete('src/App.svelte', (c) =>
33+
c.replace(`I'm orangered`, `I'm replaced`)
34+
);
35+
expect(await getText('span')).toBe(`I'm replaced`);
36+
expect(await getColor('span')).toBe('orangered');
37+
await editFileAndWaitForHmrComplete(
38+
'src/App.svelte',
39+
(c) => c.replace(`color: orangered`, `color: magenta`),
40+
'/src/App.svelte?svelte&type=style&lang.css'
41+
);
42+
expect(await getColor('span')).toBe('magenta');
43+
});
44+
45+
test('should apply updates when editing MultiFile.html', async () => {
46+
expect(await getText('h1')).toBe(`I'm blue`);
47+
expect(await getText('h2')).toBe(`I'm red`);
48+
await editFileAndWaitForHmrComplete(
49+
'src/lib/multifile/MultiFile.html',
50+
(c) => c.replace(`I'm blue`, `I'm replaced`).replace(`I'm red`, `I'm replaced too`),
51+
'/src/lib/multifile/MultiFile.svelte'
52+
);
53+
expect(await getText('h1')).toBe(`I'm replaced`);
54+
expect(await getText('h2')).toBe(`I'm replaced too`);
55+
});
56+
57+
test('should apply updates when editing MultiFile.scss', async () => {
58+
expect(await getColor('h1')).toBe('blue');
59+
await editFileAndWaitForHmrComplete(
60+
'src/lib/multifile/MultiFile.scss',
61+
(c) => c.replace(`color: blue`, `color: magenta`),
62+
'/src/lib/multifile/MultiFile.svelte?svelte&type=style&lang.css'
63+
);
64+
expect(await getColor('h1')).toBe('magenta');
65+
});
66+
67+
test('should apply updates when editing _someImport.scss', async () => {
68+
expect(await getColor('h2')).toBe('red');
69+
await editFileAndWaitForHmrComplete(
70+
'src/lib/multifile/_someImport.scss',
71+
(c) => c.replace(`color: red`, `color: magenta`),
72+
'/src/lib/multifile/MultiFile.svelte?svelte&type=style&lang.css'
73+
);
74+
expect(await getColor('h2')).toBe('magenta');
75+
});
76+
77+
test('should apply updates when editing MultiFile.ts', async () => {
78+
expect(await getText('p')).toBe(`I'm green`);
79+
await editFileAndWaitForHmrComplete(
80+
'src/lib/multifile/MultiFile.ts',
81+
(c) => c.replace(`'green'`, `'a replaced value'`),
82+
'/src/lib/multifile/MultiFile.svelte'
83+
);
84+
expect(await getText('p')).toBe(`I'm a replaced value`);
85+
});
86+
87+
test('should apply updates when editing someother.css', async () => {
88+
expect(await getColor('p')).toBe('green');
89+
await editFileAndWaitForHmrComplete('src/lib/multifile/someother.css', (c) =>
90+
c.replace(`color: green`, `color: magenta`)
91+
);
92+
expect(await getColor('p')).toBe('magenta');
93+
});
94+
});
95+
}
Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,16 @@
11
<script lang="ts">
22
import logo from './assets/svelte.png';
3-
import Counter from './lib/Counter.svelte';
43
import MultiFile from './lib/multifile/MultiFile.svelte';
54
</script>
65

76
<main>
87
<img src={logo} alt="Svelte Logo" />
9-
<h1>Hello Typescript!</h1>
10-
11-
<Counter />
12-
138
<MultiFile />
9+
<span>I'm orangered</span>
1410
</main>
1511

1612
<style>
17-
:root {
18-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell,
19-
'Open Sans', 'Helvetica Neue', sans-serif;
20-
}
21-
22-
main {
23-
text-align: center;
24-
padding: 1em;
25-
margin: 0 auto;
26-
}
27-
28-
img {
29-
height: 16rem;
30-
width: 16rem;
31-
}
32-
33-
h1 {
34-
color: #ff3e00;
35-
text-transform: uppercase;
36-
font-size: 4rem;
37-
font-weight: 100;
38-
line-height: 1.1;
39-
margin: 2rem auto;
40-
max-width: 14rem;
41-
}
42-
43-
p {
44-
max-width: 14rem;
45-
margin: 1rem auto;
46-
line-height: 1.35;
47-
}
48-
49-
@media (min-width: 480px) {
50-
h1 {
51-
max-width: none;
52-
}
53-
54-
p {
55-
max-width: none;
56-
}
13+
span {
14+
color: orangered;
5715
}
5816
</style>

packages/playground/svelte-preprocess/src/lib/Counter.svelte

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<h1>i'm blue</h1>
2-
<h2>i'm red</h2>
3-
<p>i'm {foo}</p>
1+
<h1>I'm blue</h1>
2+
<h2>I'm red</h2>
3+
<p>I'm {foo}</p>
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { defineConfig } from 'vite';
2-
import svelte from '@sveltejs/vite-plugin-svelte';
1+
const { defineConfig } = require('vite');
2+
const svelte = require('@sveltejs/vite-plugin-svelte');
33

4-
// https://vitejs.dev/config/
5-
export default defineConfig({
6-
plugins: [svelte()]
4+
module.exports = defineConfig(({ command, mode }) => {
5+
const isProduction = mode === 'production';
6+
return {
7+
plugins: [svelte()],
8+
build: {
9+
minify: isProduction
10+
}
11+
};
712
});

packages/playground/testUtils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,16 @@ export async function hmrUpdateComplete(file, timeout) {
140140
});
141141
}
142142

143-
export async function editFileAndWaitForHmrComplete(file, replacer) {
143+
export async function editFileAndWaitForHmrComplete(file, replacer, fileUpdateToWaitFor?) {
144144
const newContent = await editFile(file, replacer);
145+
if (!fileUpdateToWaitFor) {
146+
fileUpdateToWaitFor = file;
147+
}
145148
try {
146-
await hmrUpdateComplete(file, 10000);
149+
await hmrUpdateComplete(fileUpdateToWaitFor, 10000);
147150
} catch (e) {
148151
console.log(`retrying hmr update for ${file}`);
149152
await editFile(file, () => newContent);
150-
await hmrUpdateComplete(file, 5000);
153+
await hmrUpdateComplete(fileUpdateToWaitFor, 5000);
151154
}
152155
}

0 commit comments

Comments
 (0)