{"id":16507,"library":"protractor-http-mock","title":"Protractor HTTP Mocking","description":"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.","status":"abandoned","version":"0.10.0","language":"javascript","source_language":"en","source_url":"https://github.com/atecarlos/protractor-http-mock","tags":["javascript"],"install":[{"cmd":"npm install protractor-http-mock","lang":"bash","label":"npm"},{"cmd":"yarn add protractor-http-mock","lang":"bash","label":"yarn"},{"cmd":"pnpm add protractor-http-mock","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides core e2e testing framework functionality that `protractor-http-mock` integrates with to intercept HTTP requests. The library is functionally dependent on Protractor.","package":"protractor","optional":false}],"imports":[{"note":"The package primarily uses CommonJS `require()` syntax. The imported `mock` object is a function that also exposes `config`, `teardown`, `requestsMade`, and `clearRequests` as properties.","wrong":"import mock from 'protractor-http-mock';","symbol":"mock","correct":"const mock = require('protractor-http-mock');"},{"note":"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.","wrong":"mock.config = {...}; // if 'mock' was imported via ESM 'import'","symbol":"mock.config","correct":"require('protractor-http-mock').config = { rootDirectory: __dirname, protractorConfig: 'my-protractor-config.conf' };"},{"note":"The `teardown` function is a method of the main `mock` object and must be called as such, usually in an `afterEach` hook.","wrong":"teardown();","symbol":"mock.teardown","correct":"mock.teardown();"}],"quickstart":{"code":"const mock = require('protractor-http-mock');\nconst path = require('path');\n\nexports.config = {\n  directConnect: true,\n  capabilities: {\n    'browserName': 'chrome'\n  },\n  framework: 'jasmine',\n  specs: [path.resolve(__dirname, 'mock.spec.js')],\n  onPrepare: function() {\n    require('protractor-http-mock').config = {\n      rootDirectory: __dirname,\n      protractorConfig: 'protractor.conf.js', // Or the actual name of this config file\n      mocks: {\n        default: ['mock-login'],\n        dir: 'mocks'\n      }\n    };\n  }\n};\n\n// mock.spec.js (in the same directory, or specified in specs)\n\ndescribe('Protractor HTTP Mock Example', function() {\n  beforeEach(function() {\n    mock.setup(); // Ensure mocks are cleared and ready for each test\n  });\n\n  afterEach(function() {\n    mock.teardown();\n  });\n\n  it('should mock a GET request for user data', function() {\n    mock([\n      {\n        request: {\n          path: '/api/users/1',\n          method: 'GET'\n        },\n        response: {\n          data: {\n            userName: 'Mocked User',\n            email: 'mock@example.com'\n          },\n          status: 200\n        }\n      }\n    ]);\n\n    // Assume your app navigates to a page that makes this request\n    browser.get('http://localhost:8000/app/#/users/1'); \n\n    // Example: Verify UI reflects mocked data (replace with actual selectors)\n    element(by.id('username-display')).getText().then(function(text) {\n      expect(text).toEqual('Mocked User');\n    });\n    element(by.id('email-display')).getText().then(function(text) {\n      expect(text).toEqual('mock@example.com');\n    });\n  });\n\n  it('should load mocks from a file', function() {\n    // Create a file: mocks/products.js\n    // module.exports = [{ request: { path: '/api/products', method: 'GET' }, response: { data: [{id: 1, name: 'Mock Product'}] } }];\n    mock(['products']); // Loads from mocks/products.js relative to rootDirectory\n    browser.get('http://localhost:8000/app/#/products');\n    // Assert that products are loaded from mock\n  });\n});\n","lang":"javascript","description":"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."},"warnings":[{"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.","message":"The 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.","severity":"breaking","affected_versions":">=0.10.0"},{"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.","message":"Mocks 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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure `mock.teardown()` is called reliably in an `afterEach` Jasmine or Mocha hook to clean up mocks and prevent leakage between tests.","message":"Failing to call `mock.teardown()` after each test can lead to mocks persisting across tests, causing unexpected behavior, side effects, and unreliable test results.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"For regex matching, define your mock request as: `{ request: { path: '/users/\\d+', method: 'GET', regex: true }, response: { ... } }`.","message":"By 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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure you have `const mock = require('protractor-http-mock');` at the top of your test file and call it as `mock.teardown();`.","cause":"The `protractor-http-mock` module was not correctly imported or its `teardown` method was called out of context.","error":"TypeError: mock.teardown is not a function"},{"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.","cause":"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).","error":"HTTP requests are not being mocked (application still makes real network calls)."},{"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.","cause":"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.","error":"Error: Cannot find module 'my-mocks/users'"},{"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`.","cause":"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`.","error":"Path '/api/data' with method 'GET' was not mocked"}],"ecosystem":"npm"}