From b69dcbe72473353b78a0a22bfb53456b4330aa72 Mon Sep 17 00:00:00 2001 From: dimaslz Date: Fri, 19 Jan 2024 21:04:02 +0100 Subject: [PATCH] test(*): update tests --- packages/cli/global-setup.ts | 7 ++ packages/cli/package.json | 2 +- packages/cli/src/generate-proxy-image.test.ts | 5 +- packages/cli/src/generate-proxy-image.ts | 3 +- packages/cli/src/list-container.test.ts | 7 +- packages/cli/src/list-container.ts | 7 +- packages/cli/src/on-create-action.test.ts | 89 +++++++------------ packages/cli/src/on-create-action.ts | 19 ++-- packages/cli/src/on-list-action.test.ts | 5 +- packages/cli/src/on-list-action.ts | 4 +- packages/cli/src/on-remove-action.test.ts | 29 ++---- packages/cli/src/on-remove-action.ts | 12 +-- packages/cli/src/on-update-action.test.ts | 48 +++------- packages/cli/src/on-update-action.ts | 19 ++-- .../cli/src/utils/validate-domain.test.ts | 15 +--- packages/cli/src/utils/validate-domain.ts | 6 +- .../cli/src/utils/validate-location.test.ts | 10 +-- packages/cli/src/utils/validate-location.ts | 6 +- packages/cli/src/utils/validate-port.test.ts | 13 +-- packages/cli/src/utils/validate-port.ts | 11 +-- 20 files changed, 116 insertions(+), 201 deletions(-) diff --git a/packages/cli/global-setup.ts b/packages/cli/global-setup.ts index 7c67a87..c89f0ab 100644 --- a/packages/cli/global-setup.ts +++ b/packages/cli/global-setup.ts @@ -1,3 +1,4 @@ +import fs from "fs"; import shell from "shelljs"; import { beforeEach, vi } from "vitest"; @@ -20,4 +21,10 @@ beforeEach(() => { mkcert: vi.fn(), }; }); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + vi.spyOn(fs, "readdirSync").mockImplementationOnce((): any[] => [ + "some-domain.com-cert.pem", + "some-domain.com-key.pem", + ]); }); diff --git a/packages/cli/package.json b/packages/cli/package.json index 6337591..4f193d2 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -41,7 +41,7 @@ "build": "ncc build src/index.ts -o dist", "to-publish": "npx vite-node publish.js", "test": "vitest --reporter=verbose", - "lint": "oxlint ." + "lint": "eslint ./src" }, "dependencies": { "@dimaslz/local-ssl-management-core": "*", diff --git a/packages/cli/src/generate-proxy-image.test.ts b/packages/cli/src/generate-proxy-image.test.ts index 1dfd938..1ac4b80 100644 --- a/packages/cli/src/generate-proxy-image.test.ts +++ b/packages/cli/src/generate-proxy-image.test.ts @@ -20,14 +20,11 @@ describe("Generate proxy image", () => { describe("failure", () => { test("does not exists config to create reverse proxy", () => { - expect(() => { - generateProxyImage([]); - }).toThrow(); + generateProxyImage([]); expect(consola.warn).toBeCalledWith( "Does not exists config to create reverse proxy", ); - expect(shell.exit).toBeCalledWith(1); }); }); diff --git a/packages/cli/src/generate-proxy-image.ts b/packages/cli/src/generate-proxy-image.ts index 323c165..de6560e 100644 --- a/packages/cli/src/generate-proxy-image.ts +++ b/packages/cli/src/generate-proxy-image.ts @@ -41,7 +41,8 @@ const renderTable = (config: Config[]) => { const generateProxyImage = (config: Config[]) => { if (!config.length) { consola.warn("Does not exists config to create reverse proxy"); - shell.exit(1); + + return; } const localhostCertExists = fs.existsSync(sslPath + "/localhost-cert.pem"); diff --git a/packages/cli/src/list-container.test.ts b/packages/cli/src/list-container.test.ts index ceb1949..325ea5e 100644 --- a/packages/cli/src/list-container.test.ts +++ b/packages/cli/src/list-container.test.ts @@ -36,14 +36,11 @@ describe("List container", () => { }; }); - expect(() => { - listContainer(); - }).toThrow(); + listContainer(); expect(consola.error).toBeCalledWith( - new Error("Something have been failure. Contact with the author."), + "Something have been failure. Contact with the author.", ); - expect(shell.exit).toBeCalledTimes(1); }); }); }); diff --git a/packages/cli/src/list-container.ts b/packages/cli/src/list-container.ts index 357e058..cf3325a 100644 --- a/packages/cli/src/list-container.ts +++ b/packages/cli/src/list-container.ts @@ -18,10 +18,9 @@ const listContainer = () => { .find((line) => /local-ssl-management/.test(line)) || ""; if (!containerData) { - consola.error( - new Error("Something have been failure. Contact with the author."), - ); - shell.exit(1); + consola.error("Something have been failure. Contact with the author."); + + return; } const [containerId, containerName, ports] = containerData diff --git a/packages/cli/src/on-create-action.test.ts b/packages/cli/src/on-create-action.test.ts index 0790930..f05b34a 100644 --- a/packages/cli/src/on-create-action.test.ts +++ b/packages/cli/src/on-create-action.test.ts @@ -18,30 +18,42 @@ describe("On create action", () => { const domain = "wrong.domain"; const port = "3000"; - expect(() => { - onCreateAction(domain, { port }); - }).toThrow(); + vi.spyOn(shell, "exec") + // eslint-disable-next-line @typescript-eslint/no-explicit-any + .mockImplementationOnce((): any => "") + .mockImplementationOnce( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (): any => { + return { stdout: 200 }; + }, + ); + + onCreateAction(domain, { port }); expect(consola.error).toBeCalledWith( - new Error("Domain (https://wrong.domain) format is not valid"), + "Domain (https://wrong.domain) format is not valid", ); - expect(shell.exit).toHaveBeenCalledWith(1); }); test("can not create with invalid port", () => { const domain = "some-domain.com"; const port = "666"; - expect(() => { - onCreateAction(domain, { port }); - }).toThrow(); + vi.spyOn(shell, "exec") + // eslint-disable-next-line @typescript-eslint/no-explicit-any + .mockImplementationOnce((): any => "") + .mockImplementationOnce( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (): any => { + return { stdout: 200 }; + }, + ); + + onCreateAction(domain, { port }); expect(consola.error).toBeCalledWith( - new Error( - "Port (--port ) should be into the range 1025 to 65535", - ), + "Port (--port ) should be into the range 1025 to 65535", ); - expect(shell.exit).toHaveBeenCalledWith(1); }); test("can not create duplicated location", () => { @@ -49,12 +61,6 @@ describe("On create action", () => { const port = "3000"; const location = "/app-name"; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - vi.spyOn(fs, "readdirSync").mockImplementationOnce((): any[] => [ - "some-domain.com-cert.pem", - "some-domain.com-key.pem", - ]); - vi.spyOn(fs, "readFileSync").mockReturnValueOnce( JSON.stringify([ { @@ -76,25 +82,16 @@ describe("On create action", () => { ]), ); - expect(() => { - onCreateAction(domain, { port, location }); - }).toThrow(); + onCreateAction(domain, { port, location }); expect(consola.error).toBeCalledWith( - new Error('Location "/app-name" already exists'), + 'Location "/app-name" already exists', ); - expect(shell.exit).toHaveBeenCalledWith(1); }); test("can not create duplicated domain", () => { const domain = "some-domain.com"; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - vi.spyOn(fs, "readdirSync").mockImplementationOnce((): any[] => [ - "some-domain.com-cert.pem", - "some-domain.com-key.pem", - ]); - vi.spyOn(fs, "readFileSync").mockReturnValueOnce( JSON.stringify([ { @@ -116,28 +113,16 @@ describe("On create action", () => { ]), ); - expect(() => { - onCreateAction(domain, {}); - }).toThrow(); + onCreateAction(domain, {}); expect(consola.error).toBeCalledWith( - new Error( - 'Domain "some-domain.com" already created with the default location "/"', - ), + 'Domain "some-domain.com" already created with the default location "/"', ); - expect(shell.exit).toHaveBeenCalledWith(1); }); }); describe("success", () => { beforeEach(() => { - vi.spyOn(fs, "mkdirSync"); - // eslint-disable-next-line @typescript-eslint/no-explicit-any - vi.spyOn(fs, "readdirSync").mockImplementationOnce((): any[] => [ - "some-domain.com-cert.pem", - "some-domain.com-key.pem", - ]); - vi.spyOn(shell, "exec") // eslint-disable-next-line @typescript-eslint/no-explicit-any .mockImplementationOnce((): any => "") @@ -290,16 +275,11 @@ describe("On create action", () => { const domain = "some-domain.com"; const port = "3000"; - expect(() => { - onCreateAction(domain, { port }); - }).toThrow(); + onCreateAction(domain, { port }); expect(consola.error).toBeCalledWith( - new Error( - 'Domain "some-domain.com" already created with the default location "/"', - ), + 'Domain "some-domain.com" already created with the default location "/"', ); - expect(shell.exit).toHaveBeenCalledWith(1); }); }); @@ -479,16 +459,11 @@ describe("On create action", () => { const domain = "some-domain.com"; const port = "3000"; - expect(() => { - onCreateAction(domain, { port }); - }).toThrow(); + onCreateAction(domain, { port }); expect(consola.error).toBeCalledWith( - new Error( - 'Domain "some-domain.com" already created with the default location "/"', - ), + 'Domain "some-domain.com" already created with the default location "/"', ); - expect(shell.exit).toHaveBeenCalledWith(1); }); }); }); diff --git a/packages/cli/src/on-create-action.ts b/packages/cli/src/on-create-action.ts index e55058e..b7d42a6 100644 --- a/packages/cli/src/on-create-action.ts +++ b/packages/cli/src/on-create-action.ts @@ -3,7 +3,6 @@ import consola from "consola"; import crypto from "crypto"; import fs from "fs"; import path from "path"; -import shell from "shelljs"; import generateProxyImage from "./generate-proxy-image"; import { validateDomain, validateLocation, validatePort } from "./utils"; @@ -65,20 +64,19 @@ const onCreateAction = ( if (domainExists && locationExists) { if (location === "/") { consola.error( - new Error( - `Domain "${domain}" already created with the default location "${location}"`, - ), + `Domain "${domain}" already created with the default location "${location}"`, ); } else { - consola.error(new Error(`Location "${location}" already exists`)); + consola.error(`Location "${location}" already exists`); } - shell.exit(1); + return; } if (domainExists && locationExists) { - consola.error(new Error(`Domain "${domain}" already exists`)); - shell.exit(1); + consola.error(`Domain "${domain}" already exists`); + + return; } const portExists = @@ -86,8 +84,9 @@ const onCreateAction = ( false; if (portExists) { - consola.error(new Error(`Port "${port}" already exists`)); - shell.exit(1); + consola.error(`Port "${port}" already exists`); + + return; } if (domainIndex > -1) { diff --git a/packages/cli/src/on-list-action.test.ts b/packages/cli/src/on-list-action.test.ts index c702fac..b501da7 100644 --- a/packages/cli/src/on-list-action.test.ts +++ b/packages/cli/src/on-list-action.test.ts @@ -8,12 +8,9 @@ describe("On list action", () => { test("no domains available", () => { vi.spyOn(fs, "readFileSync").mockReturnValue("[]"); - expect(() => { - onListAction(); - }).toThrow(); + onListAction(); expect(consola.box).toBeCalledWith(`Does not exists configs yet.`); - expect(shell.exit).toBeCalledTimes(1); }); test("list domains availables", () => { diff --git a/packages/cli/src/on-list-action.ts b/packages/cli/src/on-list-action.ts index 77b5333..1933608 100644 --- a/packages/cli/src/on-list-action.ts +++ b/packages/cli/src/on-list-action.ts @@ -2,7 +2,6 @@ import type { Config } from "@dimaslz/local-ssl-management-core"; import consola from "consola"; import fs from "fs"; import path from "path"; -import shell from "shelljs"; import { listConfigs } from "./utils"; @@ -17,7 +16,8 @@ const onListAction = () => { if (!config.length) { consola.box("Does not exists configs yet."); - shell.exit(1); + + return; } listConfigs(config); diff --git a/packages/cli/src/on-remove-action.test.ts b/packages/cli/src/on-remove-action.test.ts index 4bf5d28..fc5aba1 100644 --- a/packages/cli/src/on-remove-action.test.ts +++ b/packages/cli/src/on-remove-action.test.ts @@ -19,29 +19,19 @@ describe("On remove action", () => { test("removing domain by name does not exists", () => { vi.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify([])); - expect(() => { - onRemoveAction("dummy"); - }).toThrow(); + onRemoveAction("dummy"); - expect(consola.error).toBeCalledWith( - new Error('Domain "dummy" does not exists'), - ); - expect(shell.exit).toHaveBeenCalledWith(1); + expect(consola.error).toBeCalledWith('Domain "dummy" does not exists'); }); test("remove domain by id does not exists", () => { vi.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify([])); - expect(() => { - onRemoveAction("6eb61d17-ba78-4618-a2ac-47aeb4ba8b26"); - }).toThrow(); + onRemoveAction("6eb61d17-ba78-4618-a2ac-47aeb4ba8b26"); expect(consola.error).toBeCalledWith( - new Error( - 'Domain with id "6eb61d17-ba78-4618-a2ac-47aeb4ba8b26" does not exists', - ), + 'Domain with id "6eb61d17-ba78-4618-a2ac-47aeb4ba8b26" does not exists', ); - expect(shell.exit).toHaveBeenCalledWith(1); }); test("removing path does not exists", () => { @@ -65,16 +55,13 @@ describe("On remove action", () => { ), ); - expect(() => { - onRemoveAction("6eb61d17-ba78-4618-a2ac-47aeb4ba8b26", { - location: "/something", - }); - }).toThrow(); + onRemoveAction("6eb61d17-ba78-4618-a2ac-47aeb4ba8b26", { + location: "/something", + }); expect(consola.error).toBeCalledWith( - new Error('Location "/something" does not exists'), + 'Location "/something" does not exists', ); - expect(shell.exit).toHaveBeenCalledWith(1); }); }); diff --git a/packages/cli/src/on-remove-action.ts b/packages/cli/src/on-remove-action.ts index 4589541..02cfe52 100644 --- a/packages/cli/src/on-remove-action.ts +++ b/packages/cli/src/on-remove-action.ts @@ -2,7 +2,6 @@ import type { Config } from "@dimaslz/local-ssl-management-core"; import consola from "consola"; import fs from "fs"; import path from "path"; -import shell from "shelljs"; import generateProxyImage from "./generate-proxy-image"; const distPath = path.resolve(__dirname, "./"); @@ -33,12 +32,12 @@ const onRemoveAction = ( if (!exists) { if (isUUID) { - consola.error(new Error(`Domain with id "${_domain}" does not exists`)); + consola.error(`Domain with id "${_domain}" does not exists`); } else { - consola.error(new Error(`Domain "${domain}" does not exists`)); + consola.error(`Domain "${domain}" does not exists`); } - shell.exit(1); + return; } const byLocation = !!location; @@ -55,8 +54,9 @@ const onRemoveAction = ( ); if (!locationExists) { - consola.error(new Error(`Location "${location}" does not exists`)); - shell.exit(1); + consola.error(`Location "${location}" does not exists`); + + return; } newConfig = config.map((c, cIndex) => { diff --git a/packages/cli/src/on-update-action.test.ts b/packages/cli/src/on-update-action.test.ts index 98061e5..0ca905b 100644 --- a/packages/cli/src/on-update-action.test.ts +++ b/packages/cli/src/on-update-action.test.ts @@ -40,15 +40,11 @@ describe("On update action", () => { ), ); - expect(() => { - onUpdateAction(domain, { port }); - }).toThrow(); + onUpdateAction(domain, { port }); expect(consola.error).toBeCalledWith( - new Error('Domain "foo-domain.com" does not exists'), + 'Domain "foo-domain.com" does not exists', ); - - expect(shell.exit).toBeCalledWith(1); }); test("update domain by wrong id", () => { const domain = "48d1a85c-377a-40ef-8a82-d1405f7a0722"; @@ -74,16 +70,11 @@ describe("On update action", () => { ), ); - expect(() => { - onUpdateAction(domain, { port }); - }).toThrow(); + onUpdateAction(domain, { port }); expect(consola.error).toBeCalledWith( - new Error( - 'Domain with key "48d1a85c-377a-40ef-8a82-d1405f7a0722" does not exists', - ), + 'Domain with key "48d1a85c-377a-40ef-8a82-d1405f7a0722" does not exists', ); - expect(shell.exit).toBeCalledWith(1); }); test("location is mandatory", () => { @@ -110,12 +101,9 @@ describe("On update action", () => { ), ); - expect(() => { - onUpdateAction(domain, { port }); - }).toThrow(); + onUpdateAction(domain, { port }); - expect(consola.error).toBeCalledWith(new Error("Location is mandatory")); - expect(shell.exit).toBeCalledWith(1); + expect(consola.error).toBeCalledWith("Location is mandatory"); }); test("location does not exists on replace", () => { @@ -141,14 +129,11 @@ describe("On update action", () => { ), ); - expect(() => { - onUpdateAction(domain, { location: "/not-exists,/app-name" }); - }).toThrow(); + onUpdateAction(domain, { location: "/not-exists,/app-name" }); expect(consola.error).toBeCalledWith( - new Error('Location "/not-exists" does not exists'), + 'Location "/not-exists" does not exists', ); - expect(shell.exit).toBeCalledWith(1); }); test("location does not exists on update", () => { @@ -174,22 +159,16 @@ describe("On update action", () => { ), ); - expect(() => { - onUpdateAction(domain, { location: "/app-name" }); - }).toThrow(); + onUpdateAction(domain, { location: "/app-name" }); expect(consola.error).toBeCalledWith( - new Error('Location "/app-name" does not exists'), + 'Location "/app-name" does not exists', ); - expect(shell.exit).toBeCalledWith(1); }); test.skip("port is mandatory", () => { const domain = "48d1a85c-377a-40ef-8a82-d1405f7a074f"; - vi.spyOn(shell, "exit").mockImplementationOnce(() => { - throw new Error(); - }); vi.spyOn(fs, "readFileSync").mockReturnValue( JSON.stringify( [ @@ -210,12 +189,9 @@ describe("On update action", () => { ), ); - expect(() => { - onUpdateAction(domain, { location: "/" }); - }).toThrow(); + onUpdateAction(domain, { location: "/" }); - expect(consola.error).toBeCalledWith(new Error("Port is mandatory")); - expect(shell.exit).toBeCalledWith(1); + expect(consola.error).toBeCalledWith("Port is mandatory"); }); }); diff --git a/packages/cli/src/on-update-action.ts b/packages/cli/src/on-update-action.ts index 73ed96c..8d8c5cf 100644 --- a/packages/cli/src/on-update-action.ts +++ b/packages/cli/src/on-update-action.ts @@ -2,7 +2,6 @@ import { Config } from "@dimaslz/local-ssl-management-core"; import consola from "consola"; import fs from "fs"; import path from "path"; -import shell from "shelljs"; import generateProxyImage from "./generate-proxy-image"; import { validatePort } from "./utils"; @@ -29,21 +28,21 @@ const onUpdateAction = (domain: string, options: Options) => { if (!exists) { if (isUUID) { - consola.error(new Error(`Domain with key "${domain}" does not exists`)); + consola.error(`Domain with key "${domain}" does not exists`); } else { - consola.error(new Error(`Domain "${domain}" does not exists`)); + consola.error(`Domain "${domain}" does not exists`); } - shell.exit(1); + return; } let { port } = options; const { location } = options; if (!location) { - consola.error(new Error(`Location is mandatory`)); + consola.error(`Location is mandatory`); - shell.exit(1); + return; } const domainIndex = config.findIndex( @@ -64,9 +63,9 @@ const onUpdateAction = (domain: string, options: Options) => { ); if (!oldLocationExists) { - consola.error(new Error(`Location "${oldLocation}" does not exists`)); + consola.error(`Location "${oldLocation}" does not exists`); - shell.exit(1); + return; } } else { const locationExists = config[domainIndex].services.some( @@ -74,9 +73,9 @@ const onUpdateAction = (domain: string, options: Options) => { ); if (!locationExists) { - consola.error(new Error(`Location "${location}" does not exists`)); + consola.error(`Location "${location}" does not exists`); - shell.exit(1); + return; } } diff --git a/packages/cli/src/utils/validate-domain.test.ts b/packages/cli/src/utils/validate-domain.test.ts index 5997bd1..fababcd 100644 --- a/packages/cli/src/utils/validate-domain.test.ts +++ b/packages/cli/src/utils/validate-domain.test.ts @@ -8,27 +8,21 @@ describe("Validate domain", () => { test("domain with wrong TLD should not be acceptable", () => { const domain = "your-domain.FAKE_TLD"; - expect(() => { - validateDomain(domain); - }).toThrow(); + validateDomain(domain); expect(consola.error).toBeCalledWith( - new Error("Domain (https://your-domain.FAKE_TLD) format is not valid"), + "Domain (https://your-domain.FAKE_TLD) format is not valid", ); - expect(shell.exit).toHaveBeenCalledWith(1); }); test("domain with not allowed characters should not be acceptable", () => { const domain = "your!domain.com"; - expect(() => { - validateDomain(domain); - }).toThrow(); + validateDomain(domain); expect(consola.error).toBeCalledWith( - new Error("Domain (https://your!domain.com) format is not valid"), + "Domain (https://your!domain.com) format is not valid", ); - expect(shell.exit).toHaveBeenCalledWith(1); }); }); @@ -46,7 +40,6 @@ describe("Validate domain", () => { validateDomain(domain); expect(shell.echo).not.toBeCalled(); - expect(shell.exit).not.toHaveBeenCalledWith(1); }); }); }); diff --git a/packages/cli/src/utils/validate-domain.ts b/packages/cli/src/utils/validate-domain.ts index d0c2cd4..006738a 100644 --- a/packages/cli/src/utils/validate-domain.ts +++ b/packages/cli/src/utils/validate-domain.ts @@ -1,6 +1,5 @@ import consola from "consola"; import isUrlHttp from "is-url-http"; -import shell from "shelljs"; const validateDomain = (value: string) => { const domains = value.split(",").map((d) => `https://${d.trim()}`); @@ -14,8 +13,9 @@ const validateDomain = (value: string) => { } if (!isUrlHttp(domain)) { - consola.error(new Error(`Domain (${domainItem}) format is not valid`)); - shell.exit(1); + consola.error(`Domain (${domainItem}) format is not valid`); + + return; } }); }; diff --git a/packages/cli/src/utils/validate-location.test.ts b/packages/cli/src/utils/validate-location.test.ts index 036567b..77115f5 100644 --- a/packages/cli/src/utils/validate-location.test.ts +++ b/packages/cli/src/utils/validate-location.test.ts @@ -1,19 +1,13 @@ import consola from "consola"; -import shell from "shelljs"; import validateLocation from "./validate-location"; describe("Validate location", () => { describe("failures", () => { test("location has bad format", () => { - expect(() => { - validateLocation("foo"); - }).toThrow(); + validateLocation("foo"); - expect(consola.error).toBeCalledWith( - new Error("Location should start by /"), - ); - expect(shell.exit).toBeCalledWith(1); + expect(consola.error).toBeCalledWith("Location should start by /"); }); }); diff --git a/packages/cli/src/utils/validate-location.ts b/packages/cli/src/utils/validate-location.ts index d322078..e8e50f4 100644 --- a/packages/cli/src/utils/validate-location.ts +++ b/packages/cli/src/utils/validate-location.ts @@ -1,10 +1,10 @@ import consola from "consola"; -import shell from "shelljs"; const validateLocation = (location: string) => { if (!location.startsWith("/")) { - consola.error(new Error("Location should start by /")); - shell.exit(1); + consola.error("Location should start by /"); + + return; } }; diff --git a/packages/cli/src/utils/validate-port.test.ts b/packages/cli/src/utils/validate-port.test.ts index 678d6ab..37abdfc 100644 --- a/packages/cli/src/utils/validate-port.test.ts +++ b/packages/cli/src/utils/validate-port.test.ts @@ -1,5 +1,4 @@ import consola from "consola"; -import shell from "shelljs"; import validatePort from "./validate-port"; @@ -10,7 +9,6 @@ describe("Validate port", () => { validatePort(port); expect(consola.error).not.toBeCalled(); - expect(shell.exit).not.toHaveBeenCalledWith(1); }); }); @@ -18,22 +16,17 @@ describe("Validate port", () => { const ports = [["foo"], ["333"], ["1024"], ["70000"]]; test.each(ports)("Port %s is not valid", (port) => { - expect(() => { - validatePort(port); - }).toThrow(); + validatePort(port); if (Number(port)) { expect(consola.error).toBeCalledWith( - new Error( - "Port (--port ) should be into the range 1025 to 65535", - ), + "Port (--port ) should be into the range 1025 to 65535", ); } else { expect(consola.error).toBeCalledWith( - new Error("Port (--port ) should be a valid number"), + "Port (--port ) should be a valid number", ); } - expect(shell.exit).toHaveBeenCalledWith(1); }); }); }); diff --git a/packages/cli/src/utils/validate-port.ts b/packages/cli/src/utils/validate-port.ts index ed42d38..fbd82bd 100644 --- a/packages/cli/src/utils/validate-port.ts +++ b/packages/cli/src/utils/validate-port.ts @@ -1,19 +1,20 @@ import consola from "consola"; -import shell from "shelljs"; const validatePort = (port: string) => { const portIsNumber = !isNaN(Number(port)); if (!portIsNumber) { - consola.error(new Error("Port (--port ) should be a valid number")); - shell.exit(1); + consola.error("Port (--port ) should be a valid number"); + + return; } const portIsValid = Number(port) > 1024 && Number(port) <= 65535; if (!portIsValid) { consola.error( - new Error("Port (--port ) should be into the range 1025 to 65535"), + "Port (--port ) should be into the range 1025 to 65535", ); - shell.exit(1); + + return; } };