har-to-mocks
v2.3.1
Published
Extract response from .har file and create JSON mocks for mock server.
Downloads
173
Maintainers
Readme
har-to-mocks
Extract response from .har file and create JSON mocks for mock server.
Install CLI
npm install -g har-to-mocksor by npx
npx har-to-mocks [path to .har] [path mock/api folder] --dry-runHow does it work?
Inspect and filter requests in .har files
File can contain hundreds of requests so it's important to be able filter data. For filtering you can use flags:
- (
--url) for filtering by match in the url. Search is case sensitive and matches against the full URL including query parameters (e.g.,--url=?q=searchor--url=/api/search?id=123) - (
-m,--method=GET --method=POST) for filter specific method. Supported: 'GET', 'POST', 'PUT', 'DELETE' and 'PATCH' methods. Default value is 'GET'. - (
-t,--type=xhr) for filtering request type. Default value is 'xhr'
Video example: YouTube [email protected].
example:
$ har-to-mocks ./file.har --url=api/service --method=GETwill display:
Filtered requests:
Name │ Method │ Path │ Query
──────────────┼────────┼─────────────────────────────┼──────
userRoles │ GET │ /api/service/userRoles │
currentUserId │ GET │ /api/service/currentUserId │
active │ GET │ /api/service/clients/active │The Query column displays URL query parameters for easy identification of similar requests.
If output folder is not specified, mocks will not be written and no Status column is shown.
Interactive Mode
Use the --interactive (or -i) flag to manually select which endpoints to write:
$ har-to-mocks ./file.har ./mocks --interactiveIn interactive mode:
- A checkbox list appears with all endpoints
- Response Preview: As you navigate with arrow keys, the JSON response of the focused endpoint is displayed below the list
- Endpoints that would be written by default are pre-selected
- After selection, a folder tree preview is shown
- You're asked to confirm before files are written
Keyboard shortcuts:
↑/↓- Navigate through endpoints (updates response preview)Space- Toggle selectiona- Toggle alli- Invert selectionEnter- Confirm selection
The response preview helps you distinguish between multiple responses from the same endpoint with different data - useful when duplicate routes return different responses and you need to choose the right one.
Extract data from .har to mock/api folder
The second argument should be path to mock's folder. Export structure is prepared for mocks-to-msw which helps with integration with MSW (Mock Service Worker) and connect-api-mocker.
WARNING: When second argument is defined cli will write files. To avoid unwanted overwrite use --dry-run flag to skip writing part of process.
When a target folder is specified, the Status column shows what will happen to each file:
- create - File doesn't exist, will be created
- update - File already exists, will be overwritten
- skip - Duplicate endpoint, will be skipped (a later entry writes to the same file)
example:
$ har-to-mocks ./file.har ./mocks --url=api/service --method=GET --dry-runwill display:
Filtered requests:
Name │ Method │ Path │ Query │ Status
──────────────┼────────┼─────────────────────────────┼───────┼───────
userRoles │ GET │ /api/service/userRoles │ │ create
currentUserId │ GET │ /api/service/currentUserId │ │ create
active │ GET │ /api/service/clients/active │ │ create
Folder tree which will be applied:
└─ mocks
└─ api
└─ service
├─ userRoles
│ └─ GET.json
├─ currentUserId
│ └─ GET.json
└─ clients
└─ active
└─ GET.json
No files were written. If you want to write files remove the (--dry-run) flag.If files already exist, the tree shows which will be updated:
Folder tree which will be applied:
└─ mocks
└─ api
└─ service
├─ userRoles
│ └─ GET.json [UPDATE]
├─ currentUserId
│ └─ GET.json
└─ clients
└─ active
└─ GET.json [UPDATE]When multiple requests share the same path and method (but have different query parameters), they would write to the same file. A helpful hint is displayed:
Note: Some endpoints have status "skip" because they share the same path and method.
The last occurrence will be written. To select specific endpoints, use interactive mode:
har-to-mocks <file.har> <output-folder> --interactiveDevelopment
Prerequisites
- Node.js >= 18.0.0
- npm >= 7.0.0
Tech Stack
This project uses modern JavaScript tooling:
- Framework: @oclif/core v4 - Modern CLI framework
- Module System: ES Modules (ESM)
- Testing: Vitest with snapshot testing
- TypeScript: NodeNext module resolution for ESM compatibility
- Linting: ESLint with TypeScript support
Getting Started
Clone the repository
Install dependencies:
npm installBuild the project:
npm run buildRun the CLI locally:
./bin/dev [args]
Available Scripts
npm run build- Compile TypeScript to JavaScriptnpm run lint- Run ESLintnpm run lint:fix- Fix ESLint issues automaticallynpm test- Run tests in watch modenpm run test:ci- Run tests once (for CI)npm run test:ui- Open Vitest UI for interactive testingnpm run coverage- Generate test coverage report
Testing
The project maintains 100% test coverage with:
- Unit tests using Vitest with snapshot assertions
- Integration tests that verify CLI behavior end-to-end
- Snapshot testing to catch unintended output changes
Run tests:
npm testGenerate coverage report:
npm run coverage