Skip to content

Commit 359bd91

Browse files
committed
write vite tests, assert using API calls
We will wait for the correct stdout/stderr output, once we know that we can request the fresh css, we will fetch it and make assertions accordingly. Port is currently hardcoded, maybe we should use a packaage to ensure that we use a free port.
1 parent 5d60989 commit 359bd91

File tree

1 file changed

+87
-85
lines changed

1 file changed

+87
-85
lines changed

integrations/vite/tests/integration.test.js

+87-85
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
require('isomorphic-fetch')
2+
13
let $ = require('../../execute')
24
let { css, html, javascript } = require('../../syntax')
35

4-
let {
5-
readOutputFile,
6-
appendToInputFile,
7-
writeInputFile,
8-
waitForOutputFileCreation,
9-
waitForOutputFileChange,
10-
} = require('../../io')({ output: 'dist', input: '.' })
6+
let { readOutputFile, appendToInputFile, writeInputFile } = require('../../io')({
7+
output: 'dist',
8+
input: '.',
9+
})
10+
11+
let PORT = 1337
12+
13+
async function fetchCSS() {
14+
let response = await fetch(`http://0.0.0.0:${PORT}/index.css`, {
15+
headers: {
16+
Accept: 'text/css',
17+
},
18+
})
19+
return response.text()
20+
}
1121

1222
describe('static build', () => {
1323
it('should be possible to generate tailwind output', async () => {
@@ -33,7 +43,7 @@ describe('static build', () => {
3343
})
3444
})
3545

36-
describe.skip('watcher', () => {
46+
describe('watcher', () => {
3747
test('classes are generated when the html file changes', async () => {
3848
await writeInputFile(
3949
'index.html',
@@ -43,25 +53,23 @@ describe.skip('watcher', () => {
4353
`
4454
)
4555

46-
let runningProcess = $('vite dev', {
56+
let runningProcess = $(`vite --port ${PORT}`, {
4757
env: { TAILWIND_MODE: 'watch' },
4858
})
59+
await runningProcess.onStdout((message) => message.includes('ready in'))
4960

50-
await waitForOutputFileCreation(/index.\w+\.css$/)
51-
52-
expect(await readOutputFile(/index.\w+\.css$/)).toIncludeCss(
61+
expect(await fetchCSS()).toIncludeCss(
5362
css`
5463
.font-bold {
5564
font-weight: 700;
5665
}
5766
`
5867
)
5968

60-
await waitForOutputFileChange(/index.\w+\.css$/, async () => {
61-
await appendToInputFile('index.html', html`<div class="font-normal"></div>`)
62-
})
69+
await appendToInputFile('index.html', html`<div class="font-normal"></div>`)
70+
await runningProcess.onStdout((message) => message.includes('hmr update /index.css'))
6371

64-
expect(await readOutputFile(/index.\w+\.css$/)).toIncludeCss(
72+
expect(await fetchCSS()).toIncludeCss(
6573
css`
6674
.font-bold {
6775
font-weight: 700;
@@ -72,11 +80,10 @@ describe.skip('watcher', () => {
7280
`
7381
)
7482

75-
await waitForOutputFileChange(/index.\w+\.css$/, async () => {
76-
await appendToInputFile('index.html', html`<div class="bg-red-500"></div>`)
77-
})
83+
await appendToInputFile('index.html', html`<div class="bg-red-500"></div>`)
84+
await runningProcess.onStdout((message) => message.includes('hmr update /index.css'))
7885

79-
expect(await readOutputFile(/index.\w+\.css$/)).toIncludeCss(
86+
expect(await fetchCSS()).toIncludeCss(
8087
css`
8188
.bg-red-500 {
8289
--tw-bg-opacity: 1;
@@ -103,13 +110,12 @@ describe.skip('watcher', () => {
103110
`
104111
)
105112

106-
let runningProcess = $('vite build --watch', {
113+
let runningProcess = $(`vite --port ${PORT}`, {
107114
env: { TAILWIND_MODE: 'watch' },
108115
})
116+
await runningProcess.onStdout((message) => message.includes('ready in'))
109117

110-
await waitForOutputFileCreation('main.css')
111-
112-
expect(await readOutputFile('main.css')).toIncludeCss(
118+
expect(await fetchCSS()).toIncludeCss(
113119
css`
114120
.font-bold {
115121
font-weight: 700;
@@ -122,37 +128,36 @@ describe.skip('watcher', () => {
122128
`
123129
)
124130

125-
await waitForOutputFileChange('main.css', async () => {
126-
await writeInputFile(
127-
'tailwind.config.js',
128-
javascript`
129-
module.exports = {
130-
purge: ['./src/index.html'],
131-
mode: 'jit',
132-
darkMode: false, // or 'media' or 'class'
133-
theme: {
134-
extend: {
135-
screens: {
136-
md: '800px'
137-
},
138-
fontWeight: {
139-
bold: 'bold'
140-
}
131+
await writeInputFile(
132+
'tailwind.config.js',
133+
javascript`
134+
module.exports = {
135+
purge: ['./index.html'],
136+
mode: 'jit',
137+
darkMode: false, // or 'media' or 'class'
138+
theme: {
139+
extend: {
140+
screens: {
141+
md: '800px'
141142
},
143+
fontWeight: {
144+
bold: 'bold'
145+
}
142146
},
143-
variants: {
144-
extend: {},
145-
},
146-
corePlugins: {
147-
preflight: false,
148-
},
149-
plugins: [],
150-
}
147+
},
148+
variants: {
149+
extend: {},
150+
},
151+
corePlugins: {
152+
preflight: false,
153+
},
154+
plugins: [],
155+
}
151156
`
152-
)
153-
})
157+
)
158+
await runningProcess.onStdout((message) => message.includes('hmr update /index.css'))
154159

155-
expect(await readOutputFile('main.css')).toIncludeCss(
160+
expect(await fetchCSS()).toIncludeCss(
156161
css`
157162
.font-bold {
158163
font-weight: bold;
@@ -177,38 +182,36 @@ describe.skip('watcher', () => {
177182
`
178183
)
179184

180-
let runningProcess = $('vite watch', {
185+
let runningProcess = $(`vite --port ${PORT}`, {
181186
env: { TAILWIND_MODE: 'watch' },
182187
})
188+
await runningProcess.onStdout((message) => message.includes('ready in'))
183189

184-
await waitForOutputFileCreation('main.css')
185-
186-
expect(await readOutputFile('main.css')).toIncludeCss(
190+
expect(await fetchCSS()).toIncludeCss(
187191
css`
188192
.font-bold {
189193
font-weight: 700;
190194
}
191195
`
192196
)
193197

194-
await waitForOutputFileChange('main.css', async () => {
195-
await writeInputFile(
196-
'index.css',
197-
css`
198-
@tailwind base;
199-
@tailwind components;
200-
@tailwind utilities;
198+
await writeInputFile(
199+
'index.css',
200+
css`
201+
@tailwind base;
202+
@tailwind components;
203+
@tailwind utilities;
201204
202-
@layer components {
203-
.btn {
204-
@apply px-2 py-1 rounded;
205-
}
205+
@layer components {
206+
.btn {
207+
@apply px-2 py-1 rounded;
206208
}
207-
`
208-
)
209-
})
209+
}
210+
`
211+
)
212+
await runningProcess.onStdout((message) => message.includes('hmr update /index.css'))
210213

211-
expect(await readOutputFile('main.css')).toIncludeCss(
214+
expect(await fetchCSS()).toIncludeCss(
212215
css`
213216
.btn {
214217
border-radius: 0.25rem;
@@ -223,24 +226,23 @@ describe.skip('watcher', () => {
223226
`
224227
)
225228

226-
await waitForOutputFileChange('main.css', async () => {
227-
await writeInputFile(
228-
'index.css',
229-
css`
230-
@tailwind base;
231-
@tailwind components;
232-
@tailwind utilities;
229+
await writeInputFile(
230+
'index.css',
231+
css`
232+
@tailwind base;
233+
@tailwind components;
234+
@tailwind utilities;
233235
234-
@layer components {
235-
.btn {
236-
@apply px-2 py-1 rounded bg-red-500;
237-
}
236+
@layer components {
237+
.btn {
238+
@apply px-2 py-1 rounded bg-red-500;
238239
}
239-
`
240-
)
241-
})
240+
}
241+
`
242+
)
243+
await runningProcess.onStdout((message) => message.includes('hmr update /index.css'))
242244

243-
expect(await readOutputFile('main.css')).toIncludeCss(
245+
expect(await fetchCSS()).toIncludeCss(
244246
css`
245247
.btn {
246248
border-radius: 0.25rem;

0 commit comments

Comments
 (0)