Home / File/ test.ts — svelte Source File

test.ts — svelte Source File

Architecture documentation for test.ts, a typescript file in the svelte codebase. 10 imports, 91 dependents.

File typescript BuildSystem QualityControl 10 imports 91 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  56007373_5cb4_276b_8066_2e61ad5747d2["test.ts"]
  e97e8c41_1b06_4e9a_29f3_64dbb37dee3c["helpers.js"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> e97e8c41_1b06_4e9a_29f3_64dbb37dee3c
  40a894df_04e7_906d_8c12_9e2e7b588e57["compile_directory"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> 40a894df_04e7_906d_8c12_9e2e7b588e57
  2413c72d_083d_061e_c047_c903addfc06e["try_read_file"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> 2413c72d_083d_061e_c047_c903addfc06e
  7e13368c_af2f_099c_e7e1_de404629548f["html_equal.js"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> 7e13368c_af2f_099c_e7e1_de404629548f
  3f18e7a4_4010_5e33_35ee_c53c8401ca03["assert_html_equal"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> 3f18e7a4_4010_5e33_35ee_c53c8401ca03
  b2df78f1_f7aa_6fef_0a5c_4dcf959880ff["../suite.js"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> b2df78f1_f7aa_6fef_0a5c_4dcf959880ff
  f596e027_a951_36c9_7695_83acc4f0d6b9["node:fs"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> f596e027_a951_36c9_7695_83acc4f0d6b9
  b63ddb92_634c_990b_eb1b_0bad8a4d434e["vitest"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> b63ddb92_634c_990b_eb1b_0bad8a4d434e
  4ead6623_c53e_ab40_5690_64903d5addf1["svelte"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> 4ead6623_c53e_ab40_5690_64903d5addf1
  c696582a_9f3f_d301_2824_cb3de66aa770["#compiler"]
  56007373_5cb4_276b_8066_2e61ad5747d2 --> c696582a_9f3f_d301_2824_cb3de66aa770
  786ceeaa_a7ca_08cf_66b0_e21eab489090["_config.js"]
  786ceeaa_a7ca_08cf_66b0_e21eab489090 --> 56007373_5cb4_276b_8066_2e61ad5747d2
  fe6441be_d632_b37f_71b3_b3a5a1ba0dc3["_config.js"]
  fe6441be_d632_b37f_71b3_b3a5a1ba0dc3 --> 56007373_5cb4_276b_8066_2e61ad5747d2
  38a339e8_de43_de21_a3e2_24d6a4d705c2["_config.js"]
  38a339e8_de43_de21_a3e2_24d6a4d705c2 --> 56007373_5cb4_276b_8066_2e61ad5747d2
  1979b4df_b888_e7a7_5c5e_8bd7a1daf89f["_config.js"]
  1979b4df_b888_e7a7_5c5e_8bd7a1daf89f --> 56007373_5cb4_276b_8066_2e61ad5747d2
  style 56007373_5cb4_276b_8066_2e61ad5747d2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

// @vitest-environment jsdom

import * as fs from 'node:fs';
import { assert } from 'vitest';
import { compile_directory, try_read_file } from '../helpers.js';
import { assert_html_equal } from '../html_equal.js';
import { flushSync, mount, unmount } from 'svelte';
import { suite, type BaseTest } from '../suite.js';
import type { CompileOptions, Warning } from '#compiler';

function normalize_warning(warning: Warning) {
	delete warning.filename;
	delete warning.position;
	delete warning.frame;

	// Remove the "https://svelte.dev/e/..." link at the end
	const lines = warning.message.split('\n');
	if (lines.at(-1)?.startsWith('https://svelte.dev/e/')) {
		lines.pop();
	}
	warning.message = lines.join('\n');

	return warning;
}

function load_warnings(path: string) {
	if (!fs.existsSync(path)) {
		return [];
	}
	return JSON.parse(fs.readFileSync(path, 'utf-8')).map(normalize_warning);
}

interface CssTest extends BaseTest {
	compileOptions?: Partial<CompileOptions>;
	warnings?: Warning[];
	props?: Record<string, any>;
	hasGlobal?: boolean;
}

/**
 * Remove the "https://svelte.dev/e/..." link
 */
function strip_link(message: string) {
	return message.slice(0, message.lastIndexOf('\n'));
}

const { test, run } = suite<CssTest>(async (config, cwd) => {
	await compile_directory(cwd, 'client', { cssHash: () => 'svelte-xyz', ...config.compileOptions });
	await compile_directory(cwd, 'server', { cssHash: () => 'svelte-xyz', ...config.compileOptions });

	const expected = {
		html: try_read_file(`${cwd}/expected.html`),
		css: try_read_file(`${cwd}/expected.css`)
	};

	// we do this here, rather than in the expected.html !== null
	// block, to verify that valid code was generated
	const ClientComponent = (await import(`${cwd}/_output/client/input.svelte.js`)).default;
	const ServerComponent = (await import(`${cwd}/_output/server/input.svelte.js`)).default;

	// verify that the right elements have scoping selectors (do this first to ensure all actual files are written to disk)
	if (expected.html !== null) {
		const target = window.document.createElement('main');

		const component = mount(ClientComponent, { props: config.props ?? {}, target });
		flushSync();

		const html = target.innerHTML;

		fs.writeFileSync(`${cwd}/_output/rendered.html`, html);

		assert_html_equal(html, expected.html);

		unmount(component);
		window.document.head.innerHTML = ''; // remove added styles

		// TODO enable SSR tests
		// const actual_ssr = ServerComponent.render(config.props).html;
		// assert_html_equal(actual_ssr, expected.html);
	}

	if (config.hasGlobal !== undefined) {
		const metadata = JSON.parse(
			fs.readFileSync(`${cwd}/_output/client/input.svelte.css.json`, 'utf-8')
		);

		assert.equal(metadata.hasGlobal, config.hasGlobal);
	}

	const dom_css = fs.readFileSync(`${cwd}/_output/client/input.svelte.css`, 'utf-8').trim();
	const ssr_css = fs.readFileSync(`${cwd}/_output/server/input.svelte.css`, 'utf-8').trim();

	assert.equal(dom_css, ssr_css);

	const dom_warnings = load_warnings(`${cwd}/_output/client/input.svelte.warnings.json`);
	const ssr_warnings = load_warnings(`${cwd}/_output/server/input.svelte.warnings.json`);
	const expected_warnings = (config.warnings || []).map(normalize_warning);
	assert.deepEqual(dom_warnings, ssr_warnings);
	assert.deepEqual(dom_warnings.map(normalize_warning), expected_warnings);

	assert.equal(dom_css.trim().replace(/\r\n/g, '\n'), (expected.css ?? '').trim());
});

export { test };

await run(__dirname);

Domain

Subdomains

Types

Dependencies

Imported By

Frequently Asked Questions

What does test.ts do?
test.ts is a source file in the svelte codebase, written in typescript. It belongs to the BuildSystem domain, QualityControl subdomain.
What functions are defined in test.ts?
test.ts defines 3 function(s): load_warnings, normalize_warning, strip_link.
What does test.ts depend on?
test.ts imports 10 module(s): #compiler, ../suite.js, assert_html_equal, compile_directory, helpers.js, html_equal.js, node:fs, svelte, and 2 more.
What files import test.ts?
test.ts is imported by 91 file(s): _config.js, _config.js, _config.js, _config.js, _config.js, _config.js, _config.js, _config.js, and 83 more.
Where is test.ts in the architecture?
test.ts is located at packages/svelte/tests/css/test.ts (domain: BuildSystem, subdomain: QualityControl, directory: packages/svelte/tests/css).

Analyze Your Own Codebase

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

Try Supermodel Free