Skip to content

feat(tests): Add comprehensive unit tests for background and popup sc…#5

Merged
SIkebe merged 6 commits intomainfrom
unit-tests
Jul 13, 2025
Merged

feat(tests): Add comprehensive unit tests for background and popup sc…#5
SIkebe merged 6 commits intomainfrom
unit-tests

Conversation

@SIkebe
Copy link
Owner

@SIkebe SIkebe commented Jul 13, 2025

…ripts

This pull request introduces several enhancements to the project, including automated dependency updates, improved CI/CD workflows, expanded testing capabilities, and new mock utilities for unit testing. The changes aim to streamline development processes, increase test coverage, and improve maintainability.

Dependency Management and CI/CD Enhancements:

  • .github/dependabot.yml: Configured Dependabot to automate daily updates for npm dependencies and GitHub Actions workflows, grouping development and production dependencies for better management.
  • .github/workflows/ci.yml: Added a CI/CD pipeline to run unit tests, build the project, and provide a summary of test results for pull requests and commits to the main branch.

Testing Improvements:

  • package.json: Introduced multiple Jest test scripts (test, test:unit, test:integration, etc.) and added relevant testing dependencies (jest, ts-jest, @types/jest, etc.). [1] [2]
  • src/background.ts: Exported key functions for testing purposes, enabling better test coverage for the background script.
  • tests/setup/jest.setup.ts: Created a Jest setup file to mock the Chrome API globally, reduce console noise, and add custom matchers for streamlined testing.

Mock Utilities for Unit Testing:

  • tests/mocks/chrome-api.mock.ts: Implemented comprehensive Chrome API mocks, including tabs, windows, storage, and display mocks, to facilitate unit testing of Chrome extension features.
  • tests/mocks/popup-mock.ts: Added a mock for testing the popup script, simulating DOM interactions, accessibility updates, and Chrome API calls.

…ripts

- Implemented Chrome API mocks for testing purposes.
- Created a mock for the popup script to simulate DOM interactions.
- Developed unit tests for the background script, covering constants, error handling, and display bounds.
- Added unit tests for the popup script, including DOM initialization, language selection, and button functionality.
- Enhanced test utilities for better setup and teardown of test environments.
- Configured TypeScript for testing with specific compiler options.
@SIkebe SIkebe requested a review from Copilot July 13, 2025 14:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a comprehensive suite of unit tests and associated test infrastructure for both the background and popup scripts of the Split Translator extension. Key changes include:

  • Test configuration and setup: added tsconfig.test.json, Jest setup, and Chrome API mocks.
  • Shared utilities: introduced TestUtils for common test helpers and mock generation.
  • New unit tests: created detailed tests for popup behaviors, background script logic, and display/tab handling.
  • Adjusted production code: exported internal functions from background.ts to enable testing.

Reviewed Changes

Copilot reviewed 12 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tsconfig.test.json Added test-specific TypeScript configuration with performance optimizations
tests/utils/test-utils.ts Introduced common test utilities (DOM setup, mock generators, assertion helpers)
tests/unit/popup.test.ts Added popup script unit tests covering DOM, language selection, button interactions, keyboard navigation, status updates, and error handling
tests/unit/background.test.ts Added background script unit tests covering constants, error handling, dimension enforcement, tab readiness, display bounds, URL and tab validation
tests/setup/jest.setup.ts Configured Jest setup with Chrome API mocks, console suppression, custom matchers, and test environment reset
tests/mocks/popup-mock.ts Introduced PopupMock to simulate DOMContentLoaded and popup behaviors
tests/mocks/chrome-api.mock.ts Introduced ChromeApiMock for mocking Chrome API interactions in tests
src/background.ts Exported internal functions for testability
package.json Added test scripts and devDependencies for Jest and TS-Jest
.npmrc Removed unnecessary cache configuration
.github/workflows/ci.yml Configured CI to run unit and integration tests
.github/dependabot.yml Established Dependabot configuration for dependencies and GitHub Actions workflows
Comments suppressed due to low confidence (2)

tests/unit/popup.test.ts:60

  • The test asserts that chrome.storage.sync.get is called but does not verify that the select value is actually updated to the saved language. Consider adding an assertion on targetLanguageSelect.value.
    it('should load saved language preference', async () => {

src/background.ts:277

  • The export includes handleSplitAndTranslate which is not defined in this module; it should either export handleSplitView or define the missing function.
    handleSplitAndTranslate,

Comment on lines +169 to +176
await backgroundModule.waitForTabReady(123, 50);

const endTime = Date.now();
const elapsed = endTime - startTime;

// Should have waited approximately the timeout duration
expect(elapsed).toBeGreaterThanOrEqual(40); // Allow some margin
expect(elapsed).toBeLessThan(200); // But not too long
Copy link

Copilot AI Jul 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test uses real timers and will wait ~100ms per run, slowing down the suite. Consider using jest.useFakeTimers() and advancing the timer to speed up this timeout test.

Suggested change
await backgroundModule.waitForTabReady(123, 50);
const endTime = Date.now();
const elapsed = endTime - startTime;
// Should have waited approximately the timeout duration
expect(elapsed).toBeGreaterThanOrEqual(40); // Allow some margin
expect(elapsed).toBeLessThan(200); // But not too long
const promise = backgroundModule.waitForTabReady(123, 50);
jest.advanceTimersByTime(50);
await promise;
// Should have waited approximately the timeout duration

Copilot uses AI. Check for mistakes.
@SIkebe SIkebe merged commit 9bdf2e5 into main Jul 13, 2025
1 check passed
@SIkebe SIkebe deleted the unit-tests branch July 13, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants