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. 4 imports, 20 dependents.

File javascript CoreAstro CoreMiddleware 4 imports 20 dependents 5 functions

Entity Profile

Dependency Diagram

graph LR
  ff334e41_2760_839e_fc38_ab9318c18dfc["test-utils.js"]
  0a624eac_945e_c9e8_c9de_3feb9de2dd15["test-utils.js"]
  ff334e41_2760_839e_fc38_ab9318c18dfc --> 0a624eac_945e_c9e8_c9de_3feb9de2dd15
  dd4f09ce_3fd7_8295_f616_8876cda4555c["loadFixture"]
  ff334e41_2760_839e_fc38_ab9318c18dfc --> dd4f09ce_3fd7_8295_f616_8876cda4555c
  fc0217ba_5b68_32cc_f77d_49b4596e615e["node:events"]
  ff334e41_2760_839e_fc38_ab9318c18dfc --> fc0217ba_5b68_32cc_f77d_49b4596e615e
  0132f612_2bd0_7945_e548_57b5235cfc54["node-mocks-http"]
  ff334e41_2760_839e_fc38_ab9318c18dfc --> 0132f612_2bd0_7945_e548_57b5235cfc54
  16e7fb07_ae19_7dcd_c4e1_9bbcfc63ada6["api-route.test.js"]
  16e7fb07_ae19_7dcd_c4e1_9bbcfc63ada6 --> ff334e41_2760_839e_fc38_ab9318c18dfc
  88032f43_074b_e17c_21ad_fb8ea851953b["assets.test.js"]
  88032f43_074b_e17c_21ad_fb8ea851953b --> ff334e41_2760_839e_fc38_ab9318c18dfc
  2bd4703a_4d86_3170_5b40_e4140dfb504d["bad-urls.test.js"]
  2bd4703a_4d86_3170_5b40_e4140dfb504d --> ff334e41_2760_839e_fc38_ab9318c18dfc
  8ae931f1_3254_60f3_4bee_e625daa5a1fe["encoded.test.js"]
  8ae931f1_3254_60f3_4bee_e625daa5a1fe --> ff334e41_2760_839e_fc38_ab9318c18dfc
  c7c1034b_9f55_0f9a_eeb6_33dd8764f56f["error-page-host.test.js"]
  c7c1034b_9f55_0f9a_eeb6_33dd8764f56f --> ff334e41_2760_839e_fc38_ab9318c18dfc
  ad4fa82c_9b07_bb3c_2653_22af64b27a1c["errors.test.js"]
  ad4fa82c_9b07_bb3c_2653_22af64b27a1c --> ff334e41_2760_839e_fc38_ab9318c18dfc
  319f466e_43c3_79d2_2a8b_48308f94dccb["headers.test.js"]
  319f466e_43c3_79d2_2a8b_48308f94dccb --> ff334e41_2760_839e_fc38_ab9318c18dfc
  27eedb97_fe65_a5ff_3646_65231ad39efe["image.test.js"]
  27eedb97_fe65_a5ff_3646_65231ad39efe --> ff334e41_2760_839e_fc38_ab9318c18dfc
  04b035fa_05b8_32f1_445c_07ea89146837["locals.test.js"]
  04b035fa_05b8_32f1_445c_07ea89146837 --> ff334e41_2760_839e_fc38_ab9318c18dfc
  165fd3dc_7654_c82f_234d_6594d4be498c["node-middleware.test.js"]
  165fd3dc_7654_c82f_234d_6594d4be498c --> ff334e41_2760_839e_fc38_ab9318c18dfc
  style ff334e41_2760_839e_fc38_ab9318c18dfc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { EventEmitter } from 'node:events';
import httpMocks from 'node-mocks-http';
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';

process.env.ASTRO_NODE_AUTOSTART = 'disabled';
process.env.ASTRO_NODE_LOGGING = 'disabled';
/**
 * @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
 */

export function loadFixture(inlineConfig) {
	if (!inlineConfig?.root) throw new Error("Must provide { root: './fixtures/...' }");

	// resolve the relative root (i.e. "./fixtures/tailwindcss") to a full filepath
	// without this, the main `loadFixture` helper will resolve relative to `packages/astro/test`
	return baseLoadFixture({
		...inlineConfig,
		root: new URL(inlineConfig.root, import.meta.url).toString(),
	});
}

export function createRequestAndResponse(reqOptions) {
	const req = httpMocks.createRequest(reqOptions);

	const res = httpMocks.createResponse({
		eventEmitter: EventEmitter,
		req,
	});

	const done = toPromise(res);

	// Get the response as text
	const text = async () => {
		const chunks = await done;
		return buffersToString(chunks);
	};

	return { req, res, done, text };
}

/** @returns {Promise<Array<Buffer>>} */
function toPromise(res) {
	return new Promise((resolve) => {
		// node-mocks-http doesn't correctly handle non-Buffer typed arrays,
		// so override the write method to fix it.
		const write = res.write;
		res.write = function (data, encoding) {
			if (ArrayBuffer.isView(data) && !Buffer.isBuffer(data)) {
				data = Buffer.from(data.buffer);
			}
			return write.call(this, data, encoding);
		};
		res.on('end', () => {
			const chunks = res._getChunks();
			resolve(chunks);
		});
	});
}

function buffersToString(buffers) {
	const decoder = new TextDecoder();
	let str = '';
	for (const buffer of buffers) {
		str += decoder.decode(buffer);
	}
	return str;
}

export function waitServerListen(server) {
	return new Promise((resolve, reject) => {
		function onListen() {
			server.off('error', onError);
			resolve();
		}
		function onError(error) {
			server.off('listening', onListen);
			reject(error);
		}
		server.once('listening', onListen);
		server.once('error', onError);
	});
}

Domain

Subdomains

Dependencies

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, CoreMiddleware subdomain.
What functions are defined in test-utils.js?
test-utils.js defines 5 function(s): buffersToString, createRequestAndResponse, loadFixture, toPromise, waitServerListen.
What does test-utils.js depend on?
test-utils.js imports 4 module(s): loadFixture, node-mocks-http, node:events, test-utils.js.
What files import test-utils.js?
test-utils.js is imported by 20 file(s): api-route.test.js, assets.test.js, bad-urls.test.js, encoded.test.js, error-page-host.test.js, errors.test.js, headers.test.js, image.test.js, and 12 more.
Where is test-utils.js in the architecture?
test-utils.js is located at packages/integrations/node/test/test-utils.js (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/integrations/node/test).

Analyze Your Own Codebase

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

Try Supermodel Free