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

describe('Environment Variables', () => {
	let fixture;

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

		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('includes public 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 its working.
		await Promise.all(
			dirs.map(async (path) => {
				if (path.endsWith('.js')) {
					let js = await fixture.readFile(`/${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 its 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');
	});
});