Skip to content

Commit

Permalink
Merge pull request #689 from novice1993/main
Browse files Browse the repository at this point in the history
[vscode/engine] getGitLog 함수 관련 테스트 코드 구현
  • Loading branch information
novice1993 authored Oct 6, 2024
2 parents 68d4aaa + 24d53e9 commit 9f68406
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 11 deletions.
189 changes: 180 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/vscode/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const config = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/", "/out/"],
verbose: true,
rootDir: "./",
};

export default config;
5 changes: 4 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"pretest": "npm run compile-tests && npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"lint:fix": "eslint src --ext ts --fix",
"test": "node ./out/test/runTest.js"
"test": "jest"
},
"dependencies": {
"@githru-vscode-ext/analysis-engine": "^0.7.1",
Expand All @@ -90,6 +90,7 @@
},
"devDependencies": {
"@types/glob": "^7.2.0",
"@types/jest": "^29.5.12",
"@types/mocha": "^9.1.1",
"@types/node": "14.x",
"@types/vscode": "^1.67.0",
Expand All @@ -107,8 +108,10 @@
"eslint-plugin-unused-imports": "^3.0.0",
"formdata-polyfill": "^4.0.10",
"glob": "^8.0.1",
"jest": "^29.7.0",
"mocha": "^9.2.2",
"prettier": "^3.0.1",
"ts-jest": "^29.2.5",
"ts-loader": "^9.2.8",
"typescript": "^4.6.4",
"webpack": "^5.70.0",
Expand Down
File renamed without changes.
61 changes: 61 additions & 0 deletions packages/vscode/src/test/utils/getGitLog.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as cp from "child_process";

import { getGitLog } from "../../utils/git.util";

const generateMockGitLogData = (index: number) => `
commit ${index}1234567890abcdef1234567890abcdef${index}5678 (HEAD -> main)
Author: Mock User ${index} <mock${index}@example.com>
AuthorDate: Mon Sep ${index} 21:42:00 2023 +0000
Commit: Mock Committer ${index} <committer${index}@example.com>
CommitDate: Mon Sep ${index} 21:43:00 2023 +0000
Commit message ${index}
`;

jest.mock("child_process");
const mockSpawn = cp.spawn as jest.Mock;

let mockSpawnCallCount = 0;

mockSpawn.mockImplementation(() => {
return {
stdout: {
on: jest.fn((event, callback) => {
if (event === "data") {
const mockData = generateMockGitLogData(mockSpawnCallCount);
callback(Buffer.from(mockData));
mockSpawnCallCount++;
}
if (event === "close") {
callback();
}
}),
},
stderr: {
on: jest.fn((event, callback) => {
callback(Buffer.from("mocked error message"));
}),
},
on: jest.fn((event, callback) => {
if (event === "exit") {
callback(0);
}
}),
};
});

describe("getGitLog util test", () => {
afterEach(() => {
mockSpawnCallCount = 0; // initailize call count
});

it("should return the combined git log output from number of threads", async () => {
const result = await getGitLog("git", "/mocked/path/to/repo");

const expectedData = Array.from({ length: mockSpawnCallCount }) // Create an array with length equal to call count
.map((_, index) => generateMockGitLogData(index)) // Insert mock data into the array for each index
.join(""); // Concatenate all mock data into a single string

return expect(result).toEqual(expectedData);
});
});
2 changes: 1 addition & 1 deletion packages/vscode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"outDir": "dist",
"lib": ["ES2020"],
"sourceMap": true,
"rootDir": "src",
"rootDir": "./src",
"strict": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
Expand Down

0 comments on commit 9f68406

Please sign in to comment.