Skip to content

Commit 47ce2a0

Browse files
authored
test: improve webpack test performance (#9489)
1 parent d4b8dd3 commit 47ce2a0

File tree

3 files changed

+110
-70
lines changed

3 files changed

+110
-70
lines changed

tests/webpack-test/ConfigTestCases.template.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const describeCases = config => {
198198
return;
199199
}
200200
// Wait for uncaught errors to occur
201-
setTimeout(done, 200);
201+
setTimeout(done);
202202
return;
203203
};
204204
if (config.cache) {
@@ -256,12 +256,7 @@ const describeCases = config => {
256256
require("@rspack/core")(options, (err, stats) => {
257257
deprecationTracker();
258258
if (err) return handleFatalError(err, done);
259-
const { errorsCount } = stats.toJson({
260-
all: false,
261-
modules: true,
262-
errorsCount: true
263-
});
264-
if (errorsCount === 0) {
259+
if (!stats.hasErrors()) {
265260
const infrastructureLogging = stderr.toString();
266261
if (infrastructureLogging) {
267262
return done(
@@ -334,23 +329,36 @@ const describeCases = config => {
334329
colors: false
335330
};
336331
fs.mkdirSync(outputDirectory, { recursive: true });
337-
fs.writeFileSync(
338-
path.join(outputDirectory, "stats.txt"),
339-
stats.toString(statOptions),
340-
"utf-8"
341-
);
342-
const jsonStats = stats.toJson({
343-
errorDetails: true
344-
});
345-
fs.writeFileSync(
346-
path.join(outputDirectory, "stats.json"),
347-
JSON.stringify(jsonStats, null, 2),
348-
"utf-8"
349-
);
332+
// CHANGE: no test cases use stats.txt
333+
// fs.writeFileSync(
334+
// path.join(outputDirectory, "stats.txt"),
335+
// stats.toString(statOptions),
336+
// "utf-8"
337+
// );
338+
339+
const getStatsJson = (() => {
340+
let cache = null;
341+
return () => {
342+
if (!cache) {
343+
cache = stats.toJson({
344+
errorDetails: true
345+
});
346+
}
347+
return cache;
348+
};
349+
})();
350+
// const jsonStats = getStatsJson();
351+
// CHANGE: no test cases use stats.json
352+
// fs.writeFileSync(
353+
// path.join(outputDirectory, "stats.json"),
354+
// JSON.stringify(jsonStats, null, 2),
355+
// "utf-8"
356+
// );
350357
if (
358+
fs.existsSync(path.join(testDirectory, "errors.js")) &&
351359
checkArrayExpectation(
352360
testDirectory,
353-
jsonStats,
361+
getStatsJson(),
354362
"error",
355363
"Error",
356364
done
@@ -359,9 +367,10 @@ const describeCases = config => {
359367
return;
360368
}
361369
if (
370+
fs.existsSync(path.join(testDirectory, "warnings.js")) &&
362371
checkArrayExpectation(
363372
testDirectory,
364-
jsonStats,
373+
getStatsJson(),
365374
"warning",
366375
"Warning",
367376
done
@@ -448,7 +457,6 @@ const describeCases = config => {
448457
afterEach: _afterEach,
449458
expect,
450459
jest,
451-
__STATS__: jsonStats,
452460
nsObj: m => {
453461
Object.defineProperty(m, Symbol.toStringTag, {
454462
value: "Module"
@@ -466,9 +474,9 @@ const describeCases = config => {
466474
baseModuleScope.self = globalContext;
467475
baseModuleScope.document = globalContext.document;
468476
baseModuleScope.URL = URL;
469-
if (typeof Blob !== "undefined") {
470-
baseModuleScope.Blob = Blob;
471-
}
477+
if (typeof Blob !== "undefined") {
478+
baseModuleScope.Blob = Blob;
479+
}
472480
baseModuleScope.Worker =
473481
require("./helpers/createFakeWorker")({
474482
outputDirectory
@@ -545,6 +553,13 @@ const describeCases = config => {
545553
);
546554
let esm = esmCache.get(p);
547555
if (!esm) {
556+
let moduleContext = esmContext;
557+
if (content.includes("__STATS__")) {
558+
moduleContext = vm.createContext({
559+
__STATS__: getStatsJson(),
560+
...moduleContext
561+
});
562+
}
548563
esm = new vm.SourceTextModule(content, {
549564
identifier: esmIdentifier + "-" + p,
550565
url:
@@ -641,6 +656,9 @@ const describeCases = config => {
641656
__filename: p,
642657
_globalAssign: { expect }
643658
};
659+
if (content.includes("__STATS__")) {
660+
moduleScope.__STATS__ = getStatsJson();
661+
}
644662
if (testConfig.moduleScope) {
645663
testConfig.moduleScope(moduleScope);
646664
}
@@ -701,7 +719,7 @@ const describeCases = config => {
701719
}
702720
// give a free pass to compilation that generated an error
703721
if (
704-
!jsonStats.errors.length &&
722+
!stats.hasErrors() &&
705723
filesCount !== optionsArr.length
706724
) {
707725
return done(

tests/webpack-test/TestCases.template.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -345,28 +345,38 @@ const describeCases = config => {
345345
}
346346
compiler.close(err => {
347347
if (err) return done(err);
348-
const statOptions = {
349-
preset: "verbose",
350-
colors: false,
351-
modules: true,
352-
reasonsSpace: 1000
353-
};
354348
fs.mkdirSync(outputDirectory, { recursive: true });
355-
fs.writeFileSync(
356-
path.join(outputDirectory, "stats.txt"),
357-
stats.toString(statOptions),
358-
"utf-8"
359-
);
360-
const jsonStats = stats.toJson({
361-
errorDetails: true,
362-
modules: false,
363-
assets: false,
364-
chunks: false
365-
});
349+
// CHANGE: no test cases use stats.txt
350+
// const statOptions = {
351+
// preset: "verbose",
352+
// colors: false,
353+
// modules: true,
354+
// reasonsSpace: 1000
355+
// };
356+
// fs.writeFileSync(
357+
// path.join(outputDirectory, "stats.txt"),
358+
// stats.toString(statOptions),
359+
// "utf-8"
360+
// );
361+
const getStatsJson = (() => {
362+
let cache = null;
363+
return () => {
364+
if (!cache) {
365+
cache = stats.toJson({
366+
errorDetails: true,
367+
modules: false,
368+
assets: false,
369+
chunks: false
370+
});
371+
}
372+
return cache;
373+
};
374+
})();
366375
if (
376+
fs.existsSync(path.join(testDirectory, "errors.js")) &&
367377
checkArrayExpectation(
368378
testDirectory,
369-
jsonStats,
379+
getStatsJson(),
370380
"error",
371381
"Error",
372382
done
@@ -375,9 +385,10 @@ const describeCases = config => {
375385
return;
376386
}
377387
if (
388+
fs.existsSync(path.join(testDirectory, "warnings.js")) &&
378389
checkArrayExpectation(
379390
testDirectory,
380-
jsonStats,
391+
getStatsJson(),
381392
"warning",
382393
"Warning",
383394
done

tests/webpack-test/WatchTestCases.template.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -229,39 +229,50 @@ const describeCases = config => {
229229
run.done = true;
230230
run.stats = stats;
231231
if (err) return compilationFinished(err);
232-
const statOptions = {
233-
preset: "verbose",
234-
cached: true,
235-
cachedAssets: true,
236-
cachedModules: true,
237-
colors: false
238-
};
239232
fs.mkdirSync(outputDirectory, { recursive: true });
240-
fs.writeFileSync(
241-
path.join(
242-
outputDirectory,
243-
`stats.${runs[runIdx] && runs[runIdx].name}.txt`
244-
),
245-
stats.toString(statOptions),
246-
"utf-8"
247-
);
248-
const jsonStats = stats.toJson({
249-
errorDetails: true
250-
});
233+
// CHANGE: no test cases use stats.txt
234+
// const statOptions = {
235+
// preset: "verbose",
236+
// cached: true,
237+
// cachedAssets: true,
238+
// cachedModules: true,
239+
// colors: false
240+
// };
241+
// fs.writeFileSync(
242+
// path.join(
243+
// outputDirectory,
244+
// `stats.${runs[runIdx] && runs[runIdx].name}.txt`
245+
// ),
246+
// stats.toString(statOptions),
247+
// "utf-8"
248+
// );
249+
const getStatsJson = (() => {
250+
let cache = null;
251+
return () => {
252+
if (!cache) {
253+
cache = stats.toJson({
254+
errorDetails: true
255+
});
256+
}
257+
return cache;
258+
};
259+
})();
251260
if (
261+
fs.existsSync(path.join(testDirectory, run.name, "errors.js")) &&
252262
checkArrayExpectation(
253263
path.join(testDirectory, run.name),
254-
jsonStats,
264+
getStatsJson(),
255265
"error",
256266
"Error",
257267
compilationFinished
258268
)
259269
)
260270
return;
261271
if (
272+
fs.existsSync(path.join(testDirectory, run.name, "warnings.js")) &&
262273
checkArrayExpectation(
263274
path.join(testDirectory, run.name),
264-
jsonStats,
275+
getStatsJson(),
265276
"warning",
266277
"Warning",
267278
compilationFinished
@@ -328,7 +339,7 @@ const describeCases = config => {
328339
p,
329340
run.it,
330341
run.name,
331-
jsonStats,
342+
content.includes("__STATS__") ? getStatsJson() : undefined,
332343
state,
333344
expect,
334345
globalContext,
@@ -382,7 +393,7 @@ const describeCases = config => {
382393
tempDirectory,
383394
false
384395
);
385-
}, 1500);
396+
});
386397
} else {
387398
const deprecations = deprecationTracker();
388399
if (
@@ -406,7 +417,7 @@ const describeCases = config => {
406417
compilationFinished();
407418
}
408419
);
409-
}, 300);
420+
});
410421
},
411422
45000
412423
);
@@ -420,7 +431,7 @@ const describeCases = config => {
420431
run.getNumberOfTests = getNumberOfTests;
421432
it(`${run.name} should allow to read stats`, done => {
422433
if (run.stats) {
423-
run.stats.toString({ all: true });
434+
// run.stats.toString({ all: true });
424435
run.stats = undefined;
425436
}
426437
done();

0 commit comments

Comments
 (0)