TypeScript Test Suite
Relevant source files
The following files were used as context for generating this wiki page:
The TypeScript test suite for the tauri-plugin-serialplugin provides a comprehensive framework for verifying the functionality of the serial port interface within a Tauri environment. It is designed to validate core plugin operations, ensure robust error handling, and verify complex behaviors like auto-reconnection and concurrent operations. By leveraging Jest and custom mocking utilities, the suite isolates the frontend logic from the actual hardware, allowing for reliable and repeatable automated testing of the serial communication lifecycle.
Sources: tests/README.md:1-5, package.json:41-45
Architecture and Configuration
The test suite is built on Jest and utilizes ts-jest for TypeScript support. The environment is configured via jest.config.js and initialized through a dedicated setup file that mocks critical Tauri global objects and modules.
Mocking Strategy
Because the plugin relies on the Tauri IPC bridge, the test suite mocks the @tauri-apps/api modules. This ensures that calls to the Rust backend are intercepted and can be controlled to return success or error states.
The diagram shows how the setup process prepares the environment by replacing real Tauri API modules with Jest mock functions.
Key modules mocked include:
@tauri-apps/api/core: Specifically theinvokefunction used for command execution.@tauri-apps/api/event: Thelistenandoncefunctions used for serial data events.window.__TAURI__: Global internal bridge for compatibility.TextEncoder/TextDecoder: Standard globals mocked to support encoding tests.
Sources: tests/setup.ts:1-40, tests/test-utils.ts:1-25, package.json:41-55
Test Suite Structure
The suite is organized into logical modules, each targeting a specific area of the SerialPort class functionality.
| Test File | Description |
|---|---|
port-operations.test.ts | Validates basic lifecycle: open, close, read, write. |
error-handling.test.ts | Verifies behavior during hardware failures, timeouts, and permission issues. |
static-methods.test.ts | Tests global operations like available_ports and closeAll. |
listeners.test.ts | Ensures data events and disconnect events are properly emitted and canceled. |
auto-reconnect.test.ts | Validates the automatic reconnection logic upon physical disconnection. |
Sources: tests/README.md:7-60
Implementation Details
Setup Utilities
The test-utils.ts file provides helper functions to reduce boilerplate. The setupTestMocks() function initializes the common mocks and returns them for use within expect assertions.
export const setupTestMocks = () => {
const mockInvoke = invoke as MockInvoke;
const mockListen = listen as MockListen;
// ... configuration of mocks
return { mockInvoke, mockListen, mockOnce, mockUnlisten };
};
Sources: tests/test-utils.ts:10-35
Core Operation Testing
Tests for core operations verify that the frontend correctly translates high-level method calls into specific Tauri IPC commands. For instance, the open() method must call plugin:serialplugin|open with the correct port configuration.
The sequence illustrates the flow of a successful port opening operation during a test.
Sources: tests/port-operations.test.ts:18-35, tests/README.md:100-110
Error Handling and Resiliency
The suite includes rigorous tests for failure scenarios to ensure the plugin remains stable under adverse conditions.
Error Scenarios Tested
- Resource Constraints: "Device busy" or "Permission denied" during port opening.
- Hardware Failures: "Port not found" or "Operation timed out" during active reads/writes.
- State Integrity: Verifying that operations fail gracefully when the port is already closed or hasn't been opened yet.
it('should handle recovery after errors', async () => {
// 1. Attempt write on closed port (Should fail)
await expect(serialPort.write('test')).rejects.toThrow();
// 2. Open port (Should succeed)
await serialPort.open();
// 3. Write data (Should succeed)
await expect(serialPort.write('test')).resolves.toBe(5);
});
Sources: tests/error-handling.test.ts:10-60, tests/error-handling.test.ts:115-140
Execution Commands
The test suite can be executed via npm or pnpm scripts defined in the project root.
| Command | Action |
|---|---|
npm test | Runs the full test suite once. |
npm run test:watch | Runs tests in watch mode for active development. |
npm test -- tests/port-operations.test.ts | Runs a specific test file. |
npm test -- --coverage | Generates a code coverage report. |
Sources: package.json:26-27, tests/README.md:64-75
Conclusion
The TypeScript Test Suite is a critical component of the tauri-plugin-serialplugin architecture, providing high confidence in the reliability of serial communications. By simulating the Tauri bridge and various hardware states, it allows developers to verify complex state management, such as auto-reconnection and error recovery, without physical serial devices.