microsoft / microsoft/vscode-react-native
[Feature] Add unit tests for revertOpenModule command
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 2.7k
- Forks
- 295
- Avg merge
- 11h 17m
- Merged PRs (30d)
- 24
Description
Summary
The revertOpenModule.ts command implements functionality to revert extension modifications to the open/opn package module, but currently lacks unit test coverage.
Motivation
- Test coverage gap:
src/extension/commands/revertOpenModule.ts(127 lines) has no test file - Complex logic: File system operations, version checks, and pnpm support require testing
- Error-prone operations: File deletion and JSON modification need validation
- Maintenance: Tests would prevent regressions in complex conditional logic
Current State
- Source file:
src/extension/commands/revertOpenModule.ts(127 lines) - Test file: Does not exist
- Complexity: Package manager detection, version comparison, file operations
Command Functionality
The revertOpenModule command:
- Detects React Native version to determine package name ("open" vs "opn")
- Supports both npm and pnpm package managers
- Deletes
open-main.jsfile from node_modules - Modifies package.json to remove custom entry point
- Handles multiple error cases with localized messages
Proposed Changes
Create test/extension/commands/revertOpenModule.test.ts with coverage for:
-
Version detection:
- Test RN >= 0.60.0 uses "open" package
- Test RN < 0.60.0 uses "opn" package
- Test canary version handling
-
Package manager support:
- Test npm project path resolution
- Test pnpm project path resolution (.pnpm folder detection)
- Test open module discovery in pnpm structure
-
File operations:
- Mock fs operations (existsSync, unlinkSync, writeFileSync)
- Test open-main.js deletion
- Test package.json main entry deletion
- Test error handling for missing files
-
Error cases:
- Module not found
- File deletion failures
- package.json modification failures
Test Structure Example
describe('RevertOpenModule', () => {
let command: RevertOpenModule;
let mockProject: AppLauncher;
let fsMock: sinon.SinonStubbedInstance<typeof fs>;
describe('version detection', () => {
it('should use "open" for RN >= 0.60.0', async () => { /* ... */ });
it('should use "opn" for RN < 0.60.0', async () => { /* ... */ });
});
describe('pnpm support', () => {
it('should detect pnpm project', async () => { /* ... */ });
it('should resolve open module in .pnpm folder', async () => { /* ... */ });
});
describe('file operations', () => {
it('should delete open-main.js', async () => { /* ... */ });
it('should update package.json main entry', async () => { /* ... */ });
it('should handle missing files gracefully', async () => { /* ... */ });
});
});
Benefits
- Validate complex file system operations
- Ensure pnpm support works correctly
- Catch version detection regressions
- Improve error handling reliability
- Document expected behavior through tests
References
- Source:
src/extension/commands/revertOpenModule.ts - Related: SettingsHelper.getPackageManager(), ProjectVersionHelper
- Similar patterns: File operation tests in other command tests
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/extension/commands/revertOpenModule.ts and compare similar command tests, then inspect SettingsHelper.getPackageManager() and ProjectVersionHelper. Create test/extension/commands/revertOpenModule.test.ts covering React Native version detection, npm and pnpm resolution, file operations, and the listed error cases. Done means the command's conditional paths and failures are covered by passing unit tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react-native, typescript
- Domain
- devtools, testing
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100