Skip to content

Commit

Permalink
Move createBasicEnvironment as test-util
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 4, 2023
1 parent 7419bb6 commit c9ded7e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 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';
7 changes: 2 additions & 5 deletions packages/astro/test/units/render/head.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ import {
renderHead,
Fragment,
} from '../../../dist/runtime/server/index.js';
import {
createBasicEnvironment,
createRenderContext,
renderPage,
} from '../../../dist/core/render/index.js';
import { createRenderContext, renderPage } from '../../../dist/core/render/index.js';
import { defaultLogging as logging } from '../../test-utils.js';
import { createBasicEnvironment } from '../test-utils.js';
import * as cheerio from 'cheerio';

const createAstroModule = (AstroComponent) => ({ default: AstroComponent });
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/units/render/jsx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} 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 Down
24 changes: 24 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,9 @@ 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';

class VirtualVolume extends Volume {
#root = '';
Expand Down Expand Up @@ -151,3 +154,24 @@ 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),
ssr: options.ssr ?? true,
streaming: options.streaming ?? true,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ 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,
} from '../test-utils.js';

async function createDevEnvironment(overrides = {}) {
const env = createBasicEnvironment({
Expand Down

0 comments on commit c9ded7e

Please sign in to comment.