From dceb1502fd35aaa1545607a5d9b3c8911f9a3cb9 Mon Sep 17 00:00:00 2001 From: caohuilin Date: Thu, 12 Dec 2024 11:56:15 +0800 Subject: [PATCH] fix: unit test --- .../runtime/plugin-router-v5/package.json | 1 + .../plugin-router-v5/tests/index.test.ts | 53 +++++++++++++++---- .../tests/document/cli.test.tsx | 6 +-- pnpm-lock.yaml | 3 ++ 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/packages/runtime/plugin-router-v5/package.json b/packages/runtime/plugin-router-v5/package.json index d77b579595de..a36b7b114d72 100644 --- a/packages/runtime/plugin-router-v5/package.json +++ b/packages/runtime/plugin-router-v5/package.json @@ -62,6 +62,7 @@ }, "dependencies": { "@modern-js/plugin": "workspace:*", + "@modern-js/plugin-v2": "workspace:*", "@modern-js/runtime-utils": "workspace:*", "@modern-js/types": "workspace:*", "@modern-js/utils": "workspace:*", diff --git a/packages/runtime/plugin-router-v5/tests/index.test.ts b/packages/runtime/plugin-router-v5/tests/index.test.ts index 922340dca282..595a2591f6ca 100644 --- a/packages/runtime/plugin-router-v5/tests/index.test.ts +++ b/packages/runtime/plugin-router-v5/tests/index.test.ts @@ -1,5 +1,7 @@ -import { AppContext, manager } from '@modern-js/core'; -import RuntimePlugin from '@modern-js/runtime/cli'; +import type { AppTools } from '@modern-js/app-tools'; +import { createPluginManager } from '@modern-js/plugin-v2'; +import { createContext, initPluginAPI } from '@modern-js/plugin-v2/cli'; +import runtimePlugin from '@modern-js/runtime/cli'; import plugin, { useHistory, useParams } from '../src'; import cliPlugin from '../src/cli'; @@ -12,20 +14,51 @@ describe('plugin-router-legacy', () => { }); describe('cli-router-legacy', () => { - const main = manager.clone().usePlugin(RuntimePlugin, cliPlugin as any); - let runner: any; - - beforeAll(async () => { - runner = await main.init(); - }); + const setup = async () => { + const pluginManager = createPluginManager(); + pluginManager.addPlugins([runtimePlugin(), cliPlugin() as Plugin]); + const plugins = pluginManager.getPlugins(); + const context = await createContext({ + appContext: { + plugins, + } as any, + config: {}, + normalizedConfig: { plugins: [] } as any, + }); + const pluginAPI = { + ...initPluginAPI({ + context, + pluginManager, + }), + checkEntryPoint: ({ path, entry }: any) => { + return { path, entry }; + }, + modifyEntrypoints: ({ entrypoints }: any) => { + return { entrypoints }; + }, + generateEntryCode: async ({ entrypoints }: any) => {}, + _internalRuntimePlugins: ({ entrypoint, plugins }: any) => { + return { entrypoint, plugins }; + }, + addRuntimeExports: () => {}, + modifyFileSystemRoutes: () => {}, + onBeforeGenerateRoutes: () => {}, + }; + context.pluginAPI = pluginAPI; + for (const plugin of plugins) { + await plugin.setup(pluginAPI); + } + return pluginAPI; + }; test('should plugin-router-legacy defined', async () => { expect(cliPlugin).toBeDefined(); }); it('plugin-router-legacy cli config is defined', async () => { - AppContext.set({ metaName: 'modern-js' } as any); - const config = await runner.config(); + const api = await setup(); + api.updateAppContext({ metaName: 'modern-js' } as any); + const config = await api.getHooks().config.call(); expect( config.find( (item: any) => item.source.alias['@modern-js/runtime/plugins'], diff --git a/packages/runtime/plugin-runtime/tests/document/cli.test.tsx b/packages/runtime/plugin-runtime/tests/document/cli.test.tsx index d80ba49348ae..fa678dc35d22 100644 --- a/packages/runtime/plugin-runtime/tests/document/cli.test.tsx +++ b/packages/runtime/plugin-runtime/tests/document/cli.test.tsx @@ -7,11 +7,7 @@ import { } from '@modern-js/plugin-v2'; import { createContext, initPluginAPI } from '@modern-js/plugin-v2/cli'; -import type { - AppTools, - AppToolsContext, - AppToolsHooks, -} from '@modern-js/app-tools'; +import type { AppTools, AppToolsContext } from '@modern-js/app-tools'; import { getBundleEntry } from '../../../../solutions/app-tools/src/plugins/analyze/getBundleEntry'; import { documentPlugin, getDocumenByEntryName } from '../../src/document/cli'; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8528dee7c687..5628e8186b85 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2310,6 +2310,9 @@ importers: '@modern-js/plugin': specifier: workspace:* version: link:../../toolkit/plugin + '@modern-js/plugin-v2': + specifier: workspace:* + version: link:../../toolkit/plugin-v2 '@modern-js/runtime-utils': specifier: workspace:* version: link:../../toolkit/runtime-utils