Skip to content

Commit 740cd96

Browse files
authored
test: run webpack stats cases with test tools (#11791)
* test: run webpack stats cases with test tools * test: run webpack stats cases with test tools * test: run webpack stats cases with test tools
1 parent 62ba358 commit 740cd96

File tree

1,063 files changed

+2085
-4332
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,063 files changed

+2085
-4332
lines changed

packages/rspack-test-tools/src/case/stats-output.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ function defaultOptions(
7979
index: number,
8080
context: ITestContext
8181
): TCompilerOptions<ECompilerType.Rspack> {
82-
if (fs.existsSync(path.join(context.getSource(), "rspack.config.js"))) {
82+
if (
83+
fs.existsSync(path.join(context.getSource(), "rspack.config.js")) ||
84+
fs.existsSync(path.join(context.getSource(), "webpack.config.js"))
85+
) {
8386
return {
8487
experiments: {
8588
css: true,
@@ -217,7 +220,8 @@ async function check(
217220
// CHANGE: The time unit display in Rspack is second
218221
.replace(/[.0-9]+(\s?s)/g, "X$1")
219222
// CHANGE: Replace bundle size, since bundle sizes may differ between platforms
220-
.replace(/[0-9]+\.?[0-9]+ KiB/g, "xx KiB");
223+
.replace(/[0-9]+\.?[0-9]+ KiB/g, "xx KiB")
224+
.replace(/[0-9]+ ms/g, "xx ms");
221225
}
222226

223227
const snapshotPath = path.isAbsolute(snapshot)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @ts-nocheck
2+
3+
module.exports = class LogTestPlugin {
4+
constructor(noTraced) {
5+
this.noTraced = noTraced;
6+
}
7+
apply(compiler) {
8+
const logSome = logger => {
9+
logger.group("Group");
10+
if (!this.noTraced) {
11+
logger.error("Error");
12+
logger.warn("Warning");
13+
}
14+
logger.info("Info");
15+
logger.log("Log");
16+
logger.debug("Debug");
17+
logger.groupCollapsed("Collapsed group");
18+
logger.log("Log inside collapsed group");
19+
logger.group("Inner group");
20+
logger.log("Inner inner message");
21+
logger.groupEnd();
22+
logger.groupEnd();
23+
logger.log("Log");
24+
logger.groupEnd();
25+
logger.log("End");
26+
};
27+
logSome(compiler.getInfrastructureLogger("LogTestPlugin"));
28+
compiler.hooks.compilation.tap("LogTestPlugin", compilation => {
29+
const logger = compilation.getLogger("LogTestPlugin");
30+
logSome(logger);
31+
32+
const otherLogger = compilation.getLogger("LogOtherTestPlugin");
33+
otherLogger.debug("debug message only");
34+
});
35+
}
36+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @ts-nocheck
2+
const diff = require("jest-diff").diff;
3+
const { stripVTControlCharacters: stripAnsi } = require("node:util");
4+
5+
const processStats = str => {
6+
return str.trim().split('\n').map(i => i.trim()).join('\n').replace(/\d+(\.\d+)?/g, 'XX').replace(/"/g, "");
7+
};
8+
const webpackStats = require('../__snapshots__/StatsTestCases.basictest.js.snap.webpack');
9+
10+
module.exports = (rawStats, name) => {
11+
const key = `StatsTestCases should print correct stats for ${name} 1`;
12+
const res = stripAnsi(
13+
diff(processStats(rawStats), processStats(webpackStats[key] || ''), { expand: false, contextLines: 0 })
14+
);
15+
return res;
16+
};
17+

packages/rspack-test-tools/src/test/tester.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,15 @@ export class Tester implements ITester {
109109
env: ITestEnv,
110110
methods: Array<"run" | "check">
111111
) {
112-
for (const i of methods) {
113-
if (typeof step[i] === "function") {
114-
await step[i]!(env, this.context);
112+
try {
113+
for (const i of methods) {
114+
if (typeof step[i] === "function") {
115+
await step[i]!(env, this.context);
116+
}
115117
}
118+
} catch (e) {
119+
console.error(e);
120+
throw e;
116121
}
117122
}
118123
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const { createFsFromVolume, Volume } = require("memfs");
2+
3+
module.exports = {
4+
description: "should create JSON of children stats",
5+
options(context) {
6+
return [
7+
{
8+
context: __dirname,
9+
entry: "../fixtures/a"
10+
},
11+
{
12+
context: __dirname,
13+
entry: "../fixtures/b"
14+
}
15+
];
16+
},
17+
compiler(context, compiler) {
18+
compiler.outputFileSystem = createFsFromVolume(new Volume());
19+
},
20+
async build(context, compiler) {
21+
return new Promise((resolve, reject) => {
22+
compiler.run((err, stats) => {
23+
if (err) return reject(err);
24+
try {
25+
const statsObject = stats.toJson();
26+
expect(statsObject).toEqual(
27+
expect.objectContaining({ children: expect.any(Array) })
28+
);
29+
expect(statsObject.children).toHaveLength(2);
30+
resolve();
31+
} catch (e) {
32+
reject(e);
33+
}
34+
});
35+
});
36+
}
37+
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module.exports = {
2+
description: "should contain additional chunks",
3+
options(context) {
4+
return {
5+
context: context.getSource(),
6+
entry: {
7+
entryA: "./fixtures/a",
8+
entryB: "./fixtures/chunk-b"
9+
}
10+
};
11+
},
12+
async check(stats) {
13+
expect(
14+
stats.toJson({
15+
all: false,
16+
errorsCount: true,
17+
chunkGroups: true
18+
})
19+
).toMatchInlineSnapshot(`
20+
Object {
21+
errorsCount: 0,
22+
namedChunkGroups: Object {
23+
chunkB: Object {
24+
assets: Array [
25+
Object {
26+
name: chunkB.js,
27+
size: 143,
28+
},
29+
],
30+
assetsSize: 143,
31+
auxiliaryAssets: undefined,
32+
auxiliaryAssetsSize: undefined,
33+
childAssets: undefined,
34+
children: undefined,
35+
chunks: Array [
36+
513,
37+
],
38+
filteredAssets: 0,
39+
name: chunkB,
40+
},
41+
entryA: Object {
42+
assets: Array [
43+
Object {
44+
name: entryA.js,
45+
size: 204,
46+
},
47+
],
48+
assetsSize: 204,
49+
auxiliaryAssets: undefined,
50+
auxiliaryAssetsSize: undefined,
51+
childAssets: undefined,
52+
children: undefined,
53+
chunks: Array [
54+
759,
55+
],
56+
filteredAssets: 0,
57+
name: entryA,
58+
},
59+
entryB: Object {
60+
assets: Array [
61+
Object {
62+
name: entryB.js,
63+
size: 3206,
64+
},
65+
],
66+
assetsSize: 3206,
67+
auxiliaryAssets: undefined,
68+
auxiliaryAssetsSize: undefined,
69+
childAssets: undefined,
70+
children: undefined,
71+
chunks: Array [
72+
706,
73+
],
74+
filteredAssets: 0,
75+
name: entryB,
76+
},
77+
},
78+
}
79+
`);
80+
}
81+
};
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
module.exports = {
2+
description: "should contain assets",
3+
options(context) {
4+
return {
5+
context: context.getSource(),
6+
entry: {
7+
entryA: "./fixtures/a",
8+
entryB: "./fixtures/chunk-b"
9+
}
10+
};
11+
},
12+
async check(stats) {
13+
expect(
14+
stats.toJson({
15+
all: false,
16+
errorsCount: true,
17+
assets: true
18+
})
19+
).toMatchInlineSnapshot(`
20+
Object {
21+
assets: Array [
22+
Object {
23+
auxiliaryChunkIdHints: Array [],
24+
auxiliaryChunkNames: Array [],
25+
cached: false,
26+
chunkIdHints: Array [],
27+
chunkNames: Array [
28+
entryB,
29+
],
30+
emitted: true,
31+
info: Object {
32+
chunkhash: Array [],
33+
contenthash: Array [],
34+
fullhash: Array [],
35+
isOverSizeLimit: false,
36+
javascriptModule: false,
37+
minimized: true,
38+
related: Object {},
39+
},
40+
name: entryB.js,
41+
size: 3206,
42+
type: asset,
43+
},
44+
Object {
45+
auxiliaryChunkIdHints: Array [],
46+
auxiliaryChunkNames: Array [],
47+
cached: false,
48+
chunkIdHints: Array [],
49+
chunkNames: Array [
50+
entryA,
51+
],
52+
emitted: true,
53+
info: Object {
54+
chunkhash: Array [],
55+
contenthash: Array [],
56+
fullhash: Array [],
57+
isOverSizeLimit: false,
58+
javascriptModule: false,
59+
minimized: true,
60+
related: Object {},
61+
},
62+
name: entryA.js,
63+
size: 204,
64+
type: asset,
65+
},
66+
Object {
67+
auxiliaryChunkIdHints: Array [],
68+
auxiliaryChunkNames: Array [],
69+
cached: false,
70+
chunkIdHints: Array [],
71+
chunkNames: Array [
72+
chunkB,
73+
],
74+
emitted: true,
75+
info: Object {
76+
chunkhash: Array [],
77+
contenthash: Array [],
78+
fullhash: Array [],
79+
isOverSizeLimit: false,
80+
javascriptModule: false,
81+
minimized: true,
82+
related: Object {},
83+
},
84+
name: chunkB.js,
85+
size: 143,
86+
type: asset,
87+
},
88+
],
89+
assetsByChunkName: Object {
90+
chunkB: Array [
91+
chunkB.js,
92+
],
93+
entryA: Array [
94+
entryA.js,
95+
],
96+
entryB: Array [
97+
entryB.js,
98+
],
99+
},
100+
errorsCount: 0,
101+
filteredAssets: undefined,
102+
}
103+
`);
104+
}
105+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = {
2+
description: "should print env string in stats",
3+
options(context) {
4+
return {
5+
context: context.getSource(),
6+
entry: "./fixtures/a"
7+
};
8+
},
9+
async check(stats) {
10+
expect(
11+
stats.toString({
12+
all: false,
13+
env: true,
14+
_env: "production"
15+
})
16+
).toBe('Environment (--env): "production"');
17+
expect(
18+
stats.toString({
19+
all: false,
20+
env: true,
21+
_env: {
22+
prod: ["foo", "bar"],
23+
baz: true
24+
}
25+
})
26+
).toBe(
27+
"Environment (--env): {\n" +
28+
' "prod": [\n' +
29+
' "foo",\n' +
30+
' "bar"\n' +
31+
" ],\n" +
32+
' "baz": true\n' +
33+
"}"
34+
);
35+
}
36+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
description: "should omit all properties with all false",
3+
options(context) {
4+
return {
5+
context: context.getSource(),
6+
entry: "./fixtures/a"
7+
};
8+
},
9+
async check(stats) {
10+
expect(stats.toJson({
11+
all: false
12+
})).toEqual({});
13+
}
14+
};

0 commit comments

Comments
 (0)