Skip to content

Commit ad843a3

Browse files
harshhgithubHarsh Shukla
andauthored
test: add tests for cleanTestResultPaths helper function (#1676)
Co-authored-by: Harsh Shukla <you@example.com>
1 parent a7c3a53 commit ad843a3

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

packages/helpers/test/testing.test.js

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { listFiles, buildParams, hasNestedConfig } = require('@asyncapi/generator-helpers');
2-
const fs = require('fs/promises');
1+
const { listFiles, buildParams, hasNestedConfig, cleanTestResultPaths } = require('@asyncapi/generator-helpers');
2+
const { rm, readdir } = require('fs/promises');
33

44
jest.mock('fs/promises', () => ({
55
rm: jest.fn(),
@@ -18,16 +18,16 @@ describe('listFiles', () => {
1818
{ name: 'subdir', isFile: () => false },
1919
];
2020

21-
fs.readdir.mockResolvedValue(mockDirents);
21+
readdir.mockResolvedValue(mockDirents);
2222
const mockPath = '/mock/path';
2323

2424
const result = await listFiles(mockPath);
25-
expect(fs.readdir).toHaveBeenCalledWith(mockPath, { withFileTypes: true });
25+
expect(readdir).toHaveBeenCalledWith(mockPath, { withFileTypes: true });
2626
expect(result).toEqual(['file1.txt', 'file2.js']);
2727
});
2828

2929
it('should return an empty array if no files exist', async () => {
30-
fs.readdir.mockResolvedValue([
30+
readdir.mockResolvedValue([
3131
{ name: 'folder', isFile: () => false },
3232
]);
3333
const mockPath = '/mock/path';
@@ -36,6 +36,68 @@ describe('listFiles', () => {
3636
});
3737
});
3838

39+
describe('cleanTestResultPaths', () => {
40+
beforeEach(() => {
41+
jest.clearAllMocks();
42+
});
43+
44+
it('should remove path when testResultPath exists', async () => {
45+
const config = { testResultPath: '/tmp/test-results' };
46+
47+
await cleanTestResultPaths(config);
48+
49+
expect(rm).toHaveBeenCalledWith('/tmp/test-results', { recursive: true, force: true });
50+
});
51+
52+
it('should not call rm when config is null/undefined', async () => {
53+
await cleanTestResultPaths(null);
54+
await cleanTestResultPaths(undefined);
55+
56+
expect(rm).not.toHaveBeenCalled();
57+
});
58+
59+
it('should warn when testResultPath is missing and no nested config', async () => {
60+
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
61+
const config = { someKey: 'value' };
62+
63+
await cleanTestResultPaths(config);
64+
65+
expect(consoleSpy).toHaveBeenCalledWith(
66+
'Configuration missing testResultPath - no test results to clean:',
67+
config
68+
);
69+
70+
consoleSpy.mockRestore();
71+
});
72+
73+
it('should recurse into nested configs and clean their paths', async () => {
74+
const config = {
75+
nested: {
76+
testResultPath: '/tmp/nested-results',
77+
},
78+
};
79+
80+
await cleanTestResultPaths(config);
81+
82+
expect(rm).toHaveBeenCalledWith('/tmp/nested-results', { recursive: true, force: true });
83+
});
84+
85+
it('should handle rm throwing an error gracefully', async () => {
86+
rm.mockRejectedValueOnce(new Error('Permission denied'));
87+
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
88+
89+
const config = { testResultPath: '/tmp/fail-results' };
90+
91+
await cleanTestResultPaths(config);
92+
93+
expect(consoleSpy).toHaveBeenCalledWith(
94+
expect.stringContaining('Failed to clean /tmp/fail-results: Permission denied')
95+
);
96+
97+
consoleSpy.mockRestore();
98+
});
99+
});
100+
39101
describe('hasNestedConfig', () => {
40102
it('should return false for an empty object', () => {
41103
expect(hasNestedConfig({})).toBe(false);
@@ -87,6 +149,7 @@ describe('hasNestedConfig', () => {
87149
expect(hasNestedConfig(config)).toBe(true);
88150
});
89151
});
152+
90153
describe('buildParams', () => {
91154
it('should include clientFileName when language is not java', () => {
92155
const config = { clientFileName: 'myClient.js' };
@@ -139,4 +202,4 @@ describe('buildParams', () => {
139202
clientFileName: 'client.js',
140203
});
141204
});
142-
});
205+
});

0 commit comments

Comments
 (0)