Fix tests to match new behavior

This commit is contained in:
Sander Declerck 2025-10-08 10:56:31 +02:00
parent 240123372a
commit 8950d528d5
No known key found for this signature in database

View file

@ -1,5 +1,5 @@
import assert from "node:assert/strict"; import assert from "node:assert/strict";
import { describe, it, mock } from "node:test"; import { beforeEach, describe, it, mock } from "node:test";
import { setTimeout } from "node:timers/promises"; import { setTimeout } from "node:timers/promises";
import { import {
MALWARE_ACTION_PROMPT, MALWARE_ACTION_PROMPT,
@ -88,6 +88,11 @@ describe("scanCommand", async () => {
const { scanCommand } = await import("./index.js"); const { scanCommand } = await import("./index.js");
beforeEach(() => {
// Reset malware action back to prompt mode for other tests
malwareAction = MALWARE_ACTION_PROMPT;
});
it("should succeed when there are no changes", async () => { it("should succeed when there are no changes", async () => {
let progressWasStopped = false; let progressWasStopped = false;
mockStartProcess.mock.mockImplementationOnce(() => ({ mockStartProcess.mock.mockImplementationOnce(() => ({
@ -205,7 +210,6 @@ describe("scanCommand", async () => {
mockConfirm.mock.resetCalls(); mockConfirm.mock.resetCalls();
let failureMessageWasSet = false; let failureMessageWasSet = false;
let exitCode = null;
mockStartProcess.mock.mockImplementationOnce(() => ({ mockStartProcess.mock.mockImplementationOnce(() => ({
setText: () => {}, setText: () => {},
@ -220,27 +224,10 @@ describe("scanCommand", async () => {
{ name: "malicious", version: "1.0.0" }, { name: "malicious", version: "1.0.0" },
]); ]);
// Mock process.exit const result = await scanCommand(["install", "malicious"]);
const originalExit = process.exit;
process.exit = mock.fn((code) => {
exitCode = code;
throw new Error("Process exit called"); // Prevent actual exit
});
try {
await assert.rejects(
scanCommand(["install", "malicious"]),
/Process exit called/
);
} finally {
// Restore original process.exit
process.exit = originalExit;
// Reset malware action back to prompt mode for other tests
malwareAction = MALWARE_ACTION_PROMPT;
}
assert.equal(failureMessageWasSet, true); assert.equal(failureMessageWasSet, true);
assert.equal(exitCode, 1); assert.equal(result, 1);
// Confirm should not have been called in block mode // Confirm should not have been called in block mode
assert.equal(mockConfirm.mock.callCount(), 0); assert.equal(mockConfirm.mock.callCount(), 0);
}); });
@ -252,7 +239,6 @@ describe("scanCommand", async () => {
// Reset mock call count // Reset mock call count
mockConfirm.mock.resetCalls(); mockConfirm.mock.resetCalls();
let processExited = false;
let userWasPrompted = false; let userWasPrompted = false;
mockStartProcess.mock.mockImplementationOnce(() => ({ mockStartProcess.mock.mockImplementationOnce(() => ({
@ -271,26 +257,9 @@ describe("scanCommand", async () => {
return false; return false;
}); });
// Mock process.exit const result = await scanCommand(["install", "malicious"]);
const originalExit = process.exit;
process.exit = mock.fn(() => {
processExited = true;
throw new Error("Process exit called"); // Prevent actual exit
});
try { assert.equal(result, 1);
await assert.rejects(
scanCommand(["install", "malicious"]),
/Process exit called/
);
} finally {
// Restore original process.exit
process.exit = originalExit;
// Reset malware action back to prompt mode for other tests
malwareAction = MALWARE_ACTION_PROMPT;
}
assert.equal(processExited, true);
assert.equal(userWasPrompted, false); assert.equal(userWasPrompted, false);
}); });
}); });