From f3af7b12f0196d6765e7b194e33e94e281e7fd75 Mon Sep 17 00:00:00 2001 From: harpsealjs Date: Mon, 27 May 2024 19:51:16 +0800 Subject: [PATCH] test: export runner (#6630) --- packages/rspack-test-tools/etc/api.md | 175 ++++++++++++++++++ .../rspack-test-tools/src/runner/index.ts | 1 + .../src/runner/runner/index.ts | 8 + 3 files changed, 184 insertions(+) create mode 100644 packages/rspack-test-tools/src/runner/runner/index.ts diff --git a/packages/rspack-test-tools/etc/api.md b/packages/rspack-test-tools/etc/api.md index 9b7ed599d67..f319cc44a8c 100644 --- a/packages/rspack-test-tools/etc/api.md +++ b/packages/rspack-test-tools/etc/api.md @@ -11,6 +11,9 @@ import { Compiler } from '@rspack/core'; import { Compiler as Compiler_2 } from 'webpack'; import type { Configuration } from 'webpack'; import EventEmitter from 'events'; +import { IBasicGlobalContext as IBasicGlobalContext_2 } from '../type'; +import { IBasicGlobalContext as IBasicGlobalContext_3 } from '../../type'; +import { IBasicModuleScope as IBasicModuleScope_2 } from '../../type'; import { ITestCompilerManager as ITestCompilerManager_2 } from '../type'; import { RspackOptions } from '@rspack/core'; import { RspackOptionsNormalized } from '@rspack/core'; @@ -21,6 +24,7 @@ import { StatsCompilation } from '@rspack/core'; import type { StatsCompilation as StatsCompilation_2 } from 'webpack'; import { StatsError } from '@rspack/core'; import { StatsWarnings } from '@rspack/core'; +import { TRunnerRequirer as TRunnerRequirer_2 } from '..'; import { WebpackOptionsNormalized } from 'webpack'; // @public (undocumented) @@ -50,6 +54,39 @@ export class BasicProcessor implements ITestProcessor { run(env: ITestEnv, context: ITestContext): Promise; } +// @public (undocumented) +export abstract class BasicRunner implements ITestRunner { + constructor(_options: IBasicRunnerOptions); + // (undocumented) + protected baseModuleScope: IBasicModuleScope | null; + // (undocumented) + protected abstract createBaseModuleScope(): IBasicModuleScope; + // (undocumented) + protected abstract createGlobalContext(): IBasicGlobalContext; + // (undocumented) + protected abstract createModuleScope(requireFn: TRunnerRequirer, m: TModuleObject, file: TBasicRunnerFile): IBasicModuleScope; + // (undocumented) + protected createRunner(): void; + // (undocumented) + protected getFile(modulePath: string[] | string, currentDirectory: string): TBasicRunnerFile | null; + // (undocumented) + getGlobal(name: string): unknown; + // (undocumented) + getRequire(): TRunnerRequirer; + // (undocumented) + protected globalContext: IBasicGlobalContext | null; + // (undocumented) + protected _options: IBasicRunnerOptions; + // (undocumented) + protected postExecute(m: Object, file: TBasicRunnerFile): void; + // (undocumented) + protected preExecute(code: string, file: TBasicRunnerFile): void; + // (undocumented) + protected requirers: Map; + // (undocumented) + run(file: string): Promise; +} + // @public (undocumented) export class BasicRunnerFactory implements TRunnerFactory { constructor(name: string, context: ITestContext); @@ -74,6 +111,22 @@ export class BuiltinProcessor extends SnapshotProcessor static defaultOptions(this: BuiltinProcessor, context: ITestContext): TCompilerOptions; } +// @public (undocumented) +export class CommonJsRunner extends BasicRunner { + // (undocumented) + protected createBaseModuleScope(): IBasicModuleScope; + // (undocumented) + protected createCjsRequirer(): TRunnerRequirer; + // (undocumented) + protected createGlobalContext(): IBasicGlobalContext; + // (undocumented) + protected createMissRequirer(): TRunnerRequirer; + // (undocumented) + protected createModuleScope(requireFn: TRunnerRequirer, m: TModuleObject, file: TBasicRunnerFile): IBasicModuleScope; + // (undocumented) + protected createRunner(): void; +} + // @public (undocumented) export function compareContent(sourceContent: string | false, distContent: string | false, compareOptions: ICompareOptions): TCompareResult; @@ -300,6 +353,37 @@ export function escapeEOL(str: string): string; // @public (undocumented) export function escapeSep(str: string): string; +// @public (undocumented) +export class EsmRunner extends CommonJsRunner { + // (undocumented) + protected createEsmRequirer(): TRunnerRequirer; + // (undocumented) + protected createRunner(): void; +} + +// @public (undocumented) +export class FakeDocumentWebRunner extends CommonJsRunner { + constructor(_webOptions: IBasicRunnerOptions); + // (undocumented) + protected createBaseModuleScope(): IBasicModuleScope_2; + // (undocumented) + protected createGlobalContext(): IBasicGlobalContext_3; + // (undocumented) + protected createJsonRequirer(): TRunnerRequirer; + // (undocumented) + protected createModuleScope(requireFn: TRunnerRequirer, m: any, file: TBasicRunnerFile): IBasicModuleScope_2; + // (undocumented) + protected createRunner(): void; + // (undocumented) + protected postExecute(_: Object, file: TBasicRunnerFile): void; + // (undocumented) + protected preExecute(_: string, file: TBasicRunnerFile): void; + // (undocumented) + run(file: string): Promise; + // (undocumented) + protected _webOptions: IBasicRunnerOptions; +} + // @public (undocumented) export function formatCode(name: string, raw: string, options: IFormatCodeOptions): string; @@ -447,6 +531,26 @@ export interface IBasicProcessorOptions { runable: boolean; } +// @public (undocumented) +export interface IBasicRunnerOptions { + // (undocumented) + compilerOptions: TCompilerOptions; + // (undocumented) + dist: string; + // (undocumented) + env: ITestEnv; + // (undocumented) + name: string; + // (undocumented) + runInNewContext?: boolean; + // (undocumented) + source: string; + // (undocumented) + stats?: TCompilerStatsCompilation; + // (undocumented) + testConfig: TTestConfig; +} + // @public (undocumented) export interface IBuiltinProcessorOptions extends Omit, "runable"> { } @@ -873,10 +977,45 @@ export interface IWatchProcessorOptions extends IMultiT tempDir: string; } +// @public (undocumented) +interface IWatchRunnerOptions extends IBasicRunnerOptions { + // (undocumented) + stepName: string; +} + // @public (undocumented) export interface IWatchStepProcessorOptions extends Omit, "experiments" | "optimization"> { } +// @public (undocumented) +export interface IWebRunnerOptions extends IBasicRunnerOptions { + // (undocumented) + dom: "fake" | "jsdom"; +} + +// @public (undocumented) +export class JSDOMWebRunner extends CommonJsRunner { + constructor(_webOptions: IBasicRunnerOptions); + // (undocumented) + protected createBaseModuleScope(): IBasicModuleScope_2; + // (undocumented) + protected createJSDOMRequirer(): TRunnerRequirer; + // (undocumented) + protected createResourceLoader(): { + fetch(url: string, _: { + element: HTMLScriptElement; + }): any; + }; + // (undocumented) + protected createRunner(): void; + // (undocumented) + getGlobal(name: string): unknown; + // (undocumented) + run(file: string): Promise; + // (undocumented) + protected _webOptions: IBasicRunnerOptions; +} + // @public (undocumented) export class MultipleRunnerFactory extends BasicRunnerFactory { // (undocumented) @@ -912,6 +1051,16 @@ export class NormalProcessor extends BasicProcessor protected _normalOptions: INormalProcessorOptions; } +// @public (undocumented) +export class NormalRunner extends EsmRunner { + // (undocumented) + protected createBaseModuleScope(): IBasicModuleScope; + // (undocumented) + protected createModuleScope(requireFn: TRunnerRequirer, m: { + exports: unknown; + }, file: TBasicRunnerFile): IBasicModuleScope; +} + // @public (undocumented) export class NormalRunnerFactory extends BasicRunnerFactory { // (undocumented) @@ -1293,6 +1442,17 @@ export class WatchProcessor extends MultiTaskProcessor< protected _watchOptions: IWatchProcessorOptions; } +// @public (undocumented) +export class WatchRunner extends CommonJsRunner { + constructor(_watchOptions: IWatchRunnerOptions); + // (undocumented) + protected createGlobalContext(): IBasicGlobalContext_2; + // (undocumented) + protected createModuleScope(requireFn: TRunnerRequirer, m: any, file: TBasicRunnerFile): IBasicModuleScope; + // (undocumented) + protected _watchOptions: IWatchRunnerOptions; +} + // @public (undocumented) export class WatchRunnerFactory extends BasicRunnerFactory { // (undocumented) @@ -1328,6 +1488,21 @@ export class WebpackModulePlaceholderPlugin { apply(compiler: any): void; } +// @public (undocumented) +export class WebRunner implements ITestRunner { + constructor(_webOptions: IWebRunnerOptions); + // (undocumented) + getGlobal(name: string): unknown; + // (undocumented) + getRequire(): TRunnerRequirer_2; + // (undocumented) + protected originMethods: Partial; + // (undocumented) + run(file: string): Promise; + // (undocumented) + protected _webOptions: IWebRunnerOptions; +} + // (No @packageDocumentation comment for this package) ``` diff --git a/packages/rspack-test-tools/src/runner/index.ts b/packages/rspack-test-tools/src/runner/index.ts index 3b1ed25aab9..4cafacb6c37 100644 --- a/packages/rspack-test-tools/src/runner/index.ts +++ b/packages/rspack-test-tools/src/runner/index.ts @@ -3,5 +3,6 @@ export * from "./hot"; export * from "./hot-step"; export * from "./multiple"; export * from "./normal"; +export * from "./runner"; export * from "./type"; export * from "./watch"; diff --git a/packages/rspack-test-tools/src/runner/runner/index.ts b/packages/rspack-test-tools/src/runner/runner/index.ts new file mode 100644 index 00000000000..5d73bd0ad3b --- /dev/null +++ b/packages/rspack-test-tools/src/runner/runner/index.ts @@ -0,0 +1,8 @@ +export * from "./basic"; +export * from "./cjs"; +export * from "./esm"; +export * from "./normal"; +export * from "./watch"; +export * from "./web"; +export * from "./web/fake"; +export * from "./web/jsdom";