From ebb85fe5bf08496263c85410d1cc81918481ba45 Mon Sep 17 00:00:00 2001 From: jinrui Date: Fri, 27 Dec 2024 15:56:14 +0800 Subject: [PATCH] test: refresh modified file mtime when run NEXT_START (#8863) * test: refresh modified file mtime when run NEXT_START * fix: comment --- .../src/helper/util/refreshModifyTime.ts | 6 ++++++ packages/rspack-test-tools/src/processor/cache.ts | 3 ++- packages/rspack-test-tools/src/runner/cache.ts | 13 +++++++++++++ .../tests/cacheCases/snapshot/basic/index.js | 8 -------- 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 packages/rspack-test-tools/src/helper/util/refreshModifyTime.ts diff --git a/packages/rspack-test-tools/src/helper/util/refreshModifyTime.ts b/packages/rspack-test-tools/src/helper/util/refreshModifyTime.ts new file mode 100644 index 000000000000..bcb2db322d47 --- /dev/null +++ b/packages/rspack-test-tools/src/helper/util/refreshModifyTime.ts @@ -0,0 +1,6 @@ +import { readFile, writeFile } from "fs-extra"; + +export async function refreshModifyTime(file: string) { + const data = await readFile(file); + await writeFile(file, data); +} diff --git a/packages/rspack-test-tools/src/processor/cache.ts b/packages/rspack-test-tools/src/processor/cache.ts index 6d9caba1609c..99aba3f9a9ce 100644 --- a/packages/rspack-test-tools/src/processor/cache.ts +++ b/packages/rspack-test-tools/src/processor/cache.ts @@ -95,7 +95,8 @@ export class CacheProcessor extends BasicProcessor { ): TCompilerOptions { const options = { context: context.getSource(), - mode: "development", + mode: "production", + cache: true, devtool: false, output: { path: context.getDist(), diff --git a/packages/rspack-test-tools/src/runner/cache.ts b/packages/rspack-test-tools/src/runner/cache.ts index 3acea9e7ba52..127cee52dabd 100644 --- a/packages/rspack-test-tools/src/runner/cache.ts +++ b/packages/rspack-test-tools/src/runner/cache.ts @@ -1,6 +1,7 @@ import type { StatsCompilation } from "@rspack/core"; import checkArrayExpectation from "../helper/legacy/checkArrayExpectation"; +import { refreshModifyTime } from "../helper/util/refreshModifyTime"; import { type ECompilerType, EDocumentType, @@ -13,6 +14,8 @@ import { import { BasicRunnerFactory } from "./basic"; import { WebRunner } from "./runner/web"; +const MAX_COMPILER_INDEX = 100; + export class CacheRunnerFactory< T extends ECompilerType > extends BasicRunnerFactory { @@ -105,6 +108,11 @@ export class CacheRunnerFactory< await compiler.close(); compiler.createCompiler(); + await Promise.all( + hotUpdateContext.changedFiles.map(async file => { + await refreshModifyTime(file); + }) + ); hotUpdateContext.changedFiles = []; hotUpdateContext.updateIndex++; const stats = await compiler.build(); @@ -138,6 +146,11 @@ export class CacheRunnerFactory< env.it( `NEXT_START run with compilerIndex==${compilerIndex + 1}`, async () => { + if (compilerIndex > MAX_COMPILER_INDEX) { + throw new Error( + "NEXT_START has been called more than the maximum times" + ); + } compilerIndex++; return getWebRunner().run(file); } diff --git a/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/index.js b/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/index.js index fa76f1e562dd..857fb08c5cdc 100644 --- a/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/index.js +++ b/packages/rspack-test-tools/tests/cacheCases/snapshot/basic/index.js @@ -10,12 +10,4 @@ it("should store and resume asset parser and generator states", async () => { if (COMPILER_INDEX == 1) { expect(value).toBe(3); } - - // expect(value).toBe(1); - // await NEXT_START(); - // } - // if (COMPILER_INDEX == 1) { - - // expect(value).toBe(2); - // } });