Skip to content

Commit

Permalink
fix: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Feb 21, 2025
1 parent 47cbe16 commit 1cd6656
Show file tree
Hide file tree
Showing 27 changed files with 251 additions and 263 deletions.
1 change: 1 addition & 0 deletions packages/cli/plugin-bff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@swc/helpers": "0.5.13"
},
"devDependencies": {
"@modern-js/plugin-v2": "workspace:*",
"@modern-js/app-tools": "workspace:*",
"@modern-js/bff-runtime": "workspace:*",
"@modern-js/core": "workspace:*",
Expand Down
80 changes: 47 additions & 33 deletions packages/cli/plugin-bff/tests/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
import path from 'path';
import type { Plugin as BasePlugin } from '@modern-js/plugin-v2';
import { server } from '@modern-js/plugin-v2/server';
import {
PluginManager,
type ServerConfig,
type ServerPlugin,
createContext,
type ServerPluginFurure,
compatPlugin,
handleSetupResult,
} from '@modern-js/server-core';
import plugin from '../src/server';
import './helper';
import { assign } from '@modern-js/utils/lodash';

const noop = () => {};

const pwd = path.resolve(__dirname, './fixtures/function');

function createRunner(plugins?: ServerPlugin[]) {
const appContext = createContext<any>({});
const pluginManager = new PluginManager({
appContext,
cliConfig: {
html: {},
output: {},
source: {},
tools: {},
server: {},
runtime: {},
bff: {},
dev: {},
security: {},
},
// biome-ignore lint/suspicious/noExportsInTest: <explanation>
export async function serverInit({
plugins,
serverConfig,
}: {
plugins?: (ServerPlugin | ServerPluginFurure)[];
serverConfig?: ServerConfig;
}) {
const { serverContext } = await server.run({
plugins: [compatPlugin(), ...(plugins || [])] as BasePlugin[],
options: { appContext: {}, pwd: process.cwd() },
config: assign(
{},
{
dev: {},
output: {},
source: {},
tools: {},
server: {},
html: {},
runtime: {},
bff: {},
security: {},
},
serverConfig,
),
handleSetupResult,
});

plugins && pluginManager.addPlugins(plugins);

return pluginManager.init();
const hooks = serverContext.pluginAPI?.getHooks();
return hooks as any;
}

describe('bff server plugin', () => {
Expand All @@ -51,15 +67,14 @@ describe('bff server plugin', () => {
},
};

const runner = await createRunner([plugin(), mockApiPlugin]);
const hooks = await serverInit({
plugins: [plugin(), mockApiPlugin],
});

await runner.prepareApiServer(
{
pwd,
prefix: '/',
},
{ onLast: () => noop as any },
);
await hooks.prepareApiServer.call({
pwd,
prefix: '/',
});

expect(apiHandlerInfos).toMatchSnapshot();
});
Expand All @@ -81,12 +96,11 @@ describe('bff server plugin', () => {
},
};

const runner = await createRunner([plugin(), mockApiPlugin]);
const hooks = await serverInit({
plugins: [plugin(), mockApiPlugin],
});

await runner.prepareApiServer(
{ pwd, prefix: '/api' },
{ onLast: () => noop as any },
);
await hooks.prepareApiServer.call({ pwd, prefix: '/api' });
expect(apiHandlerInfos).toMatchSnapshot();
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/server/core/src/plugins/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from '../../types';
import { getHookRunners } from './hooks';

export { handleSetupResult } from './hooks';
export const compatPlugin = (): ServerPluginFurure => ({
name: '@modern-js/server-compat',
registryHooks: {
Expand Down
3 changes: 2 additions & 1 deletion packages/server/core/src/plugins/customServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
UnstableMiddleware,
UnstableMiddlewareContext,
} from '@modern-js/types';
import { isFunction } from '@modern-js/utils';
import type { ServerNodeEnv } from '../../adapters/node/hono';
import type * as streamModule from '../../adapters/node/polyfills/stream';
import { ServerTimings } from '../../constants';
Expand Down Expand Up @@ -163,7 +164,7 @@ export class CustomServer {
const serverMiddleware = await this.serverMiddlewarePromise;

// if no server middleware in server/index.ts, return render middleware
if (!serverMiddleware) {
if (!serverMiddleware || !isFunction(serverMiddleware)) {
return renderMiddlewares;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/server/core/src/plugins/default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Logger } from '@modern-js/types';
import type { ServerPlugin } from '../types';
import type { ServerPlugin, ServerPluginFurure } from '../types';
import { compatPlugin } from './compat';
import { logPlugin } from './log';
import {
initMonitorsPlugin,
Expand Down Expand Up @@ -32,7 +33,8 @@ function createSilenceLogger() {
export function createDefaultPlugins(
options: CreateDefaultPluginsOptions = {},
) {
const plugins: ServerPlugin[] = [
const plugins: (ServerPlugin | ServerPluginFurure)[] = [
compatPlugin(),
logPlugin(),
initMonitorsPlugin(),
injectRenderHandlerPlugin(options),
Expand Down
2 changes: 1 addition & 1 deletion packages/server/core/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export {
createDefaultPlugins,
type CreateDefaultPluginsOptions,
} from './default';
export { compatPlugin } from './compat';
export { compatPlugin, handleSetupResult } from './compat';
1 change: 0 additions & 1 deletion packages/server/core/src/serverBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class ServerBase<E extends Env = any> {
handleSetupResult,
});
serverContext.serverBase = this;
serverContext.middlewares = [];
this.serverContext = serverContext as unknown as ServerContext;
this.#applyMiddlewares();

Expand Down
90 changes: 0 additions & 90 deletions packages/server/core/tests/pluginManager.test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/server/core/tests/plugins/favicon.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createServerBase, faviconPlugin } from '../../src';
import { compatPlugin, createServerBase, faviconPlugin } from '../../src';
import { getDefaultAppContext, getDefaultConfig } from '../helpers';

describe('favion plugin', () => {
Expand All @@ -9,7 +9,7 @@ describe('favion plugin', () => {
appContext: getDefaultAppContext(),
});

server.addPlugins([faviconPlugin()]);
server.addPlugins([compatPlugin(), faviconPlugin()]);

await server.init();
const response = await server.request('/favicon.ico');
Expand Down
1 change: 1 addition & 0 deletions packages/server/plugin-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"type-is": "^1.6.18"
},
"devDependencies": {
"@modern-js/plugin-v2": "workspace:*",
"@modern-js/app-tools": "workspace:*",
"@modern-js/core": "workspace:*",
"@scripts/build": "workspace:*",
Expand Down
12 changes: 5 additions & 7 deletions packages/server/plugin-express/tests/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import request from 'supertest';
import plugin from '../src/plugin';
import { APIPlugin, createPluginManager } from './helpers';
import { APIPlugin, serverInit } from './helpers';
import './common';

const pwd = path.join(__dirname, './fixtures/operator');
Expand All @@ -10,13 +10,11 @@ describe('support api function', () => {
let apiHandler: any;
const prefix = '/api';
beforeAll(async () => {
const pluginManager = createPluginManager();

pluginManager.addPlugins([APIPlugin, plugin()]);

const runner = await pluginManager.init();
const hooks = await serverInit({
plugins: [APIPlugin, plugin()],
});

apiHandler = await runner.prepareApiServer({
apiHandler = await hooks.prepareApiServer.call({
pwd,
prefix,
});
Expand Down
12 changes: 5 additions & 7 deletions packages/server/plugin-express/tests/decider.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import request from 'supertest';
import plugin from '../src/plugin';
import { APIPlugin, createPluginManager } from './helpers';
import { APIPlugin, serverInit } from './helpers';

const pwd = path.join(__dirname, './fixtures/lambda-mode');

Expand All @@ -12,13 +12,11 @@ describe('support input params decider', () => {
let apiHandler: any;

beforeAll(async () => {
const pluginManager = createPluginManager();

pluginManager.addPlugins([APIPlugin, plugin()]);

const runner = await pluginManager.init();
const hooks = await serverInit({
plugins: [APIPlugin, plugin()],
});

apiHandler = await runner.prepareApiServer({
apiHandler = await hooks.prepareApiServer.call({
pwd,
prefix,
httpMethodDecider: 'inputParams',
Expand Down
10 changes: 5 additions & 5 deletions packages/server/plugin-express/tests/functionMode.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import request from 'supertest';
import plugin from '../src/plugin';
import { APIPlugin, createPluginManager } from './helpers';
import { APIPlugin, serverInit } from './helpers';
import './common';

const pwd = path.join(__dirname, './fixtures/function-mode');
Expand All @@ -13,11 +13,11 @@ describe('function-mode', () => {
let apiHandler: any;

beforeAll(async () => {
const pluginManager = createPluginManager();
pluginManager.addPlugins([APIPlugin, plugin()]);
const hooks = await serverInit({
plugins: [APIPlugin, plugin()],
});

const runner = await pluginManager.init();
apiHandler = await runner.prepareApiServer({
apiHandler = await hooks.prepareApiServer.call({
pwd,
prefix: '/',
});
Expand Down
Loading

0 comments on commit 1cd6656

Please sign in to comment.