๐งช Testing Guide¶
This document provides a comprehensive overview of the testing strategy, frameworks, and specific tests for the YT re:Watch extension.
๐ Running Tests¶
All tests can be executed using the following command from the project root:
npm test
This command will run all test suites, including unit, integration, memory, and end-to-end tests.
๐ ๏ธ Frameworks¶
- Jest: Primary framework for unit, integration, and memory tests. Uses the
jsdomenvironment with custom browser/extension mocks. - Playwright: Used for end-to-end (E2E) testing. It allows for testing the extension in a real browser environment (Chromium/Firefox/WebKit) to simulate user interactions accurately.
๐ฌ Test Categories¶
End-to-End (E2E) Tests (/tests/e2e)¶
E2E tests simulate real user workflows from start to finish. They run the extension in a headless browser to validate the complete user experience.
global-setup.js/global-teardown.js: These files handle the setup and teardown for the Playwright test environment. The setup script launches a persistent browser context with the extension loaded, and the teardown script closes it.extension.e2e.test.js: Contains the main E2E test suite. It verifies core extension functionality, such as:- The popup window opens correctly.
- The history view displays tracked videos.
- Settings can be changed and persisted.
- Search and filter functionalities in the popup work as expected.
Integration Tests (/tests/integration)¶
Integration tests focus on the interactions between different components of the extension.
video-tracking.test.js: This suite tests the interaction between thecontent.jsscript and the storage layer. It ensures that video progress is correctly tracked and saved under various conditions, such as page navigation and video playback events.
Unit Tests (/tests/unit)¶
Unit tests verify the functionality of individual modules or components in isolation.
popup.test.js:- Purpose: Tests the UI logic in
popup.js. -
Key Scenarios:
- Internationalization (i18n): Ensures the UI correctly displays translated strings by mocking the
chrome.i18n.getMessageAPI. - Basic popup layout (button ordering, initial sync indicator state).
- Clear history UX (confirmation, use of
clearHistoryOnly, UI refresh). - URL helpers like
addTimestampToUrlused when opening videos from the popup. - Import/export flows (
exportHistory,openImportPage) including JSON structure and browser integration (blob download, YouTube#ytlh_importtab). - Storage change listener behavior and sync status message handling.
- Internationalization (i18n): Ensures the UI correctly displays translated strings by mocking the
-
storage.test.js: - Purpose: Tests the hybrid storage system (
SimpleStorage/ytStorage) and how it interacts withchrome.storage.localand IndexedDB. -
Key Scenarios:
- Local-first writes for videos (
setVideo) and playlists. - Hybrid reads:
getVideopreferringstorage.local, then falling back to IndexedDB. - Hybrid deletion:
removeVideoremoving from local storage, calling IndexedDB delete with tombstone creation, and writing legacydeleted_video_*markers. - Merged views:
getAllVideoscombining IndexedDB base data with a local overlay where local wins on newer timestamps.
- Local-first writes for videos (
-
utils.test.js: - Purpose: Tests various utility and helper functions.
- Key Scenarios:
- Time formatting functions.
- Data sorting and filtering logic.
- URL parsing and video ID extraction.
Memory Tests (/tests/memory)¶
Memory tests are designed to identify potential memory leaks or excessive resource consumption.
cleanup.test.js:- Purpose: Verifies that DOM elements and event listeners created by the
content.jsscript are properly cleaned up when they are no longer needed (e.g., during YouTube's SPA navigations). This prevents memory leaks and ensures the extension remains performant over long browsing sessions.