Skip to content

Commit 9509e5d

Browse files
authored
test: remove runner factory (#11741)
* test: remove runner factory * test: remove runner factory
1 parent 4548937 commit 9509e5d

28 files changed

+739
-819
lines changed

packages/rspack-test-tools/etc/test-tools.api.md

Lines changed: 29 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,6 @@ export class BasicCaseCreator<T extends ECompilerType> {
6161
protected tryRunTask(): void;
6262
}
6363

64-
// @public (undocumented)
65-
export class BasicRunnerFactory<T extends ECompilerType> implements TRunnerFactory<T> {
66-
constructor(name: string, context: ITestContext);
67-
// (undocumented)
68-
protected context: ITestContext;
69-
// (undocumented)
70-
create(file: string, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
71-
// (undocumented)
72-
protected createRunner(file: string, stats: () => TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
73-
// (undocumented)
74-
protected createStatsGetter(): () => TCompilerStatsCompilation<T>;
75-
// (undocumented)
76-
protected getRunnerKey(file: string): string;
77-
// (undocumented)
78-
protected name: string;
79-
}
80-
81-
// @public (undocumented)
82-
export class CacheRunnerFactory<T extends ECompilerType> extends BasicRunnerFactory<T> {
83-
// (undocumented)
84-
protected createRunner(file: string, stats: TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
85-
}
86-
8764
// @public (undocumented)
8865
export function checkChunkModules(statsJson: any, chunkModulesMap: any, strict?: boolean): boolean;
8966

@@ -264,12 +241,6 @@ export function getSimpleProcessorRunner(src: string, dist: string, options?: {
264241
context?: (src: string, dist: string) => ITestContext;
265242
}): (name: string, processor: ITestProcessor) => Promise<void>;
266243

267-
// @public (undocumented)
268-
export class HotRunnerFactory<T extends ECompilerType> extends BasicRunnerFactory<T> {
269-
// (undocumented)
270-
protected createRunner(file: string, stats: TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
271-
}
272-
273244
// @public (undocumented)
274245
export interface IBasicCaseCreatorOptions<T extends ECompilerType> {
275246
// (undocumented)
@@ -285,7 +256,7 @@ export interface IBasicCaseCreatorOptions<T extends ECompilerType> {
285256
// (undocumented)
286257
description?: (name: string, step: number) => string;
287258
// (undocumented)
288-
runner?: new (name: string, context: ITestContext) => TRunnerFactory<ECompilerType>;
259+
runner?: TTestRunnerCreator;
289260
// (undocumented)
290261
steps: (creatorConfig: IBasicCaseCreatorOptions<T> & {
291262
name: string;
@@ -500,9 +471,7 @@ export interface ITestContext {
500471
// (undocumented)
501472
getNames(): string[];
502473
// (undocumented)
503-
getRunner(key: string): ITestRunner | null;
504-
// (undocumented)
505-
getRunnerFactory<T extends ECompilerType>(name: string): TRunnerFactory<T> | null;
474+
getRunner(name: string, file: string, env: ITestEnv): ITestRunner;
506475
// (undocumented)
507476
getSource(sub?: string): string;
508477
// (undocumented)
@@ -514,8 +483,6 @@ export interface ITestContext {
514483
// (undocumented)
515484
hasError(name?: string): boolean;
516485
// (undocumented)
517-
setRunner(key: string, runner: ITestRunner): void;
518-
// (undocumented)
519486
setValue<T>(name: string, key: string, value: T): void;
520487
}
521488

@@ -564,7 +531,7 @@ export interface ITesterConfig {
564531
// (undocumented)
565532
name: string;
566533
// (undocumented)
567-
runnerFactory?: new (name: string, context: ITestContext) => TRunnerFactory<ECompilerType>;
534+
runnerCreator?: TTestRunnerCreator;
568535
// (undocumented)
569536
src: string;
570537
// (undocumented)
@@ -626,24 +593,15 @@ export interface ITestRunner {
626593
}
627594

628595
// @public (undocumented)
629-
export class LazyCompilationTestPlugin {
596+
export interface IWebRunnerOptions<T extends ECompilerType = ECompilerType.Rspack> extends INodeRunnerOptions<T> {
630597
// (undocumented)
631-
apply(compiler: Compiler | MultiCompiler): void;
598+
dom: EDocumentType;
632599
}
633600

634601
// @public (undocumented)
635-
export class MultipleRunnerFactory<T extends ECompilerType> extends BasicRunnerFactory<T> {
636-
// (undocumented)
637-
protected createRunner(file: string, stats: () => TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
638-
// (undocumented)
639-
protected getFileIndexHandler(file: string): {
640-
getIndex: () => number[];
641-
flagIndex: () => Set<string>;
642-
};
643-
// (undocumented)
644-
protected getRunnerKey(file: string): string;
602+
export class LazyCompilationTestPlugin {
645603
// (undocumented)
646-
protected runned: Set<string>;
604+
apply(compiler: Compiler | MultiCompiler): void;
647605
}
648606

649607
// @public (undocumented)
@@ -804,9 +762,7 @@ export class TestContext implements ITestContext {
804762
// (undocumented)
805763
getNames(): string[];
806764
// (undocumented)
807-
getRunner(key: string): ITestRunner | null;
808-
// (undocumented)
809-
getRunnerFactory<T extends ECompilerType>(name: string): TRunnerFactory<T> | null;
765+
getRunner(name: string, file: string, env: ITestEnv): ITestRunner;
810766
// (undocumented)
811767
getSource(sub?: string): string;
812768
// (undocumented)
@@ -818,12 +774,8 @@ export class TestContext implements ITestContext {
818774
// (undocumented)
819775
hasError(name?: string): boolean;
820776
// (undocumented)
821-
protected runnerFactory: TRunnerFactory<ECompilerType> | null;
822-
// (undocumented)
823777
protected runners: Map<string, ITestRunner>;
824778
// (undocumented)
825-
setRunner(key: string, runner: ITestRunner): void;
826-
// (undocumented)
827779
setValue<T>(name: string, key: string, value: T): void;
828780
// (undocumented)
829781
protected store: Map<string, Record<string, unknown>>;
@@ -859,23 +811,6 @@ export type TFileCompareResult = TCompareResult & {
859811
modules: Partial<Record<"modules" | "runtimeModules", TModuleCompareResult[]>>;
860812
};
861813

862-
// @public (undocumented)
863-
export type THotStepRuntimeData = {
864-
javascript: THotStepRuntimeLangData;
865-
css: THotStepRuntimeLangData;
866-
statusPath: string[];
867-
};
868-
869-
// @public (undocumented)
870-
export type THotStepRuntimeLangData = {
871-
outdatedModules: string[];
872-
outdatedDependencies: Record<string, string[]>;
873-
updatedModules: string[];
874-
updatedRuntime: string[];
875-
acceptedModules: string[];
876-
disposedModules: string[];
877-
};
878-
879814
// @public (undocumented)
880815
export type THotUpdateContext = {
881816
updateIndex: number;
@@ -942,6 +877,12 @@ export type TTestContextOptions = Omit<ITesterConfig, "name" | "steps">;
942877
// @public (undocumented)
943878
export type TTestFilter<T extends ECompilerType> = (creatorConfig: Record<string, unknown>, testConfig: TTestConfig<T>) => boolean | string;
944879

880+
// @public (undocumented)
881+
export type TTestRunnerCreator = {
882+
key: (context: ITestContext, name: string, file: string) => string;
883+
runner: (context: ITestContext, name: string, file: string, env: ITestEnv) => ITestRunner;
884+
};
885+
945886
// @public (undocumented)
946887
export type TTestRunResult = Record<string, any>;
947888

@@ -950,16 +891,6 @@ type WatchIncrementalOptions = {
950891
ignoreNotFriendlyForIncrementalWarnings?: boolean;
951892
};
952893

953-
// @public (undocumented)
954-
export class WatchRunnerFactory<T extends ECompilerType> extends BasicRunnerFactory<T> {
955-
// (undocumented)
956-
protected createRunner(file: string, stats: () => TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
957-
// (undocumented)
958-
protected createStatsGetter(): () => TCompilerStatsCompilation<T>;
959-
// (undocumented)
960-
protected getRunnerKey(file: string): string;
961-
}
962-
963894
// @public (undocumented)
964895
export class WebpackDiffConfigPlugin {
965896
constructor(modifier?: ((options: WebpackOptionsNormalized) => WebpackOptionsNormalized) | undefined);
@@ -975,6 +906,21 @@ export class WebpackModulePlaceholderPlugin {
975906
apply(compiler: any): void;
976907
}
977908

909+
// @public (undocumented)
910+
export class WebRunner<T extends ECompilerType = ECompilerType.Rspack> implements ITestRunner {
911+
constructor(_webOptions: IWebRunnerOptions<T>);
912+
// (undocumented)
913+
getGlobal(name: string): unknown;
914+
// (undocumented)
915+
getRequire(): TRunnerRequirer;
916+
// (undocumented)
917+
protected originMethods: Partial<NodeRunner<T>>;
918+
// (undocumented)
919+
run(file: string): Promise<unknown>;
920+
// (undocumented)
921+
protected _webOptions: IWebRunnerOptions<T>;
922+
}
923+
978924
// (No @packageDocumentation comment for this package)
979925

980926
```

packages/rspack-test-tools/src/case/cache.ts

Lines changed: 154 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import path from "node:path";
2-
import rspack from "@rspack/core";
2+
import rspack, { type StatsCompilation } from "@rspack/core";
33
import { isJavaScript } from "../helper";
44
import { HotUpdatePlugin } from "../helper/hot-update";
5-
import { CacheRunnerFactory } from "../runner";
5+
import checkArrayExpectation from "../helper/legacy/checkArrayExpectation";
6+
import { WebRunner } from "../runner";
67
import { BasicCaseCreator } from "../test/creator";
7-
import type {
8-
ECompilerType,
9-
ITestContext,
10-
ITestEnv,
11-
ITestProcessor,
12-
TCompilerOptions
8+
import {
9+
type ECompilerType,
10+
EDocumentType,
11+
type IModuleScope,
12+
type ITestContext,
13+
type ITestEnv,
14+
type ITestProcessor,
15+
type TCompilerOptions,
16+
type TCompilerStatsCompilation
1317
} from "../type";
1418
import { build, check, compiler, config, getCompiler, run } from "./common";
19+
import { cachedStats } from "./runner";
1520

1621
type TTarget = TCompilerOptions<ECompilerType.Rspack>["target"];
1722

23+
const MAX_COMPILER_INDEX = 100;
24+
1825
function createCacheProcessor(
1926
name: string,
2027
src: string,
@@ -78,7 +85,10 @@ function getCreator(target: TTarget) {
7885
steps: ({ name, src, target, temp }) => [
7986
createCacheProcessor(name, src, temp!, target as TTarget)
8087
],
81-
runner: CacheRunnerFactory,
88+
runner: {
89+
key: (context: ITestContext, name: string, file: string) => name,
90+
runner: createRunner
91+
},
8292
concurrent: true
8393
})
8494
);
@@ -202,3 +212,138 @@ function findBundle(
202212
}
203213
return [...prefiles, ...files];
204214
}
215+
216+
function createRunner<T extends ECompilerType.Rspack>(
217+
context: ITestContext,
218+
name: string,
219+
file: string,
220+
env: ITestEnv
221+
) {
222+
const compiler = context.getCompiler(name);
223+
const options = compiler.getOptions() as TCompilerOptions<T>;
224+
let compilerIndex = 0;
225+
const testConfig = context.getTestConfig();
226+
const source = context.getSource();
227+
const dist = context.getDist();
228+
const updatePlugin = context.getValue<HotUpdatePlugin>(
229+
name,
230+
"hotUpdateContext"
231+
)!;
232+
const getWebRunner = () => {
233+
return new WebRunner<T>({
234+
dom: context.getValue(name, "documentType") || EDocumentType.JSDOM,
235+
env,
236+
stats: cachedStats(context, name),
237+
cachable: false,
238+
name: name,
239+
runInNewContext: false,
240+
testConfig: {
241+
...testConfig,
242+
moduleScope(
243+
ms: IModuleScope,
244+
stats?: TCompilerStatsCompilation<T>,
245+
options?: TCompilerOptions<T>
246+
) {
247+
const moduleScope =
248+
typeof testConfig.moduleScope === "function"
249+
? testConfig.moduleScope(ms, stats, options)
250+
: ms;
251+
252+
moduleScope.COMPILER_INDEX = compilerIndex;
253+
moduleScope.NEXT_HMR = nextHmr;
254+
moduleScope.NEXT_START = nextStart;
255+
return moduleScope;
256+
}
257+
},
258+
source,
259+
dist,
260+
compilerOptions: options
261+
});
262+
};
263+
const nextHmr = async (
264+
m: any,
265+
options?: any
266+
): Promise<TCompilerStatsCompilation<T>> => {
267+
await updatePlugin.goNext();
268+
const stats = await compiler.build();
269+
if (!stats) {
270+
throw new Error("Should generate stats during build");
271+
}
272+
const jsonStats = stats.toJson({
273+
// errorDetails: true
274+
});
275+
const compilerOptions = compiler.getOptions();
276+
277+
const updateIndex = updatePlugin.getUpdateIndex();
278+
await checkArrayExpectation(
279+
source,
280+
jsonStats,
281+
"error",
282+
`errors${updateIndex}`,
283+
"Error",
284+
compilerOptions
285+
);
286+
await checkArrayExpectation(
287+
source,
288+
jsonStats,
289+
"warning",
290+
`warnings${updateIndex}`,
291+
"Warning",
292+
compilerOptions
293+
);
294+
295+
const updatedModules = await m.hot.check(options || true);
296+
if (!updatedModules) {
297+
throw new Error("No update available");
298+
}
299+
300+
return jsonStats as StatsCompilation;
301+
};
302+
303+
const nextStart = async (): Promise<TCompilerStatsCompilation<T>> => {
304+
await compiler.close();
305+
compiler.createCompiler();
306+
await updatePlugin.goNext();
307+
const stats = await compiler.build();
308+
if (!stats) {
309+
throw new Error("Should generate stats during build");
310+
}
311+
const jsonStats = stats.toJson({
312+
// errorDetails: true
313+
});
314+
const compilerOptions = compiler.getOptions();
315+
316+
const updateIndex = updatePlugin.getUpdateIndex();
317+
await checkArrayExpectation(
318+
source,
319+
jsonStats,
320+
"error",
321+
`errors${updateIndex}`,
322+
"Error",
323+
compilerOptions
324+
);
325+
await checkArrayExpectation(
326+
source,
327+
jsonStats,
328+
"warning",
329+
`warnings${updateIndex}`,
330+
"Warning",
331+
compilerOptions
332+
);
333+
env.it(
334+
`NEXT_START run with compilerIndex==${compilerIndex + 1}`,
335+
async () => {
336+
if (compilerIndex > MAX_COMPILER_INDEX) {
337+
throw new Error(
338+
"NEXT_START has been called more than the maximum times"
339+
);
340+
}
341+
compilerIndex++;
342+
return getWebRunner().run(file);
343+
}
344+
);
345+
return jsonStats as StatsCompilation;
346+
};
347+
348+
return getWebRunner();
349+
}

0 commit comments

Comments
 (0)