From c316ba4066ecfab8bca0e24b9f7b284561ad8878 Mon Sep 17 00:00:00 2001 From: "Breno A." Date: Thu, 9 May 2024 04:49:46 -0300 Subject: [PATCH] tests: fix mocking path --- __tests__/utils/ExtensionFilter.test.ts | 36 ++++++++++++------------- __tests__/utils/FileProcessor.test.ts | 2 ++ src/utils/FileProcessor.ts | 7 ----- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/__tests__/utils/ExtensionFilter.test.ts b/__tests__/utils/ExtensionFilter.test.ts index ea53ef3..27d9026 100644 --- a/__tests__/utils/ExtensionFilter.test.ts +++ b/__tests__/utils/ExtensionFilter.test.ts @@ -10,12 +10,8 @@ jest.mock("fs", () => ({ statSync: jest.fn() })); -jest.mock("path", () => ({ - join: jest.fn() -})); - const mockedFs = fs as jest.Mocked; -const mockedPath = path as jest.Mocked; +const mockedPath = path; describe("getFilesByExtension", () => { beforeEach(() => { // Clear all instances and calls to constructor and all methods: @@ -40,17 +36,20 @@ describe("getFilesByExtension", () => { mockedFs.readdirSync.mockReturnValueOnce([ { name: "subdir", - isDirectory: () => true + isDirectory: () => true, + isFile: () => false }, { name: "file1.prp.ts", - isDirectory: () => false + isDirectory: () => false, + isFile: () => true } ] as fs.Dirent[]); mockedFs.readdirSync.mockReturnValueOnce([ { name: "file2.prp.ts", - isDirectory: () => false + isDirectory: () => false, + isFile: () => true } ] as fs.Dirent[]); @@ -58,11 +57,9 @@ describe("getFilesByExtension", () => { isDirectory: () => false } as fs.Stats); - mockedPath.join.mockImplementation((...paths: string[]) => paths.join("/")); - const files = getFilesByExtension({ dir, extension, fsModule: mockedFs, pathModule: mockedPath }); - expect(files).toEqual(["./subdir/file2.prp.ts", "./file1.prp.ts"]); + expect(files).toEqual(["subdir\\file2.prp.ts", "file1.prp.ts"]); }); it("should throw an error when the directory does not exist", () => { @@ -89,19 +86,23 @@ describe("getFilesByExtension", () => { mockedFs.readdirSync.mockReturnValue([ { name: "file1.ts", - isDirectory: () => false + isDirectory: () => false, + isFile: () => true }, { name: "file2.js", - isDirectory: () => false + isDirectory: () => false, + isFile: () => true }, { name: "file3.prp.ts", - isDirectory: () => false + isDirectory: () => false, + isFile: () => true }, { name: "file3.prp.md", - isDirectory: () => false + isDirectory: () => false, + isFile: () => true } ] as fs.Dirent[]); @@ -110,11 +111,8 @@ describe("getFilesByExtension", () => { isDirectory: () => false } as fs.Stats); - // Mock path.join to return the file path - mockedPath.join.mockImplementation((...paths: string[]) => paths.join("/")); - const files = getFilesByExtension({ dir, extension, fsModule: mockedFs, pathModule: mockedPath }); - expect(files).toEqual(["./file3.prp.ts", "./file3.prp.md"]); + expect(files).toEqual(["file3.prp.ts", "file3.prp.md"]); }); }); \ No newline at end of file diff --git a/__tests__/utils/FileProcessor.test.ts b/__tests__/utils/FileProcessor.test.ts index fb61933..cdd76d4 100644 --- a/__tests__/utils/FileProcessor.test.ts +++ b/__tests__/utils/FileProcessor.test.ts @@ -5,6 +5,8 @@ jest.mock("fs", () => ({ promises: { access: jest.fn() }, + readFileSync: jest.fn(), + writeFileSync: jest.fn() })); describe('FileProcessor', () => { diff --git a/src/utils/FileProcessor.ts b/src/utils/FileProcessor.ts index 8270b7b..c5b0ec9 100644 --- a/src/utils/FileProcessor.ts +++ b/src/utils/FileProcessor.ts @@ -5,14 +5,7 @@ export function processFiles(params: FilesParams): void { const { files, encodings, variables, fsModule = fs, extension } = params; files.forEach(file => { - try { processFile(file); - } catch (error) { - core.error(`Error processing file: ${file}`); - if (error instanceof Error) { - core.error(error.message); - } - } }); function processFile(file: string) {