Skip to content
Merged
31 changes: 16 additions & 15 deletions e2e/runner/test/signalListener.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { join } from 'node:path';
import { expect, it } from '@rstest/core';
import { it } from '@rstest/core';
import { runRstestCli } from '../../scripts/';

it('should exit correctly when signal listener exists', async () => {
const { cli, expectExecSuccess } = await runRstestCli({
command: 'rstest',
args: ['run', 'signalListener.test.ts'],
options: {
nodeOptions: {
cwd: join(__dirname, 'fixtures'),
it.skipIf(process.platform === 'win32')(
'should catch signal exit correctly when signal listener exists',
async () => {
const { expectExecFailed, expectLog } = await runRstestCli({
command: 'rstest',
args: ['run', 'signalListener.test.ts'],
options: {
nodeOptions: {
cwd: join(__dirname, 'fixtures'),
},
},
},
});
await expectExecSuccess();
});
await expectExecFailed();

const logs = cli.stdout.split('\n').filter(Boolean);

expect(logs.find((log) => log.includes('Test Files 1 passed'))).toBeDefined();
});
expectLog(/Rstest exited unexpectedly with code 0/);
},
);
11 changes: 11 additions & 0 deletions packages/core/src/core/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,19 @@ export async function runTests(context: Rstest): Promise<void> {
afterTestsWatchRun();
});
} else {
const unExpectedExit = (code?: number) => {
logger.log(
color.red(
`Rstest exited unexpectedly with code ${code}, terminating test run.`,
),
);
process.exitCode = 1;
};
process.on('exit', unExpectedExit);

await run();
await pool.close();
await closeServer();
process.off('exit', unExpectedExit);
}
}
8 changes: 0 additions & 8 deletions packages/core/src/runtime/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,6 @@ const loadFiles = async ({
});
};

const onExit = () => {
process.exit();
};

const runInPool = async (
options: RunWorkerOptions['options'],
): Promise<
Expand Down Expand Up @@ -294,15 +290,11 @@ const runInPool = async (
process.exit = exit;
});

process.off('SIGTERM', onExit);

const teardown = async () => {
await new Promise((resolve) => getRealTimers().setTimeout!(resolve));

await Promise.all(cleanups.map((fn) => fn()));
isTeardown = true;
// should exit correctly when user's signal listener exists
process.once('SIGTERM', onExit);
};

if (type === 'collect') {
Expand Down
Loading