Context
When writing Flutter tests that involve code using wakelock_plus, tests fail with a PlatformException because the native channel is unavailable in the test environment:
PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.wakelock_plus_platform_interface.WakelockPlusApi.toggle"., null, null)
Current workaround
To mock the platform in tests, I currently do the following:
import 'package:mocktail/mocktail.dart';
import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interface.dart';
class MockWakelockPlusPlatform extends Mock
implements WakelockPlusPlatformInterface {}
final mockWakelock = MockWakelockPlusPlatform();
wakelockPlusPlatformInstance = mockWakelock;
when(
() => mockWakelock.toggle(enable: any(named: 'enable')),
).thenAnswer((_) async {});
This requires adding wakelock_plus_platform_interface as a dev_dependency directly, since it's not exported by the main wakelock_plus package.
Note: I may be wrong about the intended mocking approach. If there's an official or recommended way to mock wakelock_plus in tests, I'd be happy to hear it and update my code accordingly. This issue is based on my own exploration.
Problem
Having to depend on wakelock_plus_platform_interface directly as a dev_dependency creates a potential version drift risk: if the platform interface package is updated independently, consumers would need to manually keep both versions in sync, which is error-prone and adds maintenance overhead.
Suggested fix
Re-export WakelockPlusPlatformInterface (or at least the types needed for mocking) from the main wakelock_plus package, so that users only need to depend on wakelock_plus itself, even for testing purposes.
Context
When writing Flutter tests that involve code using wakelock_plus, tests fail with a PlatformException because the native channel is unavailable in the test environment:
Current workaround
To mock the platform in tests, I currently do the following:
This requires adding wakelock_plus_platform_interface as a dev_dependency directly, since it's not exported by the main wakelock_plus package.
Problem
Having to depend on wakelock_plus_platform_interface directly as a dev_dependency creates a potential version drift risk: if the platform interface package is updated independently, consumers would need to manually keep both versions in sync, which is error-prone and adds maintenance overhead.
Suggested fix
Re-export WakelockPlusPlatformInterface (or at least the types needed for mocking) from the main wakelock_plus package, so that users only need to depend on wakelock_plus itself, even for testing purposes.