Home / File/ test-utils.js — astro Source File

test-utils.js — astro Source File

Architecture documentation for test-utils.js, a javascript file in the astro codebase. 5 imports, 6 dependents.

File javascript CoreAstro RoutingSystem 5 imports 6 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  9c9ba75b_c6f1_7d10_d247_9a1ccac73501["test-utils.js"]
  91984440_9130_eca1_a9c3_0d4639b25a06["../dist/core/cli/index.js"]
  9c9ba75b_c6f1_7d10_d247_9a1ccac73501 --> 91984440_9130_eca1_a9c3_0d4639b25a06
  9f5f1eb4_1f5d_2ac2_d490_983d23d10d72["../dist/core/load-file.js"]
  9c9ba75b_c6f1_7d10_d247_9a1ccac73501 --> 9f5f1eb4_1f5d_2ac2_d490_983d23d10d72
  1f8b0286_f980_4274_cf61_d97375ae3dd5["../dist/core/queries.js"]
  9c9ba75b_c6f1_7d10_d247_9a1ccac73501 --> 1f8b0286_f980_4274_cf61_d97375ae3dd5
  5d6d1861_a18d_b246_cd94_08889ab7e74c["promises"]
  9c9ba75b_c6f1_7d10_d247_9a1ccac73501 --> 5d6d1861_a18d_b246_cd94_08889ab7e74c
  91decf0f_8a76_7391_4c56_d2f49dbb7487["client"]
  9c9ba75b_c6f1_7d10_d247_9a1ccac73501 --> 91decf0f_8a76_7391_4c56_d2f49dbb7487
  4e9666a7_f271_c118_7dff_83ee28e7f1da["basics.test.js"]
  4e9666a7_f271_c118_7dff_83ee28e7f1da --> 9c9ba75b_c6f1_7d10_d247_9a1ccac73501
  d19ceb64_8b38_e216_dc0d_56b5314d51a3["error-handling.test.js"]
  d19ceb64_8b38_e216_dc0d_56b5314d51a3 --> 9c9ba75b_c6f1_7d10_d247_9a1ccac73501
  2eb106f3_4abe_807d_d2de_6584cf708016["libsql-remote.test.js"]
  2eb106f3_4abe_807d_d2de_6584cf708016 --> 9c9ba75b_c6f1_7d10_d247_9a1ccac73501
  d5868422_7331_9e22_c764_fe305beddf07["ssr-no-apptoken.test.js"]
  d5868422_7331_9e22_c764_fe305beddf07 --> 9c9ba75b_c6f1_7d10_d247_9a1ccac73501
  a5b613e9_78e8_fc21_c2b4_ba7db3f91d06["static-remote.test.js"]
  a5b613e9_78e8_fc21_c2b4_ba7db3f91d06 --> 9c9ba75b_c6f1_7d10_d247_9a1ccac73501
  4e0ebb42_f09f_00ef_16a3_139ff5e093e5["remote-info.test.js"]
  4e0ebb42_f09f_00ef_16a3_139ff5e093e5 --> 9c9ba75b_c6f1_7d10_d247_9a1ccac73501
  style 9c9ba75b_c6f1_7d10_d247_9a1ccac73501 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { mkdir, unlink } from 'node:fs/promises';
import { createClient } from '@libsql/client';
import { cli } from '../dist/core/cli/index.js';
import { resolveDbConfig } from '../dist/core/load-file.js';
import { getCreateIndexQueries, getCreateTableQuery } from '../dist/core/queries.js';

const isWindows = process.platform === 'win32';

/**
 * @param {import('astro').AstroConfig} astroConfig
 */
export async function setupRemoteDb(astroConfig, options = {}) {
	const url = isWindows
		? new URL(`./.astro/${Date.now()}.db`, astroConfig.root)
		: new URL(`./${Date.now()}.db`, astroConfig.root);
	const token = 'foo';
	process.env.ASTRO_DB_REMOTE_URL = url.toString();
	if (!options.useDbAppTokenFlag) {
		process.env.ASTRO_DB_APP_TOKEN = token;
	}
	process.env.ASTRO_INTERNAL_TEST_REMOTE = true;

	if (isWindows) {
		await mkdir(new URL('.', url), { recursive: true });
	}

	const dbClient = createClient({
		url,
		authToken: token,
	});

	const { dbConfig } = await resolveDbConfig(astroConfig);
	const setupQueries = [];
	for (const [name, table] of Object.entries(dbConfig?.tables ?? {})) {
		const createQuery = getCreateTableQuery(name, table);
		const indexQueries = getCreateIndexQueries(name, table);
		setupQueries.push(createQuery, ...indexQueries);
	}

	for (const sql of setupQueries) {
		await dbClient.execute({
			sql,
			args: [],
		});
	}

	await cli({
		config: astroConfig,
		flags: {
			_: [undefined, 'astro', 'db', 'execute', 'db/seed.ts'],
			remote: true,
			...(options.useDbAppTokenFlag ? { dbAppToken: token } : {}),
		},
	});

	return {
		async stop() {
			delete process.env.ASTRO_DB_REMOTE_URL;
			delete process.env.ASTRO_DB_APP_TOKEN;
			delete process.env.ASTRO_INTERNAL_TEST_REMOTE;
			dbClient.close();
			if (!isWindows) {
				await unlink(url);
			}
		},
	};
}

export async function initializeRemoteDb(astroConfig) {
	await cli({
		config: astroConfig,
		flags: {
			_: [undefined, 'astro', 'db', 'push'],
			remote: true,
		},
	});
	await cli({
		config: astroConfig,
		flags: {
			_: [undefined, 'astro', 'db', 'execute', 'db/seed.ts'],
			remote: true,
		},
	});
}

/**
 * Clears the environment variables related to Astro DB.
 */
export function clearEnvironment() {
	const keys = Array.from(Object.keys(process.env));
	for (const key of keys) {
		if (key.startsWith('ASTRO_DB_')) {
			delete process.env[key];
		}
	}
}

Domain

Subdomains

Dependencies

  • ../dist/core/cli/index.js
  • ../dist/core/load-file.js
  • ../dist/core/queries.js
  • client
  • promises

Frequently Asked Questions

What does test-utils.js do?
test-utils.js is a source file in the astro codebase, written in javascript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in test-utils.js?
test-utils.js defines 3 function(s): clearEnvironment, initializeRemoteDb, setupRemoteDb.
What does test-utils.js depend on?
test-utils.js imports 5 module(s): ../dist/core/cli/index.js, ../dist/core/load-file.js, ../dist/core/queries.js, client, promises.
What files import test-utils.js?
test-utils.js is imported by 6 file(s): basics.test.js, error-handling.test.js, libsql-remote.test.js, remote-info.test.js, ssr-no-apptoken.test.js, static-remote.test.js.
Where is test-utils.js in the architecture?
test-utils.js is located at packages/db/test/test-utils.js (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/db/test).

Analyze Your Own Codebase

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

Try Supermodel Free