Running Tests
Relevant source files
The following files were used as context for generating this wiki page:
The consent-manager project uses the Vitest testing framework to ensure the reliability and correctness of its core logic. Tests are written in TypeScript and executed in a simulated browser environment provided by JSDOM. This setup allows for fast and accurate unit testing of modules that interact with browser features like cookies and the DOM, without needing a full browser.
The primary focus of the existing tests is the ConsentManager class, which handles the core state management of user consent. The tests cover its initialization, state changes based on user actions, and event-driven architecture.
Test Execution
Tests are executed via an npm script defined in package.json.
Sources: package.json:45
Test Command
| Script | Command | Description |
|---|---|---|
test | vitest run | Executes all test suites once and exits. |
Sources: package.json:45
The test execution flow is initiated by the npm run test command, which invokes the Vitest test runner.
This diagram shows the process of running tests, from the initial command to the final report.
Sources: package.json:45, vitest.config.js
Test Configuration
The testing environment is configured in vitest.config.js and influenced by the project's TypeScript configuration.
Vitest Configuration
The project configures Vitest to use jsdom as the test environment. This simulates a browser environment, making browser-specific APIs like document and window available within the tests. This is crucial for testing a library intended for front-end use.
// vitest.config.js
export default defineConfig({
test: {
environment: 'jsdom'
},
})
Sources: vitest.config.js:4-8
Dependencies
The testing setup relies on several development dependencies defined in package.json.
| Package | Version | Role |
|---|---|---|
vitest | ^0.33.0 | The core test framework. |
jsdom | ^22.1.0 | Provides the simulated DOM environment for running tests in Node.js. |
Sources: package.json:52-53
TypeScript Configuration
The tests are written in TypeScript and compiled according to the settings in tsconfig.json. The configuration specifies es2017 as the target and includes the dom library, which provides type definitions for DOM APIs used in the tests.
Sources: tsconfig.json:3, tsconfig.json:40
Test Implementation (ConsentManager.test.ts)
The test file src/modules/ConsentManager.test.ts provides a clear example of the project's testing strategy. It focuses on unit testing the ConsentManager class in isolation.
Sources: src/modules/ConsentManager.test.ts
Test Structure
Tests are organized using describe blocks to group related tests for a specific module. A beforeEach hook is used to ensure a clean state before each test run by clearing any cookies set in previous tests.
// src/modules/ConsentManager.test.ts
import { expect, describe, beforeEach, vi, test } from 'vitest';
import Cookie from 'js-cookie';
import ConsentManager from './ConsentManager';
describe('ConsentManager', () => {
beforeEach(() => {
Cookie.remove('consent-manager');
});
// ... test cases
});
Sources: src/modules/ConsentManager.test.ts:1-11
Test Scenarios
The test suite covers several key functionalities of the ConsentManager class:
| Test Case Description | Tested Behavior |
|---|---|
constructor - default config | Verifies that the ConsentManager initializes with correct default values when no cookie is present. |
constructor - valid cookie | Checks if the ConsentManager correctly parses an existing, valid consent cookie and sets its state. |
setGrant - required category | Ensures that a required consent category cannot be disabled. |
setGrant - optional category | Confirms that an optional category's grant status can be updated, and isCustomized is set to true. |
setGrant - invalid category | Asserts that attempting to set a grant for a non-existent category has no effect. |
event listener handling | Validates that on and off methods correctly register and de-register event listeners for state updates. |
Sources: src/modules/ConsentManager.test.ts:13-111
Event Listener Test Flow
The event listener handling test demonstrates how the event system is verified. It uses a mock function (vi.fn()) to track calls.
The following diagram illustrates the sequence of actions in this test:
This diagram shows the sequence for testing the event listener functionality, including registration, triggering, and removal of a listener.