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
198
199
200
201
202
203
204
|
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { cli, parseCliDevStart, cliServerLogSetup, loadFixture } from './test-utils.js';
import stripAnsi from 'strip-ansi';
import { promises as fs, readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { isIPv4 } from 'node:net';
import { join } from 'node:path';
import { Writable } from 'node:stream';
describe('astro cli', () => {
const cliServerLogSetupWithFixture = (flags, cmd) => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
return cliServerLogSetup(['--root', fileURLToPath(projectRootURL), ...flags], cmd);
};
it('astro', async () => {
const proc = await cli();
assert.equal(proc.exitCode, 0);
});
// Flaky test, in CI it exceeds the timeout most of the times
it.skip(
'astro check --watch reports errors on modified files',
{
timeout: 35000,
},
async () => {
let messageResolve;
const messagePromise = new Promise((resolve) => {
messageResolve = resolve;
});
const oneErrorContent = 'foobar';
/** @type {import('./test-utils').Fixture} */
const fixture = await loadFixture({
root: './fixtures/astro-check-watch/',
});
const logs = [];
const checkServer = await fixture.check({
flags: { watch: true },
logging: {
level: 'info',
dest: new Writable({
objectMode: true,
write(event, _, callback) {
logs.push({ ...event, message: stripAnsi(event.message) });
if (event.message.includes('1 error')) {
messageResolve(logs);
}
callback();
},
}),
},
});
await checkServer.watch();
const pagePath = join(fileURLToPath(fixture.config.root), 'src/pages/index.astro');
const pageContent = readFileSync(pagePath, 'utf-8');
await fs.writeFile(pagePath, oneErrorContent);
const messages = await messagePromise;
await fs.writeFile(pagePath, pageContent);
await checkServer.stop();
const diagnostics = messages.filter(
(m) => m.type === 'diagnostics' && m.message.includes('Result')
);
assert.equal(diagnostics[0].message.includes('0 errors'), true);
assert.equal(diagnostics[1].message.includes('1 error'), true);
}
);
it('astro --version', async () => {
const pkgURL = new URL('../package.json', import.meta.url);
const pkgVersion = await fs.readFile(pkgURL, 'utf8').then((data) => JSON.parse(data).version);
const proc = await cli('--version');
assert.equal(proc.stdout.includes(pkgVersion), true);
});
it(
'astro check no errors',
{
timeout: 35000,
},
async () => {
let proc = undefined;
const projectRootURL = new URL('./fixtures/astro-check-no-errors/', import.meta.url);
try {
proc = await cli('check', '--root', fileURLToPath(projectRootURL));
} catch (err) {}
assert.equal(proc?.stdout.includes('0 errors'), true);
}
);
it(
'astro check has errors',
{
timeout: 35000,
},
async () => {
let stdout = undefined;
const projectRootURL = new URL('./fixtures/astro-check-errors/', import.meta.url);
// When `astro check` finds errors, it returns an error code. As such, we need to wrap this
// in a try/catch because otherwise Mocha will always report this test as a fail
try {
await cli('check', '--root', fileURLToPath(projectRootURL));
} catch (err) {
stdout = err.toString();
}
assert.equal(stdout.includes('1 error'), true);
}
);
it('astro dev welcome', async () => {
const pkgURL = new URL('../package.json', import.meta.url);
const pkgVersion = await fs.readFile(pkgURL, 'utf8').then((data) => JSON.parse(data).version);
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
const proc = cli('dev', '--root', fileURLToPath(projectRootURL));
const { messages } = await parseCliDevStart(proc);
const index = messages[0].includes('[vite]') ? 1 : 0;
assert.equal(messages[index].includes('astro'), true);
assert.equal(messages[index].includes(pkgVersion), true);
assert.equal(messages[index].includes('ready in'), true);
});
['dev', 'preview'].forEach((cmd) => {
const networkLogFlags = [['--host'], ['--host', '0.0.0.0']];
networkLogFlags.forEach(([flag, flagValue]) => {
it(`astro ${cmd} ${flag} ${flagValue ?? ''} - network log`, async () => {
const { local, network } = await cliServerLogSetupWithFixture(
flagValue ? [flag, flagValue] : [flag],
cmd
);
assert.notEqual(local, undefined);
assert.notEqual(network, undefined);
const localURL = new URL(local);
const networkURL = new URL(network);
assert.equal(['localhost', '127.0.0.1'].includes(localURL.hostname), true),
`Expected local URL to be on localhost`;
// Note: our tests run in parallel so this could be 3000+!
assert.equal(Number.parseInt(localURL.port) >= 4321, true, `Expected Port to be >= 4321`);
assert.equal(
networkURL.port,
localURL.port,
`Expected local and network ports to be equal`
);
assert.equal(
isIPv4(networkURL.hostname),
true,
`Expected network URL to respect --host flag`
);
});
});
const hostToExposeFlags = [['', '']];
hostToExposeFlags.forEach(([flag, flagValue]) => {
it(`astro ${cmd} ${flag} ${flagValue} - host to expose`, async () => {
const { local, network } = await cliServerLogSetupWithFixture([flag, flagValue], cmd);
assert.notEqual(local, undefined);
assert.notEqual(network, undefined);
const localURL = new URL(local);
assert.equal(
['localhost', '127.0.0.1'].includes(localURL.hostname),
true,
`Expected local URL to be on localhost`
);
assert.throws(() => new URL(networkURL));
});
});
const noNetworkLogFlags = [
['--host', 'localhost'],
['--host', '127.0.0.1'],
];
noNetworkLogFlags.forEach(([flag, flagValue]) => {
it(`astro ${cmd} ${flag} ${flagValue} - no network log`, async () => {
const { local, network } = await cliServerLogSetupWithFixture([flag, flagValue], cmd);
assert.notEqual(local, undefined);
assert.equal(network, undefined);
const localURL = new URL(local);
assert.equal(
['localhost', '127.0.0.1'].includes(localURL.hostname),
true,
`Expected local URL to be on localhost`
);
});
});
});
});
|