{"id":19964254,"url":"https://github.com/mdasberg/ng-apimock","last_synced_at":"2025-04-09T10:07:33.096Z","repository":{"id":9340760,"uuid":"61722766","full_name":"mdasberg/ng-apimock","owner":"mdasberg","description":"Node plugin that provides the ability to use scenario based api mocking:  for local development for protractor testing","archived":false,"fork":false,"pushed_at":"2022-12-06T14:36:12.000Z","size":2327,"stargazers_count":97,"open_issues_count":44,"forks_count":24,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-02T07:09:11.638Z","etag":null,"topics":["angular2","angularjs","mock","mock-data","mocking-interface","ng-apimock","protractor","protractor-cucumber","protractor-tests","scenario"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mdasberg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-22T13:49:38.000Z","updated_at":"2025-01-24T05:46:13.000Z","dependencies_parsed_at":"2023-01-11T20:11:58.796Z","dependency_job_id":null,"html_url":"https://github.com/mdasberg/ng-apimock","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdasberg%2Fng-apimock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdasberg%2Fng-apimock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdasberg%2Fng-apimock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdasberg%2Fng-apimock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mdasberg","download_url":"https://codeload.github.com/mdasberg/ng-apimock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248018060,"owners_count":21034048,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular2","angularjs","mock","mock-data","mocking-interface","ng-apimock","protractor","protractor-cucumber","protractor-tests","scenario"],"created_at":"2024-11-13T02:21:39.174Z","updated_at":"2025-04-09T10:07:33.056Z","avatar_url":"https://github.com/mdasberg.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ng-apimock [![Build Status](https://travis-ci.org/mdasberg/ng-apimock.svg?branch=master)](https://travis-ci.org/mdasberg/ng-apimock) [![npm version](https://img.shields.io/node/v/ng-apimock.svg)](https://github.com/mdasberg/ng-apimock) [![dependency Status](https://img.shields.io/david/mdasberg/ng-apimock.svg)](https://david-dm.org/mdasberg/ng-apimock) [![devDependency Status](https://img.shields.io/david/dev/mdasberg/ng-apimock.svg)](https://david-dm.org/mdasberg/gng-apimock#info=devDependencies) [![npm downloads](https://img.shields.io/npm/dm/ng-apimock.svg?style=flat-square)](https://www.npmjs.com/package/ng-apimock)\n\n\u003e Node plugin that provides the ability to use scenario based api mocking:\n - for local development\n - for protractor testing\n\n#### Plugins that use ng-apimock\n - [grunt-ng-apimock](https://mdasberg.github.io/grunt-ng-apimock) is a plugin that makes ng-apimock available for [Grunt](http://gruntjs.com/)\n - [gulp-ng-apimock](https://mdasberg.github.io/gulp-ng-apimock) is a plugin that makes ng-apimock available for [Gulp](http://gulpjs.com/)\n \n  \n# MIGRATION to the new modular version\nA new version of Ng-apimock has been released. This version has been refactored into [multiple modules](https://github.com/ng-apimock). You can find the migration guide [here](https://github.com/ng-apimock/core/blob/master/MIGRATION.md). \n\nThe functionality has been split up into the following modules:\n- [@ng-apimock/core](https://github.com/ng-apimock/core) - the api\n- [@ng-apimock/protractor-plugin](https://github.com/ng-apimock/protractor-plugin) - the protractor plugin \n- [@ng-apimock/webdriverio-plugin](https://github.com/ng-apimock/webdriverio-plugin) - the webdriverio plugin \n- [@ng-apimock/dev-interface](https://github.com/ng-apimock/dev-interface) - the development interface for local development\n- [@ng-apimock/cypress-plugin](https://github.com/ng-apimock/cypress-plugin) - the cypress plugin\n \n\n## Getting Started\n\n```shell\nnpm install ng-apimock --save-dev\n\n```\n\nOnce the plugin has been installed, you can require it with this line of JavaScript:\n\n```js\nvar ngApimock = require('ng-apimock')();\n\n```\n\n## The \"ngApimock\" process mocks\n\n### Overview\nIn order to use the available mocks, you need to call the run function with this line of JavaScript:\n\n```js\nngApimock.run({\n  \"baseUrl\": \"http://\u003cNODE_SERVER_API_URL\u003e:\u003cPORT\u003e\", // If not informed browser.baseUrl will be used\n  \"src\": \"test/mocks\",\n  \"outputDir\": \"path/to/outputDir\",\n  \"done\": function() {\n  // async\n  }\n});\n\n```\n\nThe run function will process the mock data provided in the configuration and make it accessible for connect as middleware.\n\nIn order to watch for changes, you need to call the watch function with this line of Javascript:\n\n```js\nngApimock.watch(\"test/mocks\");\n\n```\n\nThe watch function will watch for changes in the mock directory and update accordingly.\n\n### Howto write mocks\nThere are a couple of rules to follow.\n\n1. For each api call create a separate file\n2. Each file needs to follow the format below.\n\n```js\n{\n  \"expression\": \"your expression here (ie a regex without the leading and trailing '/' or a string)\",\n  \"method\": \"the http method (ie GET, POST, PUT or DELETE)\", // supports JSONP as well\n  \"body\": \"request body matcher (ie a regex without the leading and trailing '/' or a string)\"  // optional\n  \"name\": \"identifiable name for this service call\"  // if non is provided, expression$$method will be used\n  \"isArray\": \"indicates if the response data is an array or object\",\n  \"responses\": {\n    \"some-meaningful-scenario-name\": {\n      \"default\": true, // if false or not provided this response will not be used as default\n      \"status\": 200, // optional - defaults to 200\n      \"headers\": {}, // optional - defaults to {}\n      \"data\": {}, // optional\n      \"file\": \"path/to/file.ext\" // optional, when provided don't forget the matching content-type header as it will result in a file download instead of data\n      \"statusText\": \"\", // optional\n      \"delay\": 2000 // optional - defaults to no delay when provided this delay will only be used for this response\n    },\n    \"some-other-meaningful-scenario-name\": {\n      \"data\": {}\n    }\n  }\n}\n\n```\n\n## Howto use global variables\nIf for instance, you have date sensitive information in you mocks, mock data is not flexible enough.\nYou can use global variables for this. By surrounding a value in the response.data with %%theVariableName%%,\nyou can make your data more flexible, like this:\n\n```json\n\"responses\": {\n    \"some-meaningful-scenario-name\": {\n        \"data\": {\n            \"today\": \"%%today%%\"\n        }\n    }\n}\n```\n\nFor local development you can use the web interface to add, change or delete variables.\nFor protractor you can use the following commands\n```js\n     ngApimock.setGlobalVariable(name, value); // to add or update\n     ngApimock.deleteGlobalVariable(name); // to delete\n```\n\n### Howto serve selected mocks\nTo be able to use the selected mocks you need to do two things:\n\n1. Add the connect middleware\n2. Add the mocking interface to your connect configuration\n\n#### Add the connect middleware\nWhen running connect you can do add the following middleware block to your configuration\n\n\n```js\nvar app = connect();\napp.use(require('ng-apimock/lib/utils').ngApimockRequest);\napp.use(function middleware2(req, res, next) {\n  // middleware 2\n  next();\n});\n```\n\n#### Add the mocking interface to your connect configuration\nWhen running grunt-contrib-connect you can do add the following staticServe block to your configuration\n\n```js\nvar app = connect();\napp.use('/mocking', require('serve-static')('path/to/the/generated/mocking/index.html'));\napp.use(function middleware2(req, res, next) {\n  // middleware 2\n  next();\n});\n```\n\n### Howto use for local development\n\nAs you have configured both the [connect middleware](#add-the-connect-middleware) and the [mocking interface](#add-the-mocking-interface-to-your-connect-configuration), everything\n  should work out of the box. By default all the responses configured as default, will be returned if the expression matches.\n\n  If you would like to change the selected scenario, you can go to http://localhost:9000/mocking and use the interface to change the selected scenario or variables\n\nThe interface looks like this:\n\n![alt tag](https://raw.githubusercontent.com/mdasberg/ng-apimock/master/img/web-interface-ng-apimock.png)\n\n\n\n### Howto use for your protractor tests.\nAs you are building an [AngularJS](https://angularjs.org/) application you will probably use [Protractor](https://angular.github.io/protractor/#/) for testing your UI.\n\nIn order to use ngApimock in your protractor tests, require it in your protractor configuration like this:\n```js\nexports.config = {\n    onPrepare: function () {\n        global.ngApimock = require('.tmp/mocking/protractor.mock.js');\n    }\n};\n```\nand from that point on you can use it in your tests\n```js\ndescribe('Some test', function () {\n    it('should do something', function() {\n        ngApimock.selectScenario('name of some api', 'another'); // at runtime you can change a scenario\n    });\n });\n\n```\n\nBy default all the scenario's marked as default will be returned if the expression matches. So you only need to add ngApimock.selectScenario in case your test needs\nanother scenario response to be returned.\n\nNgApimock also works when running multiple tests concurrent, by using the protract session id of the test.\nThis ensures that changing a scenario in one test, will not effect another test.\n\n### Using Angular 2 or higher with Protractor?\nIf you are using Angular 2 or higher in combination with Protractor you will need to add the following to you configuration.\n\n**Protractor 4**\n```js\nexports.config = {\n    useAllAngular2AppRoots: true\n};\n```\n**Protractor 5 or higher**\n```js\nexports.config = {\n    ngApimockOpts: {\n        angularVersion: 2,  // {number} provide major version of Angular\n        hybrid: false // optional boolean which can be used for testing Angular apps within an AngularJs app.\n    }\n};\n```\n\n### Available functions\nAll these functions are protractor promises, so they can be chained.\n\n#### selectScenario(name, scenarioName, options)\nSelects the given scenario (when calling this function without a scenario or with 'passThrough' as scenario name, the call will be passed through to the actual backend)\n\n#### delayResponse(name, delay)\nSets the delay time in milliseconds for the mock so the response will be delayed. The delay set here superseeds the delay defined in the response mock.\n\n#### echoRequest(name, indicator)\nSets the indicator which enables / disables the request logging (only post request should be logged)\n\n#### setAllScenariosToDefault()\nResets all mocks to the default scenarios\n\n#### setAllScenariosToPassThrough\nResets all mocks to use passthroughs\n\n#### setGlobalVariable(key, value)\nAdds or updates the global key/value pair\n\n#### setGlobalVariables(variables)\nAdds or updates the global key/value pairs  ie. {'some':'value', 'another': 'value'}\n\n#### deleteGlobalVariable(key)\nRemove the global variable matching the key\n\n### Howto use recording functionality\nYou can record API calls in NgApimock. This is usefull if you have a live API, and want to create mocks for them.\nYou turn on Recording in the header Record (checkbox), and start calling the API. Requests are recorded for each mock. You can zoom in up to Request Response information.\nThe response data can be used in mock files, described earlier.\n\n## Contributing\nIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code committing.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdasberg%2Fng-apimock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmdasberg%2Fng-apimock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdasberg%2Fng-apimock/lists"}