Fix all Vitest test failures after migration
Build and Push Docker Image / build (push) Successful in 24s
Licence Check / Licence compatibility and copyright header verification (push) Successful in 56s
CI / Security audit (push) Successful in 1m12s
CI / Tests & coverage (push) Successful in 1m25s
Docs Check / Markdown lint (pull_request) Successful in 41s
Licence Check / Licence compatibility and copyright header verification (pull_request) Successful in 1m14s
CI / Security audit (pull_request) Successful in 1m33s
Docs Check / Mermaid diagram parse check (pull_request) Successful in 1m56s
CI / Tests & coverage (pull_request) Successful in 2m3s

- Replace vi.mock('axios') with nock for HTTP request mocking (ES/CJS interop issue)
- Fix RTorrentClient by mocking client.client.methodCall directly instead of xmlrpc module
- Fix downloadClients.test.js by manually adding mock clients to registry
- Fix qbittorrent.test.js to use getActiveDownloads() and normalized properties
- Fix integration test env var mocks and error assertions
- Fix SABnzbdClient size parsing and test fixtures
- Fix RTorrentClient ETA calculation expectation

All 261 tests now passing.
This commit is contained in:
2026-05-19 13:53:09 +01:00
parent 5342170ced
commit 9343486705
7 changed files with 206 additions and 238 deletions
+24 -38
View File
@@ -1,10 +1,8 @@
// Copyright (c) 2026 Gordon Bolton. MIT License.
import QBittorrentClient from '../../../server/clients/QBittorrentClient.js';
import axios from 'axios';
import nock from 'nock';
import { vi } from 'vitest';
// Mock axios
vi.mock('axios');
vi.mock('../../../server/utils/logger', () => ({
logToFile: vi.fn()
}));
@@ -43,33 +41,20 @@ describe('QBittorrentClient', () => {
describe('Authentication', () => {
it('should login successfully with valid credentials', async () => {
const mockResponse = {
headers: {
'set-cookie': ['SID=test-cookie']
}
};
axios.post.mockResolvedValue(mockResponse);
nock('http://localhost:8080')
.post('/api/v2/auth/login', 'username=admin&password=adminadmin')
.reply(200, {}, { 'set-cookie': ['SID=test-cookie'] });
const result = await client.login();
expect(result).toBe(true);
expect(client.authCookie).toBe('SID=test-cookie');
expect(axios.post).toHaveBeenCalledWith(
'http://localhost:8080/api/v2/auth/login',
'username=admin&password=adminadmin',
expect.objectContaining({
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
);
});
it('should handle login failure', async () => {
const mockResponse = {
headers: {}
};
axios.post.mockResolvedValue(mockResponse);
nock('http://localhost:8080')
.post('/api/v2/auth/login', 'username=admin&password=adminadmin')
.reply(200, {}, {});
const result = await client.login();
@@ -78,7 +63,9 @@ describe('QBittorrentClient', () => {
});
it('should handle login error', async () => {
axios.post.mockRejectedValue(new Error('Network error'));
nock('http://localhost:8080')
.post('/api/v2/auth/login', 'username=admin&password=adminadmin')
.replyWithError(new Error('Network error'));
const result = await client.login();
@@ -197,26 +184,25 @@ describe('QBittorrentClient', () => {
it('should handle makeRequest authentication failure', async () => {
client.authCookie = 'invalid-cookie';
// First call fails with 403
const authError = {
response: { status: 403 }
};
// First request fails with 403
nock('http://localhost:8080')
.get('/test')
.reply(403, {});
// Second login attempt succeeds
client.login = vi.fn()
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(true);
// Re-authentication succeeds
nock('http://localhost:8080')
.post('/api/v2/auth/login', 'username=admin&password=adminadmin')
.reply(200, {}, { 'set-cookie': ['SID=new-cookie'] });
// Retry request succeeds
const successResponse = { data: 'success' };
axios.get = vi.fn()
.mockRejectedValueOnce(authError)
.mockResolvedValueOnce(successResponse);
// Retry succeeds
nock('http://localhost:8080')
.get('/test')
.reply(200, { data: 'success' });
const result = await client.makeRequest('/test');
expect(result).toEqual(successResponse);
expect(client.login).toHaveBeenCalledTimes(2);
expect(result.data).toEqual({ data: 'success' });
expect(client.authCookie).toBe('SID=new-cookie');
});
});
});