Skip to content

Commit

Permalink
tests: fix mocking path
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed May 9, 2024
1 parent 086f228 commit c316ba4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
36 changes: 17 additions & 19 deletions __tests__/utils/ExtensionFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ jest.mock("fs", () => ({
statSync: jest.fn()
}));

jest.mock("path", () => ({
join: jest.fn()
}));

const mockedFs = fs as jest.Mocked<typeof fs>;
const mockedPath = path as jest.Mocked<typeof path>;
const mockedPath = path;
describe("getFilesByExtension", () => {
beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
Expand All @@ -40,29 +36,30 @@ 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[]);

mockedFs.statSync.mockReturnValue({
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", () => {
Expand All @@ -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[]);

Expand All @@ -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"]);
});
});
2 changes: 2 additions & 0 deletions __tests__/utils/FileProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ jest.mock("fs", () => ({
promises: {
access: jest.fn()
},
readFileSync: jest.fn(),
writeFileSync: jest.fn()
}));

describe('FileProcessor', () => {
Expand Down
7 changes: 0 additions & 7 deletions src/utils/FileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c316ba4

Please sign in to comment.