Skip to content

process.exit() not enforced when caught #1

@agj

Description

@agj

I've been finding mock-cli super useful for my current small project. However, I noticed that because it catches process.exit(), it doesn't stop execution of the code, and while at that point mock-cli will stop doing its work, the code in question won't, and in my case trigger another sanity check that will kill the process and my tests...

Here's an example of the issue:

// test.js
const mockCLI = require('mock-cli');
mockCLI(process.argv, { stdout: process.stdout, stderr: process.stderr });
require('./tested');
console.log("This should print.");
// tested.js
console.log("Killing process.")
process.exit(1);
console.log("Killing process again.")
process.exit(1);

When running node test it will output the following:

 ▼                            Start of CLI capture                            ▼ 
Killing process.
 ▲                             End of CLI capture                             ▲ 
Killing process again.

The workaround was to add a throw statement right after the process.exit() line, which doesn't seem very elegant to do as its evidently only for the mock testing. I wonder if there's a way to fix this without making a mess of the simplicity with which this utility works. I don't really know the innards of Node well enough to speculate at this point...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions