Skip to content

Commit cfecb8e

Browse files
committed
testing
1 parent ed7282e commit cfecb8e

File tree

5 files changed

+135
-31
lines changed

5 files changed

+135
-31
lines changed

src/functions.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as Functions from './functions';
2+
3+
describe('func1', () => {
4+
it('returns the mocked value of func2 when called', () => {
5+
// Spy on func2 and mock its return value
6+
const spy = jest.spyOn(Functions, 'func2').mockReturnValue("Mocked func2 value");
7+
8+
// Call func1, which internally calls the mocked func2
9+
const result = Functions.func1();
10+
11+
// Verify that the mocked version of func2 is called
12+
expect(result).toBe("Result: Mocked func2 value");
13+
expect(spy).toHaveBeenCalled();
14+
});
15+
});

src/functions.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//WORKS
2+
// functions.js
3+
const functions = {
4+
func2: () => "Original func2 value",
5+
func1: function() {
6+
return `Result: ${this.func2()}`;
7+
}
8+
};
9+
export const { func1, func2 } = functions;
10+
11+
/*
12+
DOESN'T WORK
13+
14+
export function func2() {
15+
return "Original func2 value";
16+
}
17+
18+
export function func1() {
19+
return `Result: ${func2()}`;
20+
}
21+
*/

src/update-changelog.test.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,61 @@
11
import * as ChangeLogManager from './update-changelog';
22

3-
const uncategorizedChangelog = `# Changelog
3+
4+
const emptyChangelog = `# Changelog
45
All notable changes to this project will be documented in this file.
56
67
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
78
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
89
910
## [Unreleased]
1011
11-
## [1.0.0]
12-
### Uncategorized
13-
- fix: a commit of the type fix patches a bug in your codebase
14-
- feat: a commit of the type feat introduces a new feature to the codebase
15-
- unknown: a commit of the type unknown does not match any of the conventional commit types
16-
1712
[Unreleased]: https://github.com/ExampleUsernameOrOrganization/ExampleRepository/
1813
`;
1914

2015
describe('updateChangelog', () => {
21-
it('should contain conventional support mappings categorization', async () => {
22-
// Call updateChangelog, which internally calls the mocked getNewChangeEntries
16+
let getNewChangeEntriesSpy: jest.SpyInstance;
17+
18+
beforeEach(() => {
19+
// Setup the spy and mock before each test
20+
// Assuming getNewChangeEntries returns a Promise<string[]>
21+
getNewChangeEntriesSpy = jest.spyOn(ChangeLogManager, 'getNewChangeEntries')
22+
.mockResolvedValue([
23+
'fix: fixed a major bug',
24+
'feat: introduced a new feature',
25+
'unknown: some non-conventional commit'
26+
]);
27+
});
28+
29+
afterEach(() => {
30+
// Restore the original function after each test
31+
getNewChangeEntriesSpy.mockRestore();
32+
});
33+
34+
it('should contain conventional support mappings categorization when autoCategorize is true', async () => {
2335
const result = await ChangeLogManager.updateChangelog({
24-
changelogContent: uncategorizedChangelog,
36+
changelogContent: emptyChangelog,
2537
currentVersion: '1.0.0',
26-
repoUrl:
27-
'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
38+
repoUrl: 'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
2839
isReleaseCandidate: true,
2940
autoCategorize: true,
3041
});
3142

32-
console.log(result);
33-
34-
// Verify that some of the commits are included in the result
3543
expect(result).toContain('### Fixed');
44+
expect(result).toContain('### Added');
45+
expect(result).not.toContain('### Uncategorized');
3646
});
3747

38-
it('should not contain conventional support mappings categorization', async () => {
39-
// Call updateChangelog, which internally calls the mocked getNewChangeEntries
48+
it('should not contain conventional support mappings categorization when autoCategorize is false', async () => {
4049
const result = await ChangeLogManager.updateChangelog({
41-
changelogContent: uncategorizedChangelog,
50+
changelogContent: emptyChangelog,
4251
currentVersion: '1.0.0',
43-
repoUrl:
44-
'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
52+
repoUrl: 'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
4553
isReleaseCandidate: true,
4654
autoCategorize: false,
4755
});
4856

49-
// Verify that some of the commits are included in the result
5057
expect(result).toContain('### Uncategorized');
5158
expect(result).not.toContain('### Fixed');
59+
expect(result).not.toContain('### Added');
5260
});
5361
});

src/update-changelog.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@ export async function updateChangelog({
267267
packageRename,
268268
});
269269

270-
console.log(`autoCategorize: ${autoCategorize}`);
271-
272270
const mostRecentTag = await getMostRecentTag({
273271
tagPrefixes,
274272
});
@@ -308,8 +306,6 @@ export async function updateChangelog({
308306
projectRootDirectory,
309307
});
310308

311-
console.log(`count of newChangeEntries: ${newChangeEntries.length}`);
312-
313309
for (const description of newChangeEntries.reverse()) {
314310
const category = autoCategorize
315311
? getCategory(description)
@@ -349,24 +345,18 @@ async function runCommand(command: string, args: string[]): Promise<string[]> {
349345
* @returns The category of the change.
350346
*/
351347
function getCategory(description: string): ChangeCategory {
352-
console.log(`description: ${description}`);
353348
// Check if description contains a colon
354349
if (description.includes(':')) {
355350
const [prefix] = description.split(':').map((part) => part.trim());
356351
switch (prefix) {
357352
case ConventionalCommitType.Feat:
358-
console.log(`mapping feat to Added`);
359353
return ChangeCategory.Added;
360354
case ConventionalCommitType.Fix:
361-
console.log(`mapping fix to Fixed`);
362355
return ChangeCategory.Fixed;
363356
default:
364-
console.log(`mapping ${prefix} to Uncategorized`);
365357
return ChangeCategory.Uncategorized;
366358
}
367359
}
368-
369-
console.log(`mapping as desc didn't contain : Uncategorized`);
370360
// Return 'Uncategorized' if no colon is found or prefix doesn't match
371361
return ChangeCategory.Uncategorized;
372362
}

src/update-changelogv2.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Place this at the very top of your test file
2+
jest.mock('./update-changelog', () => {
3+
const originalModule = jest.requireActual('./update-changelog'); // Get the actual module
4+
return {
5+
...originalModule, // spread all actual exports
6+
getNewChangeEntries: jest.fn().mockResolvedValue([
7+
'fix: Fixed a critical bug',
8+
'feat: Added new feature [PR#123](https://github.com/ExampleUsernameOrOrganization/ExampleRepository/pull/123)'
9+
])
10+
};
11+
});
12+
13+
import * as ChangeLogManager from './update-changelog';
14+
15+
16+
const emptyChangelog = `# Changelog
17+
All notable changes to this project will be documented in this file.
18+
19+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
20+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
21+
22+
## [Unreleased]
23+
24+
[Unreleased]: https://github.com/ExampleUsernameOrOrganization/ExampleRepository/
25+
`;
26+
27+
describe('updateChangelog', () => {
28+
29+
it('should contain conventional support mappings categorization when autoCategorize is true', async () => {
30+
31+
// TRIED WITH AND WITHOUT THIS LINE
32+
jest.spyOn(ChangeLogManager, 'getNewChangeEntries').mockResolvedValue([
33+
'fix: Fixed a critical bug',
34+
'feat: Added new feature [PR#123](https://github.com/ExampleUsernameOrOrganization/ExampleRepository/pull/123)'
35+
]);
36+
37+
const result = await ChangeLogManager.updateChangelog({
38+
changelogContent: emptyChangelog,
39+
currentVersion: '1.0.0',
40+
repoUrl: 'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
41+
isReleaseCandidate: true,
42+
autoCategorize: true,
43+
});
44+
45+
expect(result).toContain('### Fixed');
46+
expect(result).toContain('### Added');
47+
expect(result).not.toContain('### Uncategorized');
48+
});
49+
50+
it('should not contain conventional support mappings categorization when autoCategorize is false', async () => {
51+
52+
// TRIED WITH AND WITHOUT THIS LINE
53+
jest.spyOn(ChangeLogManager, 'getNewChangeEntries').mockResolvedValue([
54+
'fix: Fixed a critical bug',
55+
'feat: Added new feature [PR#123](https://github.com/ExampleUsernameOrOrganization/ExampleRepository/pull/123)'
56+
]);
57+
58+
const result = await ChangeLogManager.updateChangelog({
59+
changelogContent: emptyChangelog,
60+
currentVersion: '1.0.0',
61+
repoUrl: 'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
62+
isReleaseCandidate: true,
63+
autoCategorize: false,
64+
});
65+
66+
expect(result).toContain('### Uncategorized');
67+
expect(result).not.toContain('### Fixed');
68+
expect(result).not.toContain('### Added');
69+
});
70+
});

0 commit comments

Comments
 (0)