summaryrefslogtreecommitdiff
path: root/packages/astro/test/astro-envs.test.js
blob: 402878da527b69a4626887d1c7aa791eb0fc30a3 (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
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
import * as cheerio from 'cheerio';

describe('Environment Variables', () => {
	/** @type {import('./test-utils').Fixture} */
	let fixture;

	before(async () => {
		fixture = await loadFixture({
			root: './fixtures/astro-envs/',
		});
	});

	describe('Build', () => {
		before(async () => {
			await fixture.build();
		});

		it('builds without throwing', async () => {
			expect(true).to.equal(true);
		});

		it('does render public env and private env', async () => {
			let indexHtml = await fixture.readFile('/index.html');

			expect(indexHtml).to.include('CLUB_33');
			expect(indexHtml).to.include('BLUE_BAYOU');
		});

		it('does render destructured public env and private env', async () => {
			let indexHtml = await fixture.readFile('/destructured/index.html');

			expect(indexHtml).to.include('CLUB_33');
			expect(indexHtml).to.include('BLUE_BAYOU');
		});

		it('does render builtin SITE env', async () => {
			let indexHtml = await fixture.readFile('/index.html');
			expect(indexHtml).to.include('http://example.com');
		});

		it('does render destructured builtin SITE env', async () => {
			let indexHtml = await fixture.readFile('/destructured/index.html');

			expect(indexHtml).to.include('http://example.com');
		});

		it('does render builtin BASE_URL env', async () => {
			let indexHtml = await fixture.readFile('/index.html');
			expect(indexHtml).to.include('/blog');
		});

		it('includes public env in client-side JS', async () => {
			let dirs = await fixture.readdir('/_astro');
			let found = false;

			// Look in all of the .js files to see if the public env is inlined.
			// Testing this way prevents hardcoding expected js files.
			// If we find it in any of them that's good enough to know its working.
			await Promise.all(
				dirs.map(async (path) => {
					if (path.endsWith('.js')) {
						let js = await fixture.readFile(`/_astro/${path}`);
						if (js.includes('BLUE_BAYOU')) {
							found = true;
						}
					}
				})
			);

			expect(found).to.equal(true, 'found the public env variable in the JS build');
		});

		it('does not include private env in client-side JS', async () => {
			let dirs = await fixture.readdir('/');
			let found = false;

			// Look in all of the .js files to see if the public env is inlined.
			// Testing this way prevents hardcoding expected js files.
			// If we find it in any of them that's good enough to know it's NOT working.
			await Promise.all(
				dirs.map(async (path) => {
					if (path.endsWith('.js')) {
						let js = await fixture.readFile(`/${path}`);
						if (js.includes('CLUB_33')) {
							found = true;
						}
					}
				})
			);

			expect(found).to.equal(false, 'found the private env variable in the JS build');
		});
	});

	describe('Development', () => {
		/** @type {import('./test-utils').DevServer} */
		let devServer;
		before(async () => {
			devServer = await fixture.startDevServer();
		});
		after(async () => {
			await devServer.stop();
		});

		it('does render builtin BASE_URL env', async () => {
			let res = await fixture.fetch('/blog/');
			expect(res.status).to.equal(200);
			let indexHtml = await res.text();
			let $ = cheerio.load(indexHtml);
			expect($('#base-url').text()).to.equal('/blog/');
		});

		it('does render destructured builtin SITE env', async () => {
			let res = await fixture.fetch('/blog/destructured/');
			expect(res.status).to.equal(200);
			let indexHtml = await res.text();
			let $ = cheerio.load(indexHtml);
			expect($('#base-url').text()).to.equal('/blog/');
		});
	});
});