summaryrefslogtreecommitdiff
path: root/packages/astro/test/units/dev/restart.test.js
blob: a09bfce98c6f34f96ba48ca71facd39dee4d61f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import * as assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import * as cheerio from 'cheerio';

import {
	createContainerWithAutomaticRestart,
	startContainer,
} from '../../../dist/core/dev/index.js';
import { createFs, createRequestAndResponse, triggerFSEvent } from '../test-utils.js';

const root = new URL('../../fixtures/alias/', import.meta.url);

function isStarted(container) {
	return !!container.viteServer.httpServer?.listening;
}

describe('dev container restarts', () => {
	it('Surfaces config errors on restarts', async () => {
		const fs = createFs(
			{
				'/src/pages/index.astro': `
				<html>
					<head><title>Test</title></head>
					<body>
						<h1>Test</h1>
					</body>
				</html>
			`,
				'/astro.config.mjs': `

				`,
			},
			root,
		);

		const restart = await createContainerWithAutomaticRestart({
			fs,
			inlineConfig: { root: fileURLToPath(root), logLevel: 'silent' },
		});

		try {
			let r = createRequestAndResponse({
				method: 'GET',
				url: '/',
			});
			restart.container.handle(r.req, r.res);
			let html = await r.text();
			const $ = cheerio.load(html);
			assert.equal(r.res.statusCode, 200);
			assert.equal($('h1').length, 1);

			// Create an error
			let restartComplete = restart.restarted();
			fs.writeFileFromRootSync('/astro.config.mjs', 'const foo = bar');

			// Vite watches the real filesystem, so we have to mock this part. It's not so bad.
			restart.container.viteServer.watcher.emit(
				'change',
				fs.getFullyResolvedPath('/astro.config.mjs'),
			);

			// Wait for the restart to finish
			let hmrError = await restartComplete;
			assert.notEqual(typeof hmrError, 'undefined');

			// Do it a second time to make sure we are still watching

			restartComplete = restart.restarted();
			fs.writeFileFromRootSync('/astro.config.mjs', 'const foo = bar2');

			// Vite watches the real filesystem, so we have to mock this part. It's not so bad.
			restart.container.viteServer.watcher.emit(
				'change',
				fs.getFullyResolvedPath('/astro.config.mjs'),
			);

			hmrError = await restartComplete;
			assert.notEqual(typeof hmrError, 'undefined');
		} finally {
			await restart.container.close();
		}
	});

	it('Restarts the container if previously started', async () => {
		const fs = createFs(
			{
				'/src/pages/index.astro': `
				<html>
					<head><title>Test</title></head>
					<body>
						<h1>Test</h1>
					</body>
				</html>
			`,
				'/astro.config.mjs': ``,
			},
			root,
		);

		const restart = await createContainerWithAutomaticRestart({
			fs,
			inlineConfig: { root: fileURLToPath(root), logLevel: 'silent' },
		});
		await startContainer(restart.container);
		assert.equal(isStarted(restart.container), true);

		try {
			// Trigger a change
			let restartComplete = restart.restarted();
			triggerFSEvent(restart.container, fs, '/astro.config.mjs', 'change');
			await restartComplete;

			assert.equal(isStarted(restart.container), true);
		} finally {
			await restart.container.close();
		}
	});

	it('Is able to restart project using Tailwind + astro.config.ts', async () => {
		const troot = new URL('../../fixtures/tailwindcss-ts/', import.meta.url);
		const fs = createFs(
			{
				'/src/pages/index.astro': ``,
				'/astro.config.ts': ``,
			},
			troot,
		);

		const restart = await createContainerWithAutomaticRestart({
			fs,
			inlineConfig: { root: fileURLToPath(root), logLevel: 'silent' },
		});
		await startContainer(restart.container);
		assert.equal(isStarted(restart.container), true);

		try {
			// Trigger a change
			let restartComplete = restart.restarted();
			triggerFSEvent(restart.container, fs, '/astro.config.ts', 'change');
			await restartComplete;

			assert.equal(isStarted(restart.container), true);
		} finally {
			await restart.container.close();
		}
	});

	it('Is able to restart project on package.json changes', async () => {
		const fs = createFs(
			{
				'/src/pages/index.astro': ``,
			},
			root,
		);

		const restart = await createContainerWithAutomaticRestart({
			fs,
			inlineConfig: { root: fileURLToPath(root), logLevel: 'silent' },
		});
		await startContainer(restart.container);
		assert.equal(isStarted(restart.container), true);

		try {
			let restartComplete = restart.restarted();
			fs.writeFileSync('/package.json', `{}`);
			triggerFSEvent(restart.container, fs, '/package.json', 'change');
			await restartComplete;
		} finally {
			await restart.container.close();
		}
	});

	it('Is able to restart on viteServer.restart API call', async () => {
		const fs = createFs(
			{
				'/src/pages/index.astro': ``,
			},
			root,
		);

		const restart = await createContainerWithAutomaticRestart({
			fs,
			inlineConfig: { root: fileURLToPath(root), logLevel: 'silent' },
		});
		await startContainer(restart.container);
		assert.equal(isStarted(restart.container), true);

		try {
			let restartComplete = restart.restarted();
			await restart.container.viteServer.restart();
			await restartComplete;
		} finally {
			await restart.container.close();
		}
	});
});