{"id":18572401,"url":"https://github.com/a/superagent-mocker","last_synced_at":"2025-04-10T07:31:19.213Z","repository":{"id":31913430,"uuid":"35482617","full_name":"A/superagent-mocker","owner":"A","description":"Pretty simple in-browser mocks for CRUD and REST API ","archived":false,"fork":false,"pushed_at":"2018-06-23T20:10:10.000Z","size":63,"stargazers_count":131,"open_issues_count":21,"forks_count":32,"subscribers_count":28,"default_branch":"master","last_synced_at":"2025-04-01T18:51:30.262Z","etag":null,"topics":["mock","superagent","testing-tools"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/A.png","metadata":{"files":{"readme":"readme.md","changelog":"history.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-12T10:49:09.000Z","updated_at":"2025-03-02T02:48:39.000Z","dependencies_parsed_at":"2022-08-17T19:30:23.913Z","dependency_job_id":null,"html_url":"https://github.com/A/superagent-mocker","commit_stats":null,"previous_names":["shuvalov-anton/superagent-mocker"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/A%2Fsuperagent-mocker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/A%2Fsuperagent-mocker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/A%2Fsuperagent-mocker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/A%2Fsuperagent-mocker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/A","download_url":"https://codeload.github.com/A/superagent-mocker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248176308,"owners_count":21060044,"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":["mock","superagent","testing-tools"],"created_at":"2024-11-06T23:06:05.691Z","updated_at":"2025-04-10T07:31:18.996Z","avatar_url":"https://github.com/A.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# superagent-mocker\n\n[![Build Status](https://travis-ci.org/A/superagent-mocker.svg?branch=master)](https://travis-ci.org/A/superagent-mocker)\n[![Coverage Status](https://coveralls.io/repos/github/A/superagent-mocker/badge.svg?branch=master)](https://coveralls.io/github/A/superagent-mocker?branch=master)\n[![npm version](https://img.shields.io/npm/v/superagent-mocker.svg)](https://www.npmjs.com/package/superagent-mocker)\n[![npm downloads](https://img.shields.io/npm/dm/superagent-mocker.svg)](https://www.npmjs.com/package/superagent-mocker)\n\nREST API mocker for the browsers. LOOK MA NO BACKEND! 👐\n\nWritten for [superagent](https://github.com/visionmedia/superagent).\n\n## Install\n\n```shell\nnpm i superagent-mocker\n```\n\n## Usage\n\n### Setup\n\n```js\nvar request = require('superagent');\nvar mock = require('superagent-mocker')(request);\n```\n\n### Timeout\n\nYou can provide custom timeout, that can be a function or a number. Just set\n`timeout` property to the `mock`:\n\n```js\nvar mock = require('superagent-mocker');\n\n// set just number\nmock.timeout = 100;\n\n// Or function to get random\nmock.timeout = function () {\n  return Math.random() * 1e4 |0;\n}\n```\n\n### Get\n\nYou may set headers using the `mock.set()`.  To ensure header keys are not case sensitive,\nall keys will be transformed to lower case (see example).\n\n```js\nmock.get('/topics/:id', function(req) {\n  return {\n    id: req.params.id,\n    content: 'Hello World!',\n    headers: req.headers\n  };\n});\n\nrequest\n  .get('/topics/1')\n  .set({ 'X-Custom-Header': 'value of header' })\n  .end(function(err, data) {\n    console.log(data); // { id: 1, content: 'Hello World', headers: { 'x-custom-header': 'value of header' } }\n  })\n;\n```\n\n`mock.del()` works in a similar way.\n\n### Post\n\nYou may set the body of a `POST` request as the second parameter of `mock.post()`\nor in `mock.send()`.  Values set in `send()` will overwrite previously set values.\n\n```js\nmock.post('/topics/:id', function(req) {\n  return {\n    id: req.params.id,\n    body: req.body\n  };\n});\n\nrequest\n  .post('/topics/5', {\n    content: 'I will be overwritten',\n    fromPost: 'Foo'\n  })\n  .send({\n    content: 'Hello world',\n    fromSend: 'Bar'\n  })\n  .end(function(err, data) {\n    console.log(data); // { id: 5, body: { content: 'Hello world', fromPost: 'Foo', fromSend: 'Bar' } }\n  })\n;\n```\n\n`mock.put()`, `mock.patch()` methods works in a similar way.\n\n### Teardown\n\nYou can remove all of the route handlers by calling `mock.clearRoutes()`.  This is useful when defining temporary route handlers for unit tests.\n\n```js\n\n// Using the mocha testing framework\ndefine('My API module', function(){\n\n  beforeEach(function(){\n    // Guarentee each test knows exactly which routes are defined\n    mock.clearRoutes()\n  })\n\n  it('should GET /me', function(done){\n    mock.get('/me', function(){done()})\n    api.getMe()\n  })\n\n  it('should POST /me', function(done){\n    // The GET route handler no longer exists\n    // So there is no chance to see a false positive\n    // if the function actually calls GET /me\n    mock.post('/me', function(){done()})\n    api.saveMe()\n  })\n\n})\n```\n\nOr you can remove only one specified route (by method and url)\n\n```js\n// to register route\nmock.get('/me', function(){done()})\n\n...\n\n// to remove registered handler\nmock.clearRoute('get', '/me');\n\n```\n\n### Rollback library effect\n\nIn some cases it will be useful to remove patches from superagent lib after using mocks.\nIn this cases you can use ```mock.unmock(superagent)``` method, that will rollback all patches that ```mock(superagent)``` call make.\n\n## License\n\nMIT © [Shuvalov Anton](http://shuvalov.info)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa%2Fsuperagent-mocker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fa%2Fsuperagent-mocker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa%2Fsuperagent-mocker/lists"}