Skip to content

Commit 7bb28e4

Browse files
cristotodevclaude
andcommitted
test: improve proveedores test robustness for CI environments
- Clear mocks before creating new instances to ensure clean state - Add explicit mock call count assertions to catch timing issues - Extract mock references to variables for better test stability - Add small initialization delay to handle CI timing differences - These changes should resolve intermittent test failures on GitHub CI 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 598cd7e commit 7bb28e4

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

tests/unit/modules/core-business/proveedores.test.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ describe('ProveedoresResource', () => {
1010
let mockClient: FacturaScriptsClient;
1111
let proveedoresResource: ProveedoresResource;
1212

13-
beforeEach(() => {
13+
beforeEach(async () => {
14+
vi.clearAllMocks();
1415
mockClient = new FacturaScriptsClient();
1516
proveedoresResource = new ProveedoresResource(mockClient);
16-
vi.clearAllMocks();
17+
// Small delay to ensure proper initialization in CI environments
18+
await new Promise(resolve => setTimeout(resolve, 1));
1719
});
1820

1921
describe('matchesUri', () => {
@@ -65,11 +67,13 @@ describe('ProveedoresResource', () => {
6567
};
6668

6769
it('should return proveedores data with default pagination', async () => {
68-
vi.mocked(mockClient.getWithPagination).mockResolvedValue(mockPaginatedResponse);
70+
const mockGetWithPagination = vi.mocked(mockClient.getWithPagination);
71+
mockGetWithPagination.mockResolvedValue(mockPaginatedResponse);
6972

7073
const result = await proveedoresResource.getResource('facturascripts://proveedores');
7174

72-
expect(mockClient.getWithPagination).toHaveBeenCalledWith('/proveedores', 50, 0, {});
75+
expect(mockGetWithPagination).toHaveBeenCalledTimes(1);
76+
expect(mockGetWithPagination).toHaveBeenCalledWith('/proveedores', 50, 0, {});
7377
expect(result).toEqual({
7478
uri: 'facturascripts://proveedores',
7579
name: 'FacturaScripts Proveedores',
@@ -85,42 +89,51 @@ describe('ProveedoresResource', () => {
8589
});
8690

8791
it('should parse and use limit and offset from URI', async () => {
88-
vi.mocked(mockClient.getWithPagination).mockResolvedValue({
92+
const mockGetWithPagination = vi.mocked(mockClient.getWithPagination);
93+
const modifiedResponse = {
8994
...mockPaginatedResponse,
9095
meta: { ...mockPaginatedResponse.meta, limit: 10, offset: 20 },
91-
});
96+
};
97+
mockGetWithPagination.mockResolvedValue(modifiedResponse);
9298

9399
const result = await proveedoresResource.getResource('facturascripts://proveedores?limit=10&offset=20');
94100

95-
expect(mockClient.getWithPagination).toHaveBeenCalledWith('/proveedores', 10, 20, {});
101+
expect(mockGetWithPagination).toHaveBeenCalledTimes(1);
102+
expect(mockGetWithPagination).toHaveBeenCalledWith('/proveedores', 10, 20, {});
96103
expect(result.contents[0].text).toContain('"limit": 10');
97104
expect(result.contents[0].text).toContain('"offset": 20');
98105
});
99106

100107
it('should handle invalid limit and offset parameters', async () => {
101-
vi.mocked(mockClient.getWithPagination).mockResolvedValue(mockPaginatedResponse);
108+
const mockGetWithPagination = vi.mocked(mockClient.getWithPagination);
109+
mockGetWithPagination.mockResolvedValue(mockPaginatedResponse);
102110

103111
await proveedoresResource.getResource('facturascripts://proveedores?limit=invalid&offset=also-invalid');
104112

105-
expect(mockClient.getWithPagination).toHaveBeenCalledWith('/proveedores', 50, 0, {});
113+
expect(mockGetWithPagination).toHaveBeenCalledTimes(1);
114+
expect(mockGetWithPagination).toHaveBeenCalledWith('/proveedores', 50, 0, {});
106115
});
107116

108117
it('should handle API errors gracefully', async () => {
109118
const errorMessage = 'API connection failed';
110-
vi.mocked(mockClient.getWithPagination).mockRejectedValue(new Error(errorMessage));
119+
const mockGetWithPagination = vi.mocked(mockClient.getWithPagination);
120+
mockGetWithPagination.mockRejectedValue(new Error(errorMessage));
111121

112122
const result = await proveedoresResource.getResource('facturascripts://proveedores');
113123

124+
expect(mockGetWithPagination).toHaveBeenCalledTimes(1);
114125
expect(result.name).toBe('FacturaScripts Proveedores (Error)');
115126
expect(result.contents[0].text).toContain('Failed to fetch proveedores');
116127
expect(result.contents[0].text).toContain(errorMessage);
117128
});
118129

119130
it('should handle non-Error exceptions', async () => {
120-
vi.mocked(mockClient.getWithPagination).mockRejectedValue('String error');
131+
const mockGetWithPagination = vi.mocked(mockClient.getWithPagination);
132+
mockGetWithPagination.mockRejectedValue('String error');
121133

122134
const result = await proveedoresResource.getResource('facturascripts://proveedores');
123135

136+
expect(mockGetWithPagination).toHaveBeenCalledTimes(1);
124137
expect(result.name).toBe('FacturaScripts Proveedores (Error)');
125138
expect(result.contents[0].text).toContain('Unknown error');
126139
});

0 commit comments

Comments
 (0)