aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/cloudflare/test/_test-utils.js
blob: 381c66a8b8b05aa0b73dcf8d94be91fd33d8d96a (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
import { fileURLToPath } from 'node:url';
import { execa } from 'execa';
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';

/**
 * @typedef {{ stop: Promise<void>, port: number }} WranglerCLI
 */

const astroPath = fileURLToPath(new URL('../node_modules/astro/astro.js', import.meta.url));
/** Returns a process running the Astro CLI. */
export function astroCli(cwd, /** @type {string[]} */ ...args) {
	const spawned = execa(astroPath, [...args], {
		env: { ASTRO_TELEMETRY_DISABLED: true },
		cwd: cwd,
	});

	spawned.stdout.setEncoding('utf8');

	return spawned;
}

const wranglerPath = fileURLToPath(
	new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url),
);

/** Returns a process running the Wrangler CLI. */
export function wranglerCli(cwd) {
	const spawned = execa(
		wranglerPath,
		[
			'pages',
			'dev',
			'dist',
			'--ip',
			'127.0.0.1',
			'--port',
			'8788',
			'--compatibility-date',
			new Date().toISOString().slice(0, 10),
			'--log-level',
			'info',
		],
		{
			env: { CI: 1, CF_PAGES: 1 },
			cwd: cwd,
		},
	);

	spawned.stdout.setEncoding('utf8');
	spawned.stderr.setEncoding('utf8');

	return spawned;
}

/**
 * @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
 */
export function loadFixture(inlineConfig) {
	if (!inlineConfig?.root) throw new Error("Must provide { root: './fixtures/...' }");

	// resolve the relative root (i.e. "./fixtures/tailwindcss") to a full filepath
	// without this, the main `loadFixture` helper will resolve relative to `packages/astro/test`
	return baseLoadFixture({
		...inlineConfig,
		root: new URL(inlineConfig.root, import.meta.url).toString(),
	});
}