Skip to content

Commit aa1e604

Browse files
committed
fix(core): make @rstest/core tree-shakeable and fix runCli changed flag
- Convert the worker `setup` side-effect module into an explicit `installGracefulExit()` call so `@rstest/core` is safe to mark `sideEffects: false`. Tree-shaking consumers can now drop unused `loadConfig`/`mergeRstestConfig`/`mergeProjectConfig` together with the `@rsbuild/core` -> `@rspack/core` graph they value-import, while worker entries still opt in to the SIGTERM profiling handler. - Drop an explicit `changed: false` before building `runCli()` options (mirrors `toCommonOptions`), so `isRelatedRun` no longer treats it as changed-mode and `runCli({ changed: false })` runs the full suite.
1 parent e29fcdf commit aa1e604

5 files changed

Lines changed: 35 additions & 16 deletions

File tree

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"directory": "packages/core"
1919
},
2020
"license": "MIT",
21+
"sideEffects": false,
2122
"type": "module",
2223
"exports": {
2324
".": {

packages/core/src/api/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,14 @@ export async function runCli(
587587
const { _: positionals, ...commonOptions } = argv;
588588
const filters = positionals ?? [];
589589

590+
// `isRelatedRun` treats any defined `changed` as changed-mode. An embedder
591+
// forwarding parsed argv with boolean defaults (`changed: false`) would then
592+
// wrongly run only git-changed tests / reject positional filters, so drop the
593+
// explicit `false` here the same way `toCommonOptions` does for `run()`.
594+
if (commonOptions.changed === false) {
595+
commonOptions.changed = undefined;
596+
}
597+
590598
return executeHostSafeRun(async () => {
591599
// Match the CLI's environment setup so workers (spawned per run) observe
592600
// `NODE_ENV=test` / `RSTEST=true`. Run it *inside* the guarded section so the

packages/core/src/runtime/worker/globalSetupWorker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import './setup';
21
import { install } from 'source-map-support';
32
import type { FormattedError } from '../../types';
43
import { color } from '../../utils/logger';
54
import { formatTestError } from '../util';
5+
import { installGracefulExit } from './setup';
6+
7+
installGracefulExit();
68

79
let teardownCallbacks: (() => Promise<void> | void)[] = [];
810
// Track environment variable changes

packages/core/src/runtime/worker/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import './setup';
21
import { isMainThread } from 'node:worker_threads';
32
import {
43
isWorkerRequestEnvelope,
@@ -10,6 +9,9 @@ import {
109
import { ENV } from '../../utils/env';
1110
import { channel } from './channels';
1211
import { runInPool } from './runInPool';
12+
import { installGracefulExit } from './setup';
13+
14+
installGracefulExit();
1315

1416
const send = (response: WorkerResponse): void => {
1517
channel.send(wrapWorkerResponse(response));
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
const gracefulExit: boolean = process.execArgv.some(
2-
(execArg) =>
3-
execArg.startsWith('--perf') ||
4-
execArg.startsWith('--prof') ||
5-
execArg.startsWith('--cpu-prof') ||
6-
execArg.startsWith('--heap-prof') ||
7-
execArg.startsWith('--diagnostic-dir'),
8-
);
1+
// Registered via an explicit call (not a bare `import './setup'`) so the module
2+
// carries no import-time side effect — this keeps `@rstest/core` safe to mark
3+
// `sideEffects: false` for tree-shaking consumers while the worker entries still
4+
// opt in by calling `installGracefulExit()`.
5+
export function installGracefulExit(): void {
6+
const gracefulExit = process.execArgv.some(
7+
(execArg) =>
8+
execArg.startsWith('--perf') ||
9+
execArg.startsWith('--prof') ||
10+
execArg.startsWith('--cpu-prof') ||
11+
execArg.startsWith('--heap-prof') ||
12+
execArg.startsWith('--diagnostic-dir'),
13+
);
914

10-
if (gracefulExit) {
11-
// gracefully handle SIGTERM to generate CPU profile
12-
// https://github.com/nodejs/node/issues/55094
13-
process.on('SIGTERM', () => {
14-
process.exit();
15-
});
15+
if (gracefulExit) {
16+
// gracefully handle SIGTERM to generate CPU profile
17+
// https://github.com/nodejs/node/issues/55094
18+
process.on('SIGTERM', () => {
19+
process.exit();
20+
});
21+
}
1622
}

0 commit comments

Comments
 (0)