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

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

	before(async () => {
		fixture = await loadFixture({ projectRoot: './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('includes public env in client-side JS', async () => {
		let dirs = await fixture.readdir('/assets');
		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(`/assets/${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('/assets');
		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(`/assets/${path}`);
					if (js.includes('CLUB_33')) {
						found = true;
					}
				}
			})
		);

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