Registry / testing / protractor-http-mock

protractor-http-mock

JSON →
library0.10.0jsnpmunverified

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-mock
INSTALL
IMPORT
SIG · PROTRACTOR-HTTP-MO
P
protractor-http-mock
testingjavascriptv0.10.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mock
const mock = require('protractor-http-mock');
import mock from 'protractor-http-mock';
The package primarily uses CommonJS `require()` syntax. The imported `mock` object is a function that also exposes `config`, `teardown`, `requestsMade`, and `clearRequests` as properties.
mock.config
require('protractor-http-mock').config = { rootDirectory: __dirname, protractorConfig: 'my-protractor-config.conf' };
mock.config = {...}; // if 'mock' was imported via ESM 'import'
Configuration for `protractor-http-mock` is set directly on the `config` property of the module's export, typically within the `onPrepare` function of the Protractor configuration file.
mock.teardown
mock.teardown();
teardown();
The `teardown` function is a method of the main `mock` object and must be called as such, usually in an `afterEach` hook.

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.

const mock = require('protractor-http-mock'); const path = require('path'); exports.config = { directConnect: true, capabilities: { 'browserName': 'chrome' }, framework: 'jasmine', specs: [path.resolve(__dirname, 'mock.spec.js')], onPrepare: function() { require('protractor-http-mock').config = { rootDirectory: __dirname, protractorConfig: 'protractor.conf.js', // Or the actual name of this config file mocks: { default: ['mock-login'], dir: 'mocks' } }; } }; // mock.spec.js (in the same directory, or specified in specs) describe('Protractor HTTP Mock Example', function() { beforeEach(function() { mock.setup(); // Ensure mocks are cleared and ready for each test }); afterEach(function() { mock.teardown(); }); it('should mock a GET request for user data', function() { mock([ { request: { path: '/api/users/1', method: 'GET' }, response: { data: { userName: 'Mocked User', email: 'mock@example.com' }, status: 200 } } ]); // Assume your app navigates to a page that makes this request browser.get('http://localhost:8000/app/#/users/1'); // Example: Verify UI reflects mocked data (replace with actual selectors) element(by.id('username-display')).getText().then(function(text) { expect(text).toEqual('Mocked User'); }); element(by.id('email-display')).getText().then(function(text) { expect(text).toEqual('mock@example.com'); }); }); it('should load mocks from a file', function() { // Create a file: mocks/products.js // module.exports = [{ request: { path: '/api/products', method: 'GET' }, response: { data: [{id: 1, name: 'Mock Product'}] } }]; mock(['products']); // Loads from mocks/products.js relative to rootDirectory browser.get('http://localhost:8000/app/#/products'); // Assert that products are loaded from mock }); });
Debug
Known issues
breakingThe Protractor E2E test framework, on which `protractor-http-mock` is entirely dependent, has been officially deprecated since August 2022 and reached end-of-life on August 31, 2023. This renders `protractor-http-mock` effectively obsolete and unmaintained.
fix
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.
affects: >=0.10.0
gotchaMocks must be configured and called using the `mock()` function *before* the browser navigates or reloads, as `protractor-http-mock` intercepts requests at the network level. If `mock()` is called after `browser.get()` or a page reload, the requests might not be intercepted.
fix
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.
affects: >=0.1.0
gotchaFailing to call `mock.teardown()` after each test can lead to mocks persisting across tests, causing unexpected behavior, side effects, and unreliable test results.
fix
Ensure `mock.teardown()` is called reliably in an `afterEach` Jasmine or Mocha hook to clean up mocks and prevent leakage between tests.
affects: >=0.1.0
gotchaWhen using mock files (e.g., `mock(['my-file'])`), incorrect `rootDirectory` or `mocks.dir` configuration in `protractor-http-mock`'s global config will prevent the system from locating the mock files, leading to requests not being intercepted.
fix
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.
affects: >=0.1.0
gotchaBy default, the `path` property in a mock request object uses exact string matching. To use regular expressions for flexible path matching, you must explicitly set the `regex` property to `true` in the request definition.
fix
For regex matching, define your mock request as: `{ request: { path: '/users/\d+', method: 'GET', regex: true }, response: { ... } }`.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: mock.teardown is not a function
The `protractor-http-mock` module was not correctly imported or its `teardown` method was called out of context.
fix
Ensure you have `const mock = require('protractor-http-mock');` at the top of your test file and call it as `mock.teardown();`.
HTTP requests are not being mocked (application still makes real network calls).
The `mock()` function was called too late, after the browser had already initiated the HTTP requests, or the mock definition itself is incorrect (e.g., wrong path, method, or missing `regex: true` for regex paths).
fix
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.
Error: Cannot find module 'my-mocks/users'
The `protractor-http-mock` configuration for mock file directories (`rootDirectory` or `mocks.dir`) is incorrect, or the specified mock file name does not exist or has a different path.
fix
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.
Path '/api/data' with method 'GET' was not mocked
The incoming HTTP request did not match any of the currently active mock definitions. This could be due to a mismatch in `path`, `method`, `params`, `queryString`, or `headers`.
fix
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`.
Upgrade
Version history
0.10.0latest on npm
Audit
Dependencies
protractorrequiredProvides core e2e testing framework functionality that `protractor-http-mock` integrates with to intercept HTTP requests. The library is functionally dependent on Protractor.
Agent activity
13 hits · last 30 days
node
12
Resources