From 9edcc99e99a225ec00cc9cebf8f0dbf7615bcbf9 Mon Sep 17 00:00:00 2001 From: Mystic <215104920@qq.com> Date: Wed, 18 Sep 2024 21:51:43 +0800 Subject: [PATCH] test(ts): use the snapshot --- .../tests/index.test.ts | 165 +++++++++--------- 1 file changed, 85 insertions(+), 80 deletions(-) diff --git a/template/ts/{{cookiecutter.project_slug}}/tests/index.test.ts b/template/ts/{{cookiecutter.project_slug}}/tests/index.test.ts index a1bdfce..727c34a 100644 --- a/template/ts/{{cookiecutter.project_slug}}/tests/index.test.ts +++ b/template/ts/{{cookiecutter.project_slug}}/tests/index.test.ts @@ -28,119 +28,124 @@ describe("GitHub Action", () => { const mockWriteFile = fs.writeFile as jest.MockedFunction; const mockAxiosGet = axios.get as jest.MockedFunction; - beforeEach(() => { - jest.resetAllMocks(); + beforeAll(() => { mockGetInput.mockReturnValue(".github/configs/setup-custom-action-by-ts.toml"); - mockReadFile.mockResolvedValue(mockConfig); - mockAxiosGet.mockResolvedValue({ status: 200, data: {} }); }); - it("should process text, count words, calculate sum and average correctly", async () => { - await run(); - - expect(mockSetOutput).toHaveBeenCalledWith("processed_text", "Hi world! Hi!"); - expect(mockSetOutput).toHaveBeenCalledWith("word_count", 3); - expect(mockSetOutput).toHaveBeenCalledWith("sum", 15); - expect(mockSetOutput).toHaveBeenCalledWith("average", 3); + beforeEach(() => { + jest.resetAllMocks(); + setupDefaultMocks(); }); - it("should handle missing configuration file gracefully", async () => { - mockReadFile.mockRejectedValue(new Error("File not found")); + const setupDefaultMocks = () => { + mockReadFile.mockResolvedValue(mockConfig); + mockAxiosGet.mockResolvedValue({ status: 200, data: {} }); + }; + + const setupFileMocks = (inputContent: string) => { + mockReadFile.mockImplementation((path) => { + if (path === ".github/configs/setup-custom-action-by-ts.toml") { + return Promise.resolve(mockConfig); + } + if (path === "input.txt") { + return Promise.resolve(inputContent); + } + return Promise.reject(new Error("Unexpected file read")); + }); + }; - await run(); + describe("Text Processing", () => { + it("should process text, count words, calculate sum and average correctly", async () => { + await run(); - expect(mockSetFailed).toHaveBeenCalledWith("Action failed with error: File not found"); - }); + expect(mockSetOutput.mock.calls).toMatchSnapshot(); + }); - it("should handle API request failures gracefully", async () => { - mockAxiosGet.mockRejectedValue(new Error("API error")); + it("should handle empty input text", async () => { + mockReadFile.mockResolvedValue(` +input_text = "" +number_list = [] +`); - await run(); + await run(); - expect(mockWarning).toHaveBeenCalledWith("Failed to make API request: API error"); + expect(mockSetOutput.mock.calls).toMatchSnapshot(); + }); }); - it("should check API reachability correctly", async () => { - await run(); + describe("File Operations", () => { + it("should read from the input file and append text to the output file correctly", async () => { + setupFileMocks("Original file content."); - expect(mockAxiosGet).toHaveBeenCalledWith("https://api.example.com/data", { timeout: 10000 }); - expect(mockInfo).toHaveBeenCalledWith("API https://api.example.com/data is reachable."); - }); + await run(); - it("should check API reachability correctly and warn on bad status", async () => { - mockAxiosGet.mockResolvedValue({ status: 500, data: {} }); + expect(mockReadFile).toHaveBeenCalledWith("input.txt", "utf-8"); + expect(mockWriteFile).toHaveBeenCalledWith("output.txt", "Original file content.\nGoodbye!", { + encoding: "utf-8", + }); + expect(mockInfo).toHaveBeenCalledWith("Appended text to file: output.txt"); + }); - await run(); + it("should handle missing input file gracefully", async () => { + mockReadFile.mockRejectedValue(new Error("File not found")); - expect(mockWarning).toHaveBeenCalledWith("API is not reachable, status code: 500"); - }); + await run(); - it("should read from the input file and append text to the output file correctly", async () => { - mockReadFile.mockImplementation((path) => { - if (path === ".github/configs/setup-custom-action-by-ts.toml") { - return Promise.resolve(mockConfig); - } - if (path === "input.txt") { - return Promise.resolve("Original file content."); - } - return Promise.reject(new Error("Unexpected file read")); + expect(mockSetFailed).toHaveBeenCalledWith("Action failed with error: File not found"); }); - await run(); + it("should handle file write errors gracefully", async () => { + setupFileMocks("Original file content."); + mockWriteFile.mockRejectedValue(new Error("Failed to write to output file")); + + await run(); - expect(mockReadFile).toHaveBeenCalledWith(".github/configs/setup-custom-action-by-ts.toml", "utf-8"); - expect(mockReadFile).toHaveBeenCalledWith("input.txt", "utf-8"); - expect(mockWriteFile).toHaveBeenCalledWith("output.txt", "Original file content.\nGoodbye!", { - encoding: "utf-8", + expect(mockSetFailed).toHaveBeenCalledWith( + "Action failed with error: File operation failed: Failed to write to output file", + ); }); - expect(mockInfo).toHaveBeenCalledWith("Appended text to file: output.txt"); }); - it("should handle file read errors gracefully", async () => { - mockReadFile.mockImplementation((path) => { - if (path === ".github/configs/setup-custom-action-by-ts.toml") { - return Promise.resolve(mockConfig); - } - if (path === "input.txt") { - return Promise.reject(new Error("Failed to read input file")); - } - return Promise.reject(new Error("Unexpected file read")); + describe("API Operations", () => { + it("should check API reachability correctly", async () => { + await run(); + + expect(mockAxiosGet).toHaveBeenCalledWith("https://api.example.com/data", { timeout: 10000 }); + expect(mockInfo).toHaveBeenCalledWith("API https://api.example.com/data is reachable."); }); - await run(); + it("should handle API request failures gracefully", async () => { + mockAxiosGet.mockRejectedValue(new Error("API error")); - expect(mockSetFailed).toHaveBeenCalledWith( - "Action failed with error: File operation failed: Failed to read input file", - ); - }); + await run(); - it("should handle file write errors gracefully", async () => { - mockReadFile.mockImplementation((path) => { - if (path === ".github/configs/setup-custom-action-by-ts.toml") { - return Promise.resolve(mockConfig); - } - if (path === "input.txt") { - return Promise.resolve("Original file content."); - } - return Promise.reject(new Error("Unexpected file read")); + expect(mockWarning).toHaveBeenCalledWith("Failed to make API request: API error"); }); - mockWriteFile.mockRejectedValue(new Error("Failed to write to output file")); - await run(); + it("should warn on bad API status", async () => { + mockAxiosGet.mockResolvedValue({ status: 500, data: {} }); + + await run(); - expect(mockSetFailed).toHaveBeenCalledWith( - "Action failed with error: File operation failed: Failed to write to output file", - ); + expect(mockWarning).toHaveBeenCalledWith("API is not reachable, status code: 500"); + }); }); - it("should use default values when config is empty", async () => { - mockReadFile.mockResolvedValue(""); + describe("Configuration Handling", () => { + it("should use default values when config is empty", async () => { + mockReadFile.mockResolvedValue(""); + + await run(); + + expect(mockSetOutput.mock.calls).toMatchSnapshot(); + }); + + it("should handle missing configuration file gracefully", async () => { + mockReadFile.mockRejectedValue(new Error("Config file not found")); - await run(); + await run(); - expect(mockSetOutput).toHaveBeenCalledWith("processed_text", ""); - expect(mockSetOutput).toHaveBeenCalledWith("word_count", 0); - expect(mockSetOutput).toHaveBeenCalledWith("sum", 0); - expect(mockSetOutput).toHaveBeenCalledWith("average", 0); + expect(mockSetFailed).toHaveBeenCalledWith("Action failed with error: Config file not found"); + }); }); });