test: fix lint errors in tests by using type-safe mocks and assertions

This commit is contained in:
ClaudiaFang 2026-04-25 03:27:10 +00:00
parent f354749d4c
commit 60e3790c73
2 changed files with 23 additions and 21 deletions

View file

@ -98,13 +98,13 @@ describe('SyncManager', () => {
// Capture the callback passed to the modal
let callback: (choice: 'local' | 'remote') => void = () => {};
modalMock.mockImplementation(function (this: any, app: App, file: TFile, local: string, remote: string, onChoose: (choice: 'local' | 'remote') => void) {
modalMock.mockImplementation(function (this: SyncConflictModal, app: App, fileName: string, local: string, remote: string, onChoose: (choice: 'local' | 'remote') => void) {
callback = onChoose;
this.open = vi.fn();
this.close = vi.fn();
this.app = app;
this.setTitle = vi.fn().mockReturnThis();
} as any);
(this as unknown as Record<string, unknown>).open = vi.fn();
(this as unknown as Record<string, unknown>).close = vi.fn();
(this as unknown as Record<string, unknown>).app = app;
(this as unknown as Record<string, unknown>).setTitle = vi.fn().mockReturnThis();
});
await manager.pushFile(mockFile);
@ -130,13 +130,13 @@ describe('SyncManager', () => {
const modalMock = vi.mocked(SyncConflictModal);
let callback: (choice: 'local' | 'remote') => void = () => {};
modalMock.mockImplementation(function (this: any, app: App, file: TFile, local: string, remote: string, onChoose: (choice: 'local' | 'remote') => void) {
modalMock.mockImplementation(function (this: SyncConflictModal, app: App, fileName: string, local: string, remote: string, onChoose: (choice: 'local' | 'remote') => void) {
callback = onChoose;
this.open = vi.fn();
this.close = vi.fn();
this.app = app;
this.setTitle = vi.fn().mockReturnThis();
} as any);
(this as unknown as Record<string, unknown>).open = vi.fn();
(this as unknown as Record<string, unknown>).close = vi.fn();
(this as unknown as Record<string, unknown>).app = app;
(this as unknown as Record<string, unknown>).setTitle = vi.fn().mockReturnThis();
});
await manager.pushFile(mockFile);

View file

@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GitHubService } from '../../src/services/github-service';
import { requestUrl, RequestUrlResponse } from 'obsidian';
import { requestUrl, RequestUrlResponse, RequestUrlParam } from 'obsidian';
describe('GitHubService', () => {
let service: GitHubService;
@ -60,10 +60,11 @@ describe('GitHubService', () => {
const result = await service.pushFile('new.md', 'new content', 'main', 'create');
expect(result).toBe('new.md');
expect(requestUrl).toHaveBeenLastCalledWith(expect.objectContaining({
method: 'PUT',
body: expect.not.stringContaining('"sha":')
}));
const calls = vi.mocked(requestUrl).mock.calls;
const lastCall = calls[calls.length - 1] ? (calls[calls.length - 1][0] as RequestUrlParam) : null;
if (!lastCall) throw new Error('lastCall is undefined');
expect(lastCall.method).toBe('PUT');
expect(lastCall.body).not.toContain('"sha":');
});
it('should update existing file correctly (sha provided)', async () => {
@ -75,10 +76,11 @@ describe('GitHubService', () => {
const result = await service.pushFile('existing.md', 'updated content', 'main', 'update', 'old-sha');
expect(result).toBe('existing.md');
expect(requestUrl).toHaveBeenCalledWith(expect.objectContaining({
method: 'PUT',
body: expect.stringContaining('"sha":"old-sha"')
}));
const calls = vi.mocked(requestUrl).mock.calls;
const lastCall = calls[calls.length - 1] ? (calls[calls.length - 1][0] as RequestUrlParam) : null;
if (!lastCall) throw new Error('lastCall is undefined');
expect(lastCall.method).toBe('PUT');
expect(lastCall.body).toContain('"sha":"old-sha"');
});
});
});