Protractor HTTP Mock provides a NodeJS module designed to facilitate mocking HTTP calls within Protractor end-to-end tests, specifically for AngularJS applications. Its core function is to allow developers to isolate UI and client-side application code by intercepting and responding to network requests with predefined data, thus removing dependencies on external APIs during test execution. A key differentiator is its independence from Angular Mocks (ngMockE2E), meaning it does not require any modifications to the AngularJS application under test. The current stable version, 0.10.0, was released in 2017. Due to its tight coupling with Protractor, which was officially deprecated in 2022, `protractor-http-mock` is effectively an abandoned package with no ongoing development or maintenance.
npm install protractor-http-mockVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates configuring `protractor-http-mock` in a Protractor configuration file, defining an inline HTTP mock, and loading a mock from an external file within a test spec. It also shows the essential `setup` and `teardown` calls in `beforeEach` and `afterEach` hooks.
Migrate end-to-end tests to a modern framework like Cypress, Playwright, or WebdriverIO, and utilize their native HTTP mocking capabilities. `protractor-http-mock` cannot be used independently.
Always call `mock([...])` at the very beginning of your test block or `beforeEach` hook, ensuring it executes before `browser.get()` or any action that triggers HTTP requests.
Ensure `mock.teardown()` is called reliably in an `afterEach` Jasmine or Mocha hook to clean up mocks and prevent leakage between tests.
Carefully verify the `rootDirectory` in `onPrepare` and `mocks.dir` within `protractor-http-mock.config.mocks` to ensure they accurately point to the base directory and the subdirectory containing your mock definition files.
For regex matching, define your mock request as: `{ request: { path: '/users/\d+', method: 'GET', regex: true }, response: { ... } }`.Ensure you have `const mock = require('protractor-http-mock');` at the top of your test file and call it as `mock.teardown();`.Call `mock([...])` within a `beforeEach` block, ensuring it runs before `browser.get()` or any actions that trigger the HTTP requests. Double-check your mock definition's `path`, `method`, `params`, and `queryString` to ensure it precisely matches the outgoing request. Consider adding `regex: true` if using regular expressions in paths.
Verify that `protractor-http-mock.config.rootDirectory` is set correctly (e.g., `__dirname` of your config file) and that `protractor-http-mock.config.mocks.dir` accurately points to the subdirectory containing your mock files. Ensure the file 'users.js' exists within that directory and exports the mock definition.
Inspect the actual HTTP request made by the browser (via browser developer tools) and compare it against your mock definition. Adjust the mock's `request` properties (path, method, params, queryString, headers, data) to exactly match the observed request. Remember to enable `regex: true` if using regular expressions in the `path`.