Skip to content

Commit

Permalink
Move createBasicEnvironment as test-util (#7560)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored and matthewp committed Jul 11, 2023
1 parent d2a7ed3 commit a273b53
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 79 deletions.
27 changes: 1 addition & 26 deletions packages/astro/src/core/render/environment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';
import type { RuntimeMode, SSRLoadedRenderer } from '../../@types/astro';
import { getDefaultClientDirectives } from '../client-directive/default.js';
import type { LogOptions } from '../logger/core.js';
import { RouteCache } from './route-cache.js';
import type { RouteCache } from './route-cache.js';

/**
* An environment represents the static parts of rendering that do not change
Expand Down Expand Up @@ -42,27 +41,3 @@ export type CreateEnvironmentArgs = Environment;
export function createEnvironment(options: CreateEnvironmentArgs): Environment {
return options;
}

export type CreateBasicEnvironmentArgs = Partial<Environment> & {
logging: CreateEnvironmentArgs['logging'];
};

/**
* Only used in tests
*/
export function createBasicEnvironment(options: CreateBasicEnvironmentArgs): Environment {
const mode = options.mode ?? 'development';
return createEnvironment({
...options,
markdown: {
...(options.markdown ?? {}),
},
mode,
renderers: options.renderers ?? [],
clientDirectives: getDefaultClientDirectives(),
resolve: options.resolve ?? ((s: string) => Promise.resolve(s)),
routeCache: new RouteCache(options.logging, mode),
ssr: options.ssr ?? true,
streaming: options.streaming ?? true,
});
}
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export { createRenderContext } from './context.js';
export type { RenderContext } from './context.js';
export { renderPage } from './core.js';
export type { Environment } from './environment';
export { createBasicEnvironment, createEnvironment } from './environment.js';
export { createEnvironment } from './environment.js';
export { getParamsAndProps } from './params-and-props.js';
export { loadRenderer, loadRenderers } from './renderer.js';
4 changes: 2 additions & 2 deletions packages/astro/test/units/config/config-server.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { fileURLToPath } from 'url';
import { defaultLogging as logging } from '../../test-utils.js';
import { defaultLogging } from '../test-utils.js';
import { openConfig } from '../../../dist/core/config/index.js';

const cwd = fileURLToPath(new URL('../../fixtures/config-host/', import.meta.url));
Expand All @@ -11,7 +11,7 @@ describe('config.server', () => {
cwd: flags.root || cwd,
flags,
cmd: 'dev',
logging,
logging: defaultLogging,
});
}

Expand Down
3 changes: 1 addition & 2 deletions packages/astro/test/units/config/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { expect } from 'chai';

import { runInContainer } from '../../../dist/core/dev/index.js';
import { openConfig, createSettings } from '../../../dist/core/config/index.js';
import { createFs } from '../test-utils.js';
import { defaultLogging } from '../../test-utils.js';
import { createFs, defaultLogging } from '../test-utils.js';

const root = new URL('../../fixtures/tailwindcss-ts/', import.meta.url);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { fileURLToPath } from 'url';
import { validateConfig } from '../../../dist/core/config/config.js';
import { createSettings } from '../../../dist/core/config/index.js';
import { sync as _sync } from '../../../dist/core/sync/index.js';
import { defaultLogging } from '../../test-utils.js';
import { createFsWithFallback } from '../test-utils.js';
import { createFsWithFallback, defaultLogging } from '../test-utils.js';

const root = new URL('../../fixtures/content-mixed-errors/', import.meta.url);
const logging = defaultLogging;
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/units/dev/collections-renderentry.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import os from 'os';

import { runInContainer } from '../../../dist/core/dev/index.js';
import { createFsWithFallback, createRequestAndResponse } from '../test-utils.js';
import { isWindows } from '../../test-utils.js';
import mdx from '../../../../integrations/mdx/dist/index.js';
import { attachContentServerListeners } from '../../../dist/content/server-listeners.js';

const root = new URL('../../fixtures/content/', import.meta.url);

const describe = isWindows ? global.describe.skip : global.describe;
const describe = os.platform() === 'win32' ? global.describe.skip : global.describe;

async function runInContainerWithContentListeners(params, callback) {
return await runInContainer(params, async (container) => {
Expand Down
9 changes: 2 additions & 7 deletions packages/astro/test/units/dev/hydration.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { expect } from 'chai';

import { runInContainer } from '../../../dist/core/dev/index.js';
import { createFs, createRequestAndResponse } from '../test-utils.js';
import { createFs, createRequestAndResponse, silentLogging } from '../test-utils.js';
import svelte from '../../../../integrations/svelte/dist/index.js';
import { defaultLogging } from '../../test-utils.js';

const root = new URL('../../fixtures/alias/', import.meta.url);

Expand Down Expand Up @@ -33,11 +32,7 @@ describe('dev container', () => {
{
fs,
root,
logging: {
...defaultLogging,
// Error is expected in this test
level: 'silent',
},
logging: silentLogging,
userConfig: {
integrations: [svelte()],
},
Expand Down
8 changes: 6 additions & 2 deletions packages/astro/test/units/dev/restart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import {
isStarted,
startContainer,
} from '../../../dist/core/dev/index.js';
import { createFs, createRequestAndResponse, triggerFSEvent } from '../test-utils.js';
import {
createFs,
createRequestAndResponse,
defaultLogging,
triggerFSEvent,
} from '../test-utils.js';
import { createSettings, openConfig } from '../../../dist/core/config/index.js';
import { defaultLogging } from '../../test-utils.js';

const root = new URL('../../fixtures/alias/', import.meta.url);

Expand Down
9 changes: 2 additions & 7 deletions packages/astro/test/units/render/components.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { expect } from 'chai';
import * as cheerio from 'cheerio';

import { runInContainer } from '../../../dist/core/dev/index.js';
import { createFs, createRequestAndResponse } from '../test-utils.js';
import { createFs, createRequestAndResponse, silentLogging } from '../test-utils.js';
import svelte from '../../../../integrations/svelte/dist/index.js';
import { defaultLogging } from '../../test-utils.js';

const root = new URL('../../fixtures/alias/', import.meta.url);

Expand Down Expand Up @@ -33,11 +32,7 @@ describe('core/render components', () => {
{
fs,
root,
logging: {
...defaultLogging,
// Error is expected in this test
level: 'silent',
},
logging: silentLogging,
userConfig: {
integrations: [svelte()],
},
Expand Down
13 changes: 3 additions & 10 deletions packages/astro/test/units/render/head.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import {
renderHead,
Fragment,
} from '../../../dist/runtime/server/index.js';
import {
createBasicEnvironment,
createRenderContext,
renderPage,
} from '../../../dist/core/render/index.js';
import { defaultLogging as logging } from '../../test-utils.js';
import { createRenderContext, renderPage } from '../../../dist/core/render/index.js';
import { createBasicEnvironment } from '../test-utils.js';
import * as cheerio from 'cheerio';

const createAstroModule = (AstroComponent) => ({ default: AstroComponent });
Expand All @@ -23,10 +19,7 @@ describe('core/render', () => {
describe('Injected head contents', () => {
let env;
before(async () => {
env = createBasicEnvironment({
logging,
renderers: [],
});
env = createBasicEnvironment();
});

it('Multi-level layouts and head injection, with explicit head', async () => {
Expand Down
10 changes: 2 additions & 8 deletions packages/astro/test/units/render/jsx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ import {
renderSlot,
} from '../../../dist/runtime/server/index.js';
import { jsx } from '../../../dist/jsx-runtime/index.js';
import {
createBasicEnvironment,
createRenderContext,
renderPage,
loadRenderer,
} from '../../../dist/core/render/index.js';
import { createRenderContext, renderPage, loadRenderer } from '../../../dist/core/render/index.js';
import { createAstroJSXComponent, renderer as jsxRenderer } from '../../../dist/jsx/index.js';
import { defaultLogging as logging } from '../../test-utils.js';
import { createBasicEnvironment } from '../test-utils.js';

const createAstroModule = (AstroComponent) => ({ default: AstroComponent });
const loadJSXRenderer = () => loadRenderer(jsxRenderer, { import: (s) => import(s) });
Expand All @@ -23,7 +18,6 @@ describe('core/render', () => {
let env;
before(async () => {
env = createBasicEnvironment({
logging,
renderers: [await loadJSXRenderer()],
});
});
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/test/units/routing/route-matching.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// @ts-check
import { createFs, createRequestAndResponse } from '../test-utils.js';
import { createFs, createRequestAndResponse, defaultLogging } from '../test-utils.js';
import { createRouteManifest, matchAllRoutes } from '../../../dist/core/routing/index.js';
import { fileURLToPath } from 'url';
import { defaultLogging } from '../../test-utils.js';
import { createViteLoader } from '../../../dist/core/module-loader/vite.js';
import { createDevelopmentEnvironment } from '../../../dist/core/render/dev/environment.js';
import { expect } from 'chai';
Expand Down
38 changes: 38 additions & 0 deletions packages/astro/test/units/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import realFS from 'node:fs';
import npath from 'path';
import { fileURLToPath } from 'url';
import { unixify } from './correct-path.js';
import { getDefaultClientDirectives } from '../../dist/core/client-directive/index.js';
import { createEnvironment } from '../../dist/core/render/index.js';
import { RouteCache } from '../../dist/core/render/route-cache.js';
import { nodeLogDestination } from '../../dist/core/logger/node.js';

/** @type {import('../../src/core/logger/core').LogOptions} */
export const defaultLogging = {
dest: nodeLogDestination,
level: 'error',
};

/** @type {import('../../src/core/logger/core').LogOptions} */
export const silentLogging = {
dest: nodeLogDestination,
level: 'error',
};

class VirtualVolume extends Volume {
#root = '';
Expand Down Expand Up @@ -151,3 +167,25 @@ export function buffersToString(buffers) {

// A convenience method for creating an astro module from a component
export const createAstroModule = (AstroComponent) => ({ default: AstroComponent });

/**
* @param {Partial<import('../../src/core/render/environment.js').CreateEnvironmentArgs>} options
* @returns {import('../../src/core/render/environment.js').Environment}
*/
export function createBasicEnvironment(options = {}) {
const mode = options.mode ?? 'development';
return createEnvironment({
...options,
markdown: {
...(options.markdown ?? {}),
},
mode,
renderers: options.renderers ?? [],
clientDirectives: getDefaultClientDirectives(),
resolve: options.resolve ?? ((s) => Promise.resolve(s)),
routeCache: new RouteCache(options.logging, mode),
logging: options.logging ?? defaultLogging,
ssr: options.ssr ?? true,
streaming: options.streaming ?? true,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { expect } from 'chai';

import { createDefaultDevSettings } from '../../../dist/core/config/index.js';
import { createLoader } from '../../../dist/core/module-loader/index.js';
import { createBasicEnvironment } from '../../../dist/core/render/index.js';
import { createRouteManifest } from '../../../dist/core/routing/index.js';
import { createComponent, render } from '../../../dist/runtime/server/index.js';
import { createController, handleRequest } from '../../../dist/vite-plugin-astro-server/index.js';
import { defaultLogging as logging } from '../../test-utils.js';
import { createAstroModule, createFs, createRequestAndResponse } from '../test-utils.js';
import {
createAstroModule,
createBasicEnvironment,
createFs,
createRequestAndResponse,
defaultLogging,
} from '../test-utils.js';

async function createDevEnvironment(overrides = {}) {
const env = createBasicEnvironment({
logging,
renderers: [],
});
const env = createBasicEnvironment();
env.settings = await createDefaultDevSettings({}, '/');
env.settings.renderers = [];
env.loader = createLoader();
Expand Down Expand Up @@ -48,7 +49,7 @@ describe('vite-plugin-astro-server', () => {
fsMod: fs,
settings: env.settings,
},
logging
defaultLogging
);

try {
Expand Down

0 comments on commit a273b53

Please sign in to comment.