Skip to content

Commit bf3b20a

Browse files
tests: fix coverage for babel-cli (#14965)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
1 parent 6be6e04 commit bf3b20a

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

packages/babel-cli/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"@babel/helper-fixtures": "workspace:^",
4545
"@types/fs-readdir-recursive": "^1.1.0",
4646
"@types/glob": "^7.2.0",
47-
"rimraf": "^3.0.0"
47+
"rimraf": "^3.0.0",
48+
"semver": "^6.3.0"
4849
},
4950
"bin": {
5051
"babel": "./bin/babel.js",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// We need to gracefully exit rather than just terminating spawn tests with SIGINT
2+
// to let c8 correctly report the collected coverage info.
3+
// eslint-disable-next-line no-process-exit
4+
process.on("message", () => process.exit(0));
5+
process.channel.unref();

packages/babel-cli/test/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import readdir from "fs-readdir-recursive";
22
import * as helper from "@babel/helper-fixtures";
33
import rimraf from "rimraf";
4+
import semver from "semver";
45
import child from "child_process";
56
import path from "path";
67
import fs from "fs";
@@ -150,13 +151,17 @@ const assertTest = function (stdout, stderr, opts, cwd) {
150151
}
151152
};
152153

154+
const nodeGte8 = semver.gte(process.version, "8.0.0");
155+
153156
const buildTest = function (binName, testName, opts) {
154157
const binLoc = path.join(dirname, "../lib", binName);
155158

156159
return function (callback) {
157160
saveInFiles(opts.inFiles);
158161

159-
let args = [binLoc];
162+
let args = nodeGte8
163+
? ["--require", path.join(dirname, "./exit-loader.cjs"), binLoc]
164+
: [binLoc];
160165

161166
if (binName !== "babel-external-helpers" && !opts.noDefaultPlugins) {
162167
args.push("--presets", presetLocs, "--plugins", pluginLocs);
@@ -165,7 +170,11 @@ const buildTest = function (binName, testName, opts) {
165170
args = args.concat(opts.args);
166171
const env = { ...process.env, ...opts.env };
167172

168-
const spawn = child.spawn(process.execPath, args, { env, cwd: tmpLoc });
173+
const spawn = child.spawn(process.execPath, args, {
174+
env,
175+
cwd: tmpLoc,
176+
stdio: [null, null, null, "ipc"],
177+
});
169178

170179
let stderr = "";
171180
let stdout = "";
@@ -211,7 +220,11 @@ const buildTest = function (binName, testName, opts) {
211220
spawn.stderr.pipe(executor.stdin);
212221

213222
executor.on("close", function () {
214-
setTimeout(() => spawn.kill("SIGINT"), 250);
223+
if (nodeGte8) {
224+
spawn.send("exit");
225+
} else {
226+
spawn.kill("SIGKILL");
227+
}
215228
});
216229

217230
captureOutput(executor);

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ __metadata:
214214
glob: ^7.2.0
215215
make-dir: "condition:BABEL_8_BREAKING ? : ^2.1.0"
216216
rimraf: ^3.0.0
217+
semver: ^6.3.0
217218
slash: "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0"
218219
peerDependencies:
219220
"@babel/core": ^7.0.0-0

0 commit comments

Comments
 (0)