Skip to content

Commit

Permalink
test: easier way to write hmr test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Dec 30, 2024
1 parent b09af3d commit c4ca64d
Show file tree
Hide file tree
Showing 19 changed files with 271 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,18 @@ ${tooMuch.map(item => `${explain(item)}`).join("\n\n")}`);
return diff.join("\n\n");
};

module.exports = function checkArrayExpectation(
module.exports = async function checkArrayExpectation(
testDirectory,
object,
kind,
filename,
upperCaseKind,
done
) {
if (!done) {
done = upperCaseKind;
upperCaseKind = filename;
filename = `${kind}s`;
}
const usePromise = typeof done === "function";
done = typeof done === "function" ? done : error => {
throw error
};
let array = object[`${kind}s`];
if (Array.isArray(array)) {
if (kind === "warning") {
Expand Down
36 changes: 15 additions & 21 deletions packages/rspack-test-tools/src/processor/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,21 @@ export class BasicProcessor<T extends ECompilerType> implements ITestProcessor {
warnings.push(...jsonStats.warnings);
}
}
await new Promise<void>((resolve, reject) => {
checkArrayExpectation(
context.getSource(),
{ errors },
"error",
"Error",
reject
);
resolve();
});

await new Promise<void>((resolve, reject) => {
checkArrayExpectation(
context.getSource(),
{ warnings },
"warning",
"Warning",
reject
);
resolve();
});
await checkArrayExpectation(
context.getSource(),
{ errors },
"error",
"errors",
"Error"
);

await checkArrayExpectation(
context.getSource(),
{ warnings },
"warning",
"warnings",
"Warning"
);

// clear error if checked
if (fs.existsSync(context.getSource("errors.js"))) {
Expand Down
9 changes: 6 additions & 3 deletions packages/rspack-test-tools/src/processor/hot-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,12 @@ export class HotSnapshotProcessor<
runtime: string[];
}> = [];
const hotUpdateManifest: Array<{ name: string; content: string }> = [];
const changedFiles: string[] = this.updateOptions.changedFiles.map(
(i: string) => escapeSep(path.relative(context.getSource(), i))
);
const changedFiles: string[] =
this.updateOptions.updateIndex === 0
? []
: this.updateOptions.changedFiles.map((i: string) =>
escapeSep(path.relative(context.getSource(), i))
);
changedFiles.sort();

const hashes: Record<string, string> = {
Expand Down
45 changes: 5 additions & 40 deletions packages/rspack-test-tools/src/processor/hot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "node:path";
import { type Compiler, rspack } from "@rspack/core";
import { rspack } from "@rspack/core";

import { TestHotUpdatePlugin } from "../helper/plugins";
import {
ECompilerType,
type ITestContext,
Expand Down Expand Up @@ -120,42 +121,9 @@ export class HotProcessor<T extends ECompilerType> extends BasicProcessor<T> {
if (this._hotOptions.compilerType === ECompilerType.Rspack) {
options.plugins ??= [];
(options as TCompilerOptions<ECompilerType.Rspack>).plugins!.push(
new rspack.HotModuleReplacementPlugin()
new rspack.HotModuleReplacementPlugin(),
new TestHotUpdatePlugin(this.updateOptions)
);
(options as TCompilerOptions<ECompilerType.Rspack>).plugins!.push({
apply(compiler: Compiler) {
compiler.hooks.compilation.tap("HMR_TEST_PLUGIN", compilation => {
compilation.hooks.additionalTreeRuntimeRequirements.tap(
"HMR_TEST_PLUGIN",
(chunk, set) => {
set.add(compiler.webpack.RuntimeGlobals.moduleCache);
}
);
compilation.hooks.runtimeModule.tap(
"HMR_TEST_PLUGIN",
(module: any, set) => {
if (
module.constructorName ===
"DefinePropertyGettersRuntimeModule"
) {
module.source.source = Buffer.from(
`
__webpack_require__.d = function (exports, definition) {
for (var key in definition) {
if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
Object.defineProperty(exports, key, { configurable: true, enumerable: true, get: definition[key] });
}
}
};
`,
"utf-8"
);
}
}
);
});
}
});
}
return options;
}
Expand All @@ -181,10 +149,7 @@ export class HotProcessor<T extends ECompilerType> extends BasicProcessor<T> {
test: /\.(js|css|json)/,
use: [
{
loader: path.resolve(
__dirname,
"../helper/legacy/fake-update-loader.js"
),
loader: path.resolve(__dirname, "../helper/loaders/hot-update.js"),
options: this.updateOptions
}
]
Expand Down
34 changes: 14 additions & 20 deletions packages/rspack-test-tools/src/processor/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,21 @@ export class WatchProcessor<
warnings.push(...jsonStats.warnings);
}
}
await new Promise<void>((resolve, reject) => {
checkArrayExpectation(
path.join(context.getSource(), this._watchOptions.stepName),
{ errors },
"error",
"Error",
reject
);
resolve();
});
await checkArrayExpectation(
path.join(context.getSource(), this._watchOptions.stepName),
{ errors },
"error",
"errors",
"Error"
);

await new Promise<void>((resolve, reject) => {
checkArrayExpectation(
path.join(context.getSource(), this._watchOptions.stepName),
{ warnings },
"warning",
"Warning",
reject
);
resolve();
});
await checkArrayExpectation(
path.join(context.getSource(), this._watchOptions.stepName),
{ warnings },
"warning",
"warnings",
"Warning"
);

// clear error if checked
if (fs.existsSync(context.getSource("errors.js"))) {
Expand Down
28 changes: 8 additions & 20 deletions packages/rspack-test-tools/src/runner/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,19 @@ export class CacheRunnerFactory<
// errorDetails: true
});

checkArrayExpectation(
await checkArrayExpectation(
source,
jsonStats,
"error",
`errors${hotUpdateContext.updateIndex}`,
"Error",
function (err: any) {
throw err;
}
"Error"
);
checkArrayExpectation(
await checkArrayExpectation(
source,
jsonStats,
"warning",
`warnings${hotUpdateContext.updateIndex}`,
"Warning",
function (err: any) {
throw err;
}
"Warning"
);

const updatedModules = await m.hot.check(options || true);
Expand Down Expand Up @@ -123,25 +117,19 @@ export class CacheRunnerFactory<
// errorDetails: true
});

checkArrayExpectation(
await checkArrayExpectation(
source,
jsonStats,
"error",
`errors${hotUpdateContext.updateIndex}`,
"Error",
function (err: any) {
throw err;
}
"Error"
);
checkArrayExpectation(
await checkArrayExpectation(
source,
jsonStats,
"warning",
`warnings${hotUpdateContext.updateIndex}`,
"Warning",
function (err: any) {
throw err;
}
"Warning"
);
env.it(
`NEXT_START run with compilerIndex==${compilerIndex + 1}`,
Expand Down
Loading

0 comments on commit c4ca64d

Please sign in to comment.