aboutsummaryrefslogtreecommitdiff
path: root/packages/astro/test/astro-css-bundling.nodetest.js
blob: 150f9a381924bf19122fdbdc4bb117e76f1d79a2 (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
import * as cheerio from 'cheerio';
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';

// note: the hashes should be deterministic, but updating the file contents will change hashes
// be careful not to test that the HTML simply contains CSS, because it always will! filename and quanity matter here (bundling).
const EXPECTED_CSS = {
	'/index.html': ['/_astro/'], // don’t match hashes, which change based on content
	'/one/index.html': ['/_astro/'],
	'/two/index.html': ['/_astro/'],
};
const UNEXPECTED_CSS = [
	'/src/components/nav.css',
	'../css/typography.css',
	'../css/colors.css',
	'../css/page-index.css',
	'../css/page-one.css',
	'../css/page-two.css',
];

describe('CSS Bundling', function () {
	/** @type {import('./test-utils').Fixture} */
	let fixture;

	describe('defaults', () => {
		before(async () => {
			fixture = await loadFixture({
				root: './fixtures/astro-css-bundling/',
				// test suite was authored when inlineStylesheets defaulted to never
				build: { inlineStylesheets: 'never' },
			});
			await fixture.build({ mode: 'production' });
		});

		it('Bundles CSS', async () => {
			const builtCSS = new Set();

			// for all HTML files…
			for (const [filepath, css] of Object.entries(EXPECTED_CSS)) {
				const html = await fixture.readFile(filepath);
				const $ = cheerio.load(html);

				// test 1: assert new bundled CSS is present
				for (const href of css) {
					const link = $(`link[rel="stylesheet"][href^="${href}"]`);
					assert.equal(link.length >= 1, true);
					const outHref = link.attr('href');
					builtCSS.add(outHref.startsWith('../') ? outHref.slice(2) : outHref);
				}

				// test 2: assert old CSS was removed
				for (const href of UNEXPECTED_CSS) {
					const link = $(`link[rel="stylesheet"][href="${href}"]`);
					assert.equal(link.length, 0);
				}

				// test 3: assert all bundled CSS was built and contains CSS
				for (const url of builtCSS.keys()) {
					const bundledCss = await fixture.readFile(url);
					assert.ok(bundledCss);
				}
			}
		});

		it('there are 4 css files', async () => {
			const dir = await fixture.readdir('/_astro');
			assert.equal(dir.length, 4);
		});

		it('CSS includes hashes', async () => {
			const [firstFound] = await fixture.readdir('/_astro');
			assert.match(firstFound, /[a-z]+\.[\w-]{8}\.css/);
		});
	});

	describe('using custom assetFileNames config', () => {
		before(async () => {
			fixture = await loadFixture({
				root: './fixtures/astro-css-bundling/',

				// test suite was authored when inlineStylesheets defaulted to never
				build: { inlineStylesheets: 'never' },

				vite: {
					build: {
						rollupOptions: {
							output: {
								assetFileNames: 'assets/[name][extname]',
								entryFileNames: '[name].js',
							},
						},
					},
				},
			});
			await fixture.build({ mode: 'production' });
		});

		it('there are 4 css files', async () => {
			const dir = await fixture.readdir('/assets');
			assert.equal(dir.length, 4);
		});

		it('CSS does not include hashes', async () => {
			const [firstFound] = await fixture.readdir('/assets');
			assert.doesNotMatch(firstFound, /[a-z]+\.[\da-z]{8}\.css/);
		});

		it('there are 2 index named CSS files', async () => {
			const dir = await fixture.readdir('/assets');
			const indexNamedFiles = dir.filter((name) => name.startsWith('index'));
			assert.equal(indexNamedFiles.length, 2);
		});
	});
});