diff --git a/apps/dokploy/__test__/deploy/should-deploy.test.ts b/apps/dokploy/__test__/deploy/should-deploy.test.ts new file mode 100644 index 000000000..d9a1c0244 --- /dev/null +++ b/apps/dokploy/__test__/deploy/should-deploy.test.ts @@ -0,0 +1,41 @@ +import { shouldDeploy } from "@dokploy/server"; +import { describe, expect, it } from "vitest"; + +describe("shouldDeploy", () => { + it("should deploy when no watch paths are configured", () => { + expect(shouldDeploy(null, ["src/index.ts"])).toBe(true); + expect(shouldDeploy([], ["src/index.ts"])).toBe(true); + }); + + it("should deploy when watch paths match modified files", () => { + expect(shouldDeploy(["src/**"], ["src/index.ts"])).toBe(true); + expect(shouldDeploy(["apps/web/**"], ["apps/web/page.tsx"])).toBe(true); + }); + + it("should not deploy when watch paths do not match", () => { + expect(shouldDeploy(["src/**"], ["docs/readme.md"])).toBe(false); + }); + + it("should not throw when modified files contain non-string values", () => { + expect(() => + shouldDeploy(["src/**"], ["src/index.ts", undefined, null] as any), + ).not.toThrow(); + expect( + shouldDeploy(["src/**"], ["src/index.ts", undefined, null] as any), + ).toBe(true); + }); + + it("should not throw when modified files are undefined or null", () => { + expect(() => shouldDeploy(["src/**"], undefined)).not.toThrow(); + expect(() => shouldDeploy(["src/**"], null)).not.toThrow(); + expect(shouldDeploy(["src/**"], undefined)).toBe(false); + expect(shouldDeploy(["src/**"], null)).toBe(false); + }); + + it("should not throw when every modified file is non-string", () => { + expect(() => + shouldDeploy(["src/**"], [undefined, undefined] as any), + ).not.toThrow(); + expect(shouldDeploy(["src/**"], [undefined, undefined] as any)).toBe(false); + }); +}); diff --git a/packages/server/src/utils/watch-paths/should-deploy.ts b/packages/server/src/utils/watch-paths/should-deploy.ts index 4bc1de1d1..c3e7677fe 100644 --- a/packages/server/src/utils/watch-paths/should-deploy.ts +++ b/packages/server/src/utils/watch-paths/should-deploy.ts @@ -2,8 +2,11 @@ import micromatch from "micromatch"; export const shouldDeploy = ( watchPaths: string[] | null, - modifiedFiles: string[], + modifiedFiles: (string | null | undefined)[] | null | undefined, ): boolean => { if (!watchPaths || watchPaths?.length === 0) return true; - return micromatch.some(modifiedFiles, watchPaths); + const files = (modifiedFiles ?? []).filter( + (file): file is string => typeof file === "string", + ); + return micromatch.some(files, watchPaths); };