diff --git a/package.json b/package.json index ab901f0..3d16eae 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "zip": "wxt zip", "test": "vitest", "test:e2e": "playwright test", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json" }, "dependencies": { "@wxt-dev/module-react": "^1.1.5", diff --git a/tests/unit/background.test.ts b/tests/unit/background.test.ts deleted file mode 100644 index 0109517..0000000 --- a/tests/unit/background.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { describe, it, expect, vi, beforeEach } from 'vitest'; - -describe('Background Service Worker', () => { - beforeEach(() => { - vi.clearAllMocks(); - }); - - it('should return error when no API key is configured', async () => { - (chrome.storage.local.get as any).mockImplementation((keys: any, cb: any) => cb({})); - - // Simulate the handleAnalyzeText function behavior - const config = await new Promise((resolve) => - chrome.storage.local.get(['provider', 'apiKey', 'model'], resolve) - ); - - expect(config.apiKey).toBeUndefined(); - }); - - it('should store API key correctly', async () => { - const testConfig = { provider: 'openai', apiKey: 'sk-test123', model: 'gpt-4o-mini' }; - - await new Promise((resolve) => - chrome.storage.local.set(testConfig, resolve) - ); - - expect(chrome.storage.local.set).toHaveBeenCalledWith(testConfig, expect.any(Function)); - }); -}); diff --git a/tests/unit/setup.ts b/tests/unit/setup.ts index b62c696..251fcaa 100644 --- a/tests/unit/setup.ts +++ b/tests/unit/setup.ts @@ -1,9 +1,15 @@ -// Mock Chrome extension APIs +// Baseline Chrome extension API mock. Individual tests may rebuild +// global.chrome to control specifics (e.g. runtime.id presence). global.chrome = { storage: { local: { get: vi.fn((keys, cb) => cb({})), set: vi.fn((data, cb) => cb && cb()), + remove: vi.fn((keys, cb) => cb && cb()), + }, + session: { + get: vi.fn((keys, cb) => cb({})), + set: vi.fn((data, cb) => cb && cb()), }, sync: { get: vi.fn((keys, cb) => cb({})), @@ -11,10 +17,24 @@ global.chrome = { }, }, runtime: { + id: 'test-extension-id', sendMessage: vi.fn(), onMessage: { addListener: vi.fn(), }, + onInstalled: { + addListener: vi.fn(), + }, openOptionsPage: vi.fn(), }, + contextMenus: { + create: vi.fn(), + removeAll: vi.fn((cb) => cb && cb()), + onClicked: { + addListener: vi.fn(), + }, + }, + tabs: { + sendMessage: vi.fn(), + }, } as any; diff --git a/tsconfig.test.json b/tsconfig.test.json index 408a1a6..04d74ae 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "types": ["vitest/globals"] + "types": ["chrome", "vitest/globals"] }, "include": [ "tests/**/*"