Skip to content

Commit 9170f77

Browse files
test: cover the beforeParse error path and two DefinePlugin arms
`beforeParse` failing is what the hook's move to `AsyncSeriesHook` changed, and nothing exercised it: a case now fails one module from a `tap` and another from a `tapPromise`, which covers every line the move added to `NormalModule` and holds both to the module error the synchronous hook gave. Two more cases reach an unparsable string define and a code point above the Unicode maximum. Co-authored-by: Eshiv Pandey <[email protected]>
1 parent 31fbbaf commit 9170f77

12 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[{ moduleName: /thrown\.js$/ }, /^the tap threw before the parse$/],
5+
[{ moduleName: /rejected\.js$/ }, /^the tap rejected before the parse$/]
6+
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// `var` (not `const`) so the parser cannot fold the branch away and drop the
2+
// dependency — the modules must be built, only never executed.
3+
var never = false;
4+
5+
it("should report what a beforeParse tap failed with", () => {
6+
if (never) {
7+
require("./thrown.js");
8+
require("./rejected.js");
9+
}
10+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
module.exports = (options) =>
4+
options.cache && options.cache.type === "filesystem"
5+
? [/Pack got invalid because of write to/]
6+
: [];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "never read";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "never read";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
3+
const NormalModule = require("../../../../lib/module/NormalModule");
4+
5+
// `beforeParse` is an AsyncSeriesHook, so a tap can fail synchronously or by
6+
// rejecting. Either way the module carries the error and the build goes on.
7+
class FailBeforeParse {
8+
/**
9+
* @param {import("../../../../lib/Compiler")} compiler compiler
10+
*/
11+
apply(compiler) {
12+
compiler.hooks.compilation.tap("FailBeforeParse", (compilation) => {
13+
const hooks = NormalModule.getCompilationHooks(compilation);
14+
15+
hooks.beforeParse.tap("FailBeforeParse", (module) => {
16+
if (!/thrown\.js$/.test(module.resource)) return;
17+
throw new Error("the tap threw before the parse");
18+
});
19+
20+
hooks.beforeParse.tapPromise("FailBeforeParse", async (module) => {
21+
if (!/rejected\.js$/.test(module.resource)) return;
22+
throw new Error("the tap rejected before the parse");
23+
});
24+
});
25+
}
26+
}
27+
28+
/** @type {import("../../../../").Configuration} */
29+
module.exports = {
30+
mode: "development",
31+
optimization: { emitOnErrors: true },
32+
plugins: [new FailBeforeParse()]
33+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// a code point above the Unicode maximum is left as written: \u{110000}
12
module.exports = [ESCAPED_\u0056ALUE, ESCAPED_\u{56}ALUE];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[
5+
/DefinePlugin: failed to evaluate value for "typeof BROKEN" \(`this is \( not valid js`\)[\s\S]*Unexpected token/
6+
]
7+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(typeof BROKEN);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
module.exports = (options) =>
4+
options.cache && options.cache.type === "filesystem"
5+
? [/Pack got invalid because of write to/]
6+
: [];

0 commit comments

Comments
 (0)