Home / File/ svg-deduplication.test.js — astro Source File

svg-deduplication.test.js — astro Source File

Architecture documentation for svg-deduplication.test.js, a javascript file in the astro codebase. 6 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  f014a785_a863_639f_c246_c380d3261320["svg-deduplication.test.js"]
  0a624eac_945e_c9e8_c9de_3feb9de2dd15["test-utils.js"]
  f014a785_a863_639f_c246_c380d3261320 --> 0a624eac_945e_c9e8_c9de_3feb9de2dd15
  dd4f09ce_3fd7_8295_f616_8876cda4555c["loadFixture"]
  f014a785_a863_639f_c246_c380d3261320 --> dd4f09ce_3fd7_8295_f616_8876cda4555c
  e1e2fac7_5a95_7a88_cb1e_0a3b91c4e607["strict"]
  f014a785_a863_639f_c246_c380d3261320 --> e1e2fac7_5a95_7a88_cb1e_0a3b91c4e607
  5d6d1861_a18d_b246_cd94_08889ab7e74c["promises"]
  f014a785_a863_639f_c246_c380d3261320 --> 5d6d1861_a18d_b246_cd94_08889ab7e74c
  6b0635f9_51ea_77aa_767b_7857878e98a6["node:test"]
  f014a785_a863_639f_c246_c380d3261320 --> 6b0635f9_51ea_77aa_767b_7857878e98a6
  deb87372_5629_35f8_9a54_e755a08f776a["cheerio"]
  f014a785_a863_639f_c246_c380d3261320 --> deb87372_5629_35f8_9a54_e755a08f776a
  style f014a785_a863_639f_c246_c380d3261320 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import assert from 'node:assert/strict';
import { readdir } from 'node:fs/promises';
import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

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

	describe('build', () => {
		before(async () => {
			fixture = await loadFixture({
				root: './fixtures/svg-deduplication/',
			});
			await fixture.build();
		});

		it('deduplicates identical SVG files in build output', async () => {
			// Get all SVG files in the build output
			const distDir = new URL('./fixtures/svg-deduplication/dist/', import.meta.url);
			const assetsDir = new URL('./_astro/', distDir);

			let svgFiles = [];
			try {
				const files = await readdir(assetsDir);
				svgFiles = files.filter((file) => file.endsWith('.svg'));
			} catch (_e) {
				// Assets directory might not exist if no SVGs are emitted
			}

			// Should only have 2 unique SVG files (duplicate1/duplicate2 share content, unique is separate)
			assert.equal(
				svgFiles.length,
				2,
				`Expected 2 unique SVG files, found ${svgFiles.length}: ${svgFiles.join(', ')}`,
			);
		});

		it('preserves all SVG URLs in HTML output', async () => {
			const html = await fixture.readFile('/index.html');
			const $ = cheerio.load(html);

			// All three SVG components should render
			const duplicate1Svg = $('#duplicate1 svg');
			const duplicate2Svg = $('#duplicate2 svg');
			const uniqueSvg = $('#unique svg');

			assert.equal(duplicate1Svg.length, 1, 'duplicate1 SVG should render');
			assert.equal(duplicate2Svg.length, 1, 'duplicate2 SVG should render');
			assert.equal(uniqueSvg.length, 1, 'unique SVG should render');

			// Check SVG content is correct
			assert.ok(
				duplicate1Svg.find('circle').length > 0,
				'duplicate1 should contain circle element',
			);
			assert.ok(
				duplicate2Svg.find('circle').length > 0,
				'duplicate2 should contain circle element',
			);
			assert.ok(uniqueSvg.find('rect').length > 0, 'unique should contain rect element');
		});

		it('generates deterministic filenames for identical content', async () => {
			const html = await fixture.readFile('/index.html');

			// Both duplicate SVGs should reference the same underlying file
			// This is verified by the file count test above - if they reference
			// different files, we'd have 3 SVG files instead of 2
			assert.ok(html.includes('<svg'), 'SVGs should be inlined in HTML');
		});
	});

	describe('dev', () => {
		/** @type {import('./test-utils').DevServer} */
		let devServer;

		before(async () => {
			fixture = await loadFixture({
				root: './fixtures/svg-deduplication/',
			});
			devServer = await fixture.startDevServer();
		});

		after(async () => {
			await devServer.stop();
		});

		it('serves SVG components correctly in dev mode', async () => {
			const res = await fixture.fetch('/');
			const html = await res.text();
			const $ = cheerio.load(html);

			// All SVG components should render in dev mode
			const duplicate1Svg = $('#duplicate1 svg');
			const duplicate2Svg = $('#duplicate2 svg');
			const uniqueSvg = $('#unique svg');

			assert.equal(duplicate1Svg.length, 1, 'duplicate1 SVG should render in dev');
			assert.equal(duplicate2Svg.length, 1, 'duplicate2 SVG should render in dev');
			assert.equal(uniqueSvg.length, 1, 'unique SVG should render in dev');
		});
	});
});

Dependencies

Frequently Asked Questions

What does svg-deduplication.test.js do?
svg-deduplication.test.js is a source file in the astro codebase, written in javascript. It belongs to the IntegrationAdapters domain.
What does svg-deduplication.test.js depend on?
svg-deduplication.test.js imports 6 module(s): cheerio, loadFixture, node:test, promises, strict, test-utils.js.
Where is svg-deduplication.test.js in the architecture?
svg-deduplication.test.js is located at packages/astro/test/svg-deduplication.test.js (domain: IntegrationAdapters, directory: packages/astro/test).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free