Home / File/ test.js — astro Source File

test.js — astro Source File

Architecture documentation for test.js, a javascript file in the astro codebase. 7 imports, 0 dependents.

File javascript CoreAstro CoreMiddleware 7 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  28f6891b_4087_59e7_aeca_d1972ad6d0b2["test.js"]
  5d6d1861_a18d_b246_cd94_08889ab7e74c["promises"]
  28f6891b_4087_59e7_aeca_d1972ad6d0b2 --> 5d6d1861_a18d_b246_cd94_08889ab7e74c
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  28f6891b_4087_59e7_aeca_d1972ad6d0b2 --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  6b0635f9_51ea_77aa_767b_7857878e98a6["node:test"]
  28f6891b_4087_59e7_aeca_d1972ad6d0b2 --> 6b0635f9_51ea_77aa_767b_7857878e98a6
  6cf37c77_0b3e_ab41_abe6_fceb07530910["reporters"]
  28f6891b_4087_59e7_aeca_d1972ad6d0b2 --> 6cf37c77_0b3e_ab41_abe6_fceb07530910
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  28f6891b_4087_59e7_aeca_d1972ad6d0b2 --> d9a92db9_c95e_9165_13ac_24b3d859d946
  b4a76fc8_3591_85b4_7b57_55ab21d1030d["node:util"]
  28f6891b_4087_59e7_aeca_d1972ad6d0b2 --> b4a76fc8_3591_85b4_7b57_55ab21d1030d
  e64464d4_88a4_c7e2_f90f_758b06231bbe["tinyglobby"]
  28f6891b_4087_59e7_aeca_d1972ad6d0b2 --> e64464d4_88a4_c7e2_f90f_758b06231bbe
  style 28f6891b_4087_59e7_aeca_d1972ad6d0b2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import fs from 'node:fs/promises';
import path from 'node:path';
import { run } from 'node:test';
import { spec } from 'node:test/reporters';
import { pathToFileURL } from 'node:url';
import { parseArgs } from 'node:util';
import { glob } from 'tinyglobby';

const isCI = !!process.env.CI;
// 30 minutes in CI, 10 locally
const defaultTimeout = isCI ? 1860000 : 600000;

export default async function test() {
	const args = parseArgs({
		allowPositionals: true,
		options: {
			// aka --test-name-pattern: https://nodejs.org/api/test.html#filtering-tests-by-name
			match: { type: 'string', alias: 'm' },
			// aka --test-only: https://nodejs.org/api/test.html#only-tests
			only: { type: 'boolean', alias: 'o' },
			// aka --test-concurrency: https://nodejs.org/api/test.html#test-runner-execution-model
			parallel: { type: 'boolean', alias: 'p' },
			// experimental: https://nodejs.org/api/test.html#watch-mode
			watch: { type: 'boolean', alias: 'w' },
			// Test timeout in milliseconds (default: 30000ms)
			timeout: { type: 'string', alias: 't' },
			// Test setup file
			setup: { type: 'string', alias: 's' },
			// Test teardown file
			teardown: { type: 'string' },
			// Use tsx to run the tests,
			tsx: { type: 'boolean' },
			// Configures the test runner to exit the process once all known tests have finished executing even if the event loop would otherwise remain active
			'force-exit': { type: 'boolean' },
			// Test teardown file to include in the test files list
			'teardown-test': { type: 'string' },
		},
	});

	const pattern = args.positionals[1];
	if (!pattern) throw new Error('Missing test glob pattern');

	const files = await glob(pattern, {
		filesOnly: true,
		absolute: true,
		ignore: ['**/node_modules/**'],
	});

	if (args.values['teardown-test']) {
		files.push(path.resolve(args.values['teardown-test']));
	}

	// For some reason, the `only` option does not work and we need to explicitly set the CLI flag instead.
	// Node.js requires opt-in to run .only tests :(
	// https://nodejs.org/api/test.html#only-tests
	if (args.values.only) {
		process.env.NODE_OPTIONS ??= '';
		process.env.NODE_OPTIONS += ' --test-only';
	}

	if (args.values.tsx) {
		process.env.NODE_OPTIONS ??= '';
		process.env.NODE_OPTIONS += ' --import tsx';
	}

	if (!args.values.parallel) {
		// If not parallel, we create a temporary file that imports all the test files
		// so that it all runs in a single process.
		const tempTestFile = path.resolve('./node_modules/.astro/test.mjs');
		await fs.mkdir(path.dirname(tempTestFile), { recursive: true });
		await fs.writeFile(
			tempTestFile,
			files.map((f) => `import ${JSON.stringify(pathToFileURL(f).toString())};`).join('\n'),
		);

		files.length = 0;
		files.push(tempTestFile);
	}

	const teardownModule = args.values.teardown
		? await import(pathToFileURL(path.resolve(args.values.teardown)).toString())
		: undefined;

	const setupModule = args.values.setup
		? await import(pathToFileURL(path.resolve(args.values.setup)).toString())
		: undefined;

	// https://nodejs.org/api/test.html#runoptions
	run({
		files,
		testNamePatterns: args.values.match
			? args.values['teardown-test']
				? [args.values.match, 'Teardown']
				: args.values.match
			: undefined,
		concurrency: args.values.parallel,
		only: args.values.only,
		setup: setupModule?.default,
		watch: args.values.watch,
		timeout: args.values.timeout ? Number(args.values.timeout) : defaultTimeout, // Node.js defaults to Infinity, so set better fallback
		forceExit: args.values['force-exit'],
	})
		.on('test:fail', () => {
			// For some reason, a test fail using the JS API does not set an exit code of 1,
			// so we set it here manually
			process.exitCode = 1;
		})
		.on('end', () => {
			const testPassed = process.exitCode === 0 || process.exitCode === undefined;
			teardownModule?.default(testPassed);
		})
		.pipe(new spec())
		.pipe(process.stdout);
}

Domain

Subdomains

Functions

Dependencies

  • node:path
  • node:test
  • node:url
  • node:util
  • promises
  • reporters
  • tinyglobby

Frequently Asked Questions

What does test.js do?
test.js is a source file in the astro codebase, written in javascript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in test.js?
test.js defines 1 function(s): test.
What does test.js depend on?
test.js imports 7 module(s): node:path, node:test, node:url, node:util, promises, reporters, tinyglobby.
Where is test.js in the architecture?
test.js is located at scripts/cmd/test.js (domain: CoreAstro, subdomain: CoreMiddleware, directory: scripts/cmd).

Analyze Your Own Codebase

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

Try Supermodel Free