Skip to content

Commit 96eff54

Browse files
test: cover addLazyRuntimeModule with config cases
Main removed test/Compilation.unittest.js while converting unit tests to config cases; these drive the same two paths through a real build.
1 parent 28a7f50 commit 96eff54

6 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = [[/runtime module load failed/]];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should surface a failing runtime module load as a build error", () => {
2+
// the build error is asserted by errors.js
3+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
const PLUGIN_NAME = "AddLazyRuntimeModuleErrorTestPlugin";
4+
5+
/** @type {import("../../../../types").Configuration} */
6+
module.exports = {
7+
plugins: [
8+
{
9+
apply(compiler) {
10+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
11+
compilation.hooks.additionalTreeRuntimeRequirements.tap(
12+
PLUGIN_NAME,
13+
(chunk) => {
14+
compilation.addLazyRuntimeModule(
15+
chunk,
16+
() => Promise.reject(new Error("runtime module load failed")),
17+
(Ctor) => new Ctor()
18+
);
19+
}
20+
);
21+
});
22+
}
23+
}
24+
]
25+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
const RuntimeModule = require("../../../../lib/RuntimeModule");
4+
5+
// Loaded through `addLazyRuntimeModule`, so it must reach the chunk's runtime.
6+
class MarkerRuntimeModule extends RuntimeModule {
7+
constructor() {
8+
super("lazy marker");
9+
}
10+
11+
/**
12+
* @returns {string} runtime code
13+
*/
14+
generate() {
15+
return 'globalThis.__lazyRuntimeMarker = "attached";';
16+
}
17+
}
18+
19+
module.exports = MarkerRuntimeModule;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should attach a runtime module queued through addLazyRuntimeModule", () => {
2+
expect(globalThis.__lazyRuntimeMarker).toBe("attached");
3+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
const PLUGIN_NAME = "AddLazyRuntimeModuleTestPlugin";
4+
5+
/** @type {import("../../../../types").Configuration} */
6+
module.exports = {
7+
plugins: [
8+
{
9+
apply(compiler) {
10+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
11+
compilation.hooks.additionalTreeRuntimeRequirements.tap(
12+
PLUGIN_NAME,
13+
(chunk) => {
14+
compilation.addLazyRuntimeModule(
15+
chunk,
16+
() => Promise.resolve(require("./MarkerRuntimeModule")),
17+
(MarkerRuntimeModule) => new MarkerRuntimeModule()
18+
);
19+
}
20+
);
21+
});
22+
}
23+
}
24+
]
25+
};

0 commit comments

Comments
 (0)