feat(tests): Add comprehensive unit tests for background and popup sc…#5
Merged
feat(tests): Add comprehensive unit tests for background and popup sc…#5
Conversation
…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.
Contributor
There was a problem hiding this comment.
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
TestUtilsfor 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.tsto 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.getis called but does not verify that the select value is actually updated to the saved language. Consider adding an assertion ontargetLanguageSelect.value.
it('should load saved language preference', async () => {
src/background.ts:277
- The export includes
handleSplitAndTranslatewhich is not defined in this module; it should either exporthandleSplitViewor define the missing function.
handleSplitAndTranslate,
tests/unit/background.test.ts
Outdated
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 |
There was a problem hiding this comment.
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…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 fornpmdependencies 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 themainbranch.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.