{"id":13493440,"url":"https://github.com/axios/moxios","last_synced_at":"2025-05-15T18:00:16.989Z","repository":{"id":8808698,"uuid":"59626425","full_name":"axios/moxios","owner":"axios","description":"Mock axios requests for testing","archived":false,"fork":false,"pushed_at":"2024-08-02T09:41:14.000Z","size":725,"stargazers_count":1430,"open_issues_count":22,"forks_count":88,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-27T18:36:57.520Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/axios.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-25T03:03:06.000Z","updated_at":"2025-03-07T17:48:48.000Z","dependencies_parsed_at":"2023-01-13T15:00:50.010Z","dependency_job_id":"c8e15a61-1e0c-48bc-add3-0cdca18644a9","html_url":"https://github.com/axios/moxios","commit_stats":{"total_commits":39,"total_committers":18,"mean_commits":"2.1666666666666665","dds":0.6153846153846154,"last_synced_commit":"52e0d9b041ebafad9106a6f734f701b7b8712a2c"},"previous_names":["mzabriskie/moxios"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axios%2Fmoxios","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axios%2Fmoxios/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axios%2Fmoxios/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axios%2Fmoxios/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/axios","download_url":"https://codeload.github.com/axios/moxios/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252294749,"owners_count":21724965,"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":[],"created_at":"2024-07-31T19:01:15.272Z","updated_at":"2025-05-15T18:00:16.887Z","avatar_url":"https://github.com/axios.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# moxios [![build status](https://img.shields.io/travis/axios/moxios.svg?style=flat-square)](https://travis-ci.org/axios/moxios)\n\nMock [axios](https://github.com/axios/axios) requests for testing\n\n## Installing\n\n```bash\n$ npm install moxios --save-dev\n```\n\n## Example\n\n```js\nimport axios from 'axios'\nimport moxios from 'moxios'\nimport sinon from 'sinon'\nimport { equal } from 'assert'\n\ndescribe('mocking axios requests', function () {\n\n  describe('across entire suite', function () {\n\n    beforeEach(function () {\n      // import and pass your custom axios instance to this method\n      moxios.install()\n    })\n\n    afterEach(function () {\n      // import and pass your custom axios instance to this method\n      moxios.uninstall()\n    })\n\n    it('specify response for a specific request', function (done) {\n      let input = document.querySelector('.UserList__Filter__Input')\n      let button = document.querySelector('.UserList__Filter__Button')\n\n      input.value = 'flintstone'\n      button.click()\n\n      // Elsewhere in your code axios.get('/users/search', { params: { q: 'flintstone' } }) is called\n\n      moxios.wait(function () {\n        let request = moxios.requests.mostRecent()\n        request.respondWith({\n          status: 200,\n          response: [\n            { id: 1, firstName: 'Fred', lastName: 'Flintstone' },\n            { id: 2, firstName: 'Wilma', lastName: 'Flintstone' }\n          ]\n        })\n          .then(function () {\n            let list = document.querySelector('.UserList__Data')\n            equal(list.rows.length, 2)\n            equal(list.rows[0].cells[0].innerHTML, 'Fred')\n            equal(list.rows[1].cells[0].innerHTML, 'Wilma')\n            done()\n          })\n      })\n    })\n\n    it('stub response for any matching request URL', function (done) {\n      // Match against an exact URL value\n      moxios.stubRequest('/say/hello', {\n        status: 200,\n        responseText: 'hello'\n      })\n\n      // Alternatively URL can be a RegExp\n      moxios.stubRequest(/say.*/, {/* … */})\n\n      let onFulfilled = sinon.spy()\n      axios.get('/say/hello').then(onFulfilled)\n\n      moxios.wait(function () {\n        equal(onFulfilled.getCall(0).args[0].data, 'hello')\n        done()\n      })\n    })\n\n  })\n\n  it('just for a single spec', function (done) {\n    moxios.withMock(function () {\n      let onFulfilled = sinon.spy()\n      axios.get('/users/12345').then(onFulfilled)\n\n      moxios.wait(function () {\n        let request = moxios.requests.mostRecent()\n        request.respondWith({\n          status: 200,\n          response: {\n            id: 12345, firstName: 'Fred', lastName: 'Flintstone'\n          }\n        })\n          .then(function () {\n            equal(onFulfilled.called, true)\n            done()\n          })\n      })\n    })\n  })\n\n  it('Should reject the request', function (done) {\n    const errorResp = {\n      status: 400,\n      response: { message: 'invalid data' }\n    }\n\n    moxios.wait(function () {\n      let request = moxios.requests.mostRecent()\n      request.reject(errorResp)\n    })\n      .catch(function (err) {\n        equal(err.status, errorResp.status)\n        equal(err.response.message, errorResp.response.message)\n        done()\n      })\n  })\n})\n```\n\n## Mocking a axios.create() instance\n\n```js\ndescribe('some-thing', () =\u003e {\n  let axiosInstance;\n  beforeEach(() =\u003e {\n    axiosInstance = axios.create();\n    moxios.install(axiosInstance);\n  })\n  afterEach(() =\u003e {\n    moxios.uninstall(axiosInstance)\n  })\n  it('should axios a thing', (done) =\u003e {\n    moxios.stubRequest('http://www.somesite.com/awesome-url', {\n      status: 200,\n      responseText: '…'\n    })\n    axiosInstance.get('http://www.somesite.com/awesome-url')\n      .then(res =\u003e assert(res.status === 200))\n      .finally(done)\n  })\n})\n```\n\n## Thanks\n\nmoxios is heavily inspired by [jasmine-ajax](https://github.com/jasmine/jasmine-ajax)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faxios%2Fmoxios","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faxios%2Fmoxios","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faxios%2Fmoxios/lists"}