Skip to content

Commit 0c2aa2a

Browse files
test: clean up after a universal build that fails to set up
The helper made its directory and started a watcher before anything was handed back, so an error on the way left both behind. The watcher is now closed either way, and the directory removed when nothing is returned to remove it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
1 parent c82c422 commit 0c2aa2a

1 file changed

Lines changed: 50 additions & 32 deletions

File tree

‎test/universal-target.test.js‎

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,56 @@ async function build(target, hot, entry) {
4646
path.join(fs.realpathSync.native(os.tmpdir()), "wdm-universal-"),
4747
);
4848

49-
fs.writeFileSync(path.join(dir, "app.js"), APP);
50-
fs.writeFileSync(path.join(dir, "stand-in.js"), STAND_IN);
51-
52-
const compiler = webpack({
53-
mode: "development",
54-
devtool: false,
55-
context: dir,
56-
entry: entry ? entry(dir) : "./app.js",
57-
target,
58-
output: { path: path.join(dir, "dist") },
59-
infrastructureLogging: { level: "none" },
60-
stats: "none",
61-
});
62-
const instance = middleware(compiler, { hot: hot(dir), writeToDisk: true });
63-
64-
await new Promise((resolve) => {
65-
instance.waitUntilValid(resolve);
66-
});
67-
68-
const errors = /** @type {EXPECTED_ANY} */ (instance.context.stats)
69-
.toJson({ all: false, errors: true })
70-
.errors.map((/** @type {EXPECTED_ANY} */ item) => item.message);
71-
72-
await new Promise((resolve) => {
73-
instance.close(resolve);
74-
});
75-
76-
return {
77-
errors,
78-
bundle: path.join(dir, "dist", "main.mjs"),
79-
close: () => fs.rmSync(dir, { recursive: true, force: true }),
80-
};
49+
const remove = () => fs.rmSync(dir, { recursive: true, force: true });
50+
/** @type {EXPECTED_ANY} */
51+
let instance;
52+
// The watcher, closed before the directory it watches can be removed.
53+
const closeWatcher = () =>
54+
instance
55+
? new Promise((resolve) => {
56+
instance.close(resolve);
57+
})
58+
: Promise.resolve();
59+
60+
try {
61+
fs.writeFileSync(path.join(dir, "app.js"), APP);
62+
fs.writeFileSync(path.join(dir, "stand-in.js"), STAND_IN);
63+
64+
const compiler = webpack({
65+
mode: "development",
66+
devtool: false,
67+
context: dir,
68+
entry: entry ? entry(dir) : "./app.js",
69+
target,
70+
output: { path: path.join(dir, "dist") },
71+
infrastructureLogging: { level: "none" },
72+
stats: "none",
73+
});
74+
75+
instance = middleware(compiler, { hot: hot(dir), writeToDisk: true });
76+
77+
await new Promise((resolve) => {
78+
instance.waitUntilValid(resolve);
79+
});
80+
81+
const errors = /** @type {EXPECTED_ANY} */ (instance.context.stats)
82+
.toJson({ all: false, errors: true })
83+
.errors.map((/** @type {EXPECTED_ANY} */ item) => item.message);
84+
85+
await closeWatcher();
86+
87+
return {
88+
errors,
89+
bundle: path.join(dir, "dist", "main.mjs"),
90+
close: remove,
91+
};
92+
} catch (error) {
93+
// Nothing is handed back to clean up after, so it is done here.
94+
await closeWatcher();
95+
remove();
96+
97+
throw error;
98+
}
8199
}
82100

83101
/** @type {[string, (dir: string) => EXPECTED_ANY, ((dir: string) => EXPECTED_ANY) | undefined, RegExp][]} */

0 commit comments

Comments
 (0)