Testing Framework & Mocks
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
MarketSage utilizes a robust testing ecosystem built around the Jest framework to ensure the reliability of its intelligent marketplace features. The framework covers a broad spectrum of testing types, ranging from low-level utility unit tests to complex end-to-end (E2E) user scenarios. Because the project is a Chrome extension, a significant portion of the framework is dedicated to mocking browser-specific APIs (like chrome.*) and external dependencies like TensorFlow.js to provide a deterministic testing environment.
Sources: package.json:9-16, tests/setupTests.ts:4-39
Testing Architecture & Configuration
The testing infrastructure is orchestrated through npm scripts and configured via Jest settings. The project separates tests into logical directories to target specific layers of the application.
Test Execution Scripts
The following scripts define the entry points for various testing suites:
| Script | Command | Purpose |
|---|---|---|
test | jest | Runs the full test suite |
test:unit | jest tests/unit | Executes isolated unit tests for components and logic |
test:integration | jest tests/integration | Validates interaction between multiple modules |
test:ui | jest tests/ui | Tests React component rendering and user interactions |
test:e2e | jest tests/e2e | Validates full user workflows and browser manager logic |
test:coverage | jest --coverage | Generates code coverage reports |
Sources: package.json:9-16
Environment Setup
The testing environment is initialized through tests/setup.ts and tests/setupTests.ts. These files perform several critical actions:
- Global Mocks: Injects the
chromeobject into the global scope. - DOM Extension: Imports
@testing-library/jest-domto provide custom matchers for UI testing. - Observer Mocks: Provides stub implementations for
IntersectionObserverandResizeObserver, which are not natively available in thejsdomenvironment. - Custom Matchers: Extends Jest with specific matchers like
toHaveBeenCalledWithMatch.
Sources: tests/setup.ts:4-65, tests/setupTests.ts:2
Mocking Strategy
MarketSage relies heavily on jest.mock() to isolate code under test from external services and browser APIs.
Chrome API Mocks
The chrome object is mocked to simulate browser extension behavior. This includes runtime.sendMessage, tabs.query, and storage.local. The framework uses a ChromeMock interface to ensure type safety within tests.
The diagram shows how the global chrome object is populated with mocked sub-modules for testing.
Sources: tests/setup/environment.d.ts:20-53, tests/setup.ts:5-8
Third-Party & Service Mocks
Key services are mocked to return predictable data during tests:
- TensorFlow.js: Mocked to simulate AI model loading and prediction, returning static probability scores.
- Storage Utility: The
common/utils/storagemodule is fully mocked to prevent actual disk I/O, simulating state persistence for preferences and cart data. - Scam Detection Engines: Engines like
AnomalyDetectionEngineandImageAnalysisEngineare mocked to return pre-defined risk scores and issue lists.
Sources: tests/setupTests.ts:24-39, tests/unit/scamDetectionModel.test.ts:4-70
Testing Tiers
Unit Testing
Unit tests focus on isolated logic, such as the ErrorHandler and DataProcessing utilities. These tests verify that individual classes behave correctly in both success and failure states, including singleton management and metric tracking.
Sources: tests/unit/errors/ErrorHandler.test.ts:13-64, tests/unit/utils/dataProcessing.test.ts:4-71
End-to-End (E2E) Scenarios
E2E tests use a UnifiedBrowserManager to simulate complex user flows. For example, a scenario might involve searching for products across multiple platforms (AliExpress, Temu), verifying that scam warnings appear based on mocked risk assessments, and completing a checkout process.
This diagram illustrates the flow of an E2E scenario where the Browser Manager coordinates between mocked models and storage.
Sources: tests/e2e/userScenarios.test.ts:51-115
Build Verification
A specialized verify-build.js script acts as a pre-build "smoke test." It validates the integrity of the project structure before the production build process begins.
- package.json Validation: Checks for required dependencies (
react,rxjs,tfjs). - Config Checks: Ensures
tsconfig.jsonandmanifest.jsonare present and valid. - File Existence: Verifies critical source files like
StateManager.tsandErrorHandler.ts.
Sources: scripts/verify-build.js:28-100
Summary
The Testing Framework in MarketSage is a multi-layered system that prioritizes isolation and simulation. By using Jest as a core and implementing extensive mocks for the Chrome API and AI engines, the project ensures that marketplace features—such as scam detection and unified browsing—function correctly across various platforms and user configurations without requiring a live browser environment.