Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Mock axios requests for testing
$ npm install moxios --save-dev
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import { equal } from 'assert'
describe('mocking axios requests', function () {
describe('across entire suite', function () {
beforeEach(function () {
moxios.install()
})
afterEach(function () {
moxios.uninstall()
})
it('specify response for a specific request', function (done) {
let input = document.querySelector('.UserList__Filter__Input')
let button = document.querySelector('.UserList__Filter__Button')
input.value = 'flintstone'
button.click()
// Elsewhere in your code axios.get('/users/search', { params: { q: 'flintstone' } }) is called
moxios.wait(function () {
let request = moxios.requests.mostRecent()
request.respondWith({
status: 200,
response: [
{ id: 1, firstName: 'Fred', lastName: 'Flintstone' },
{ id: 2, firstName: 'Wilma', lastName: 'Flintstone' }
]
}).then(function () {
let list = document.querySelector('.UserList__Data')
equal(list.rows.length, 2)
equal(list.rows[0].cells[0].innerHTML, 'Fred')
equal(list.rows[1].cells[0].innerHTML, 'Wilma')
done()
})
})
})
it('stub response for any matching request URL', function (done) {
// Match against an exact URL value
moxios.stubRequest('/say/hello', {
status: 200,
responseText: 'hello'
})
// Alternatively URL can be a RegExp
moxios.stubRequest(/say.*/, {/* ... */})
let onFulfilled = sinon.spy()
axios.get('/say/hello').then(onFulfilled)
moxios.wait(function () {
equal(onFulfilled.getCall(0).args[0].data, 'hello')
done()
})
})
})
it('just for a single spec', function (done) {
moxios.withMock(function () {
let onFulfilled = sinon.spy()
axios.get('/users/12345').then(onFulfilled)
moxios.wait(function () {
let request = moxios.requests.mostRecent()
request.respondWith({
status: 200,
response: {
id: 12345, firstName: 'Fred', lastName: 'Flintstone'
}
}).then(function () {
equal(onFulfilled.called, true)
done()
})
})
})
})
})
moxios is heavily inspired by jasmine-ajax
MIT
FAQs
Mock axios requests for testing
The npm package moxios receives a total of 108,991 weekly downloads. As such, moxios popularity was classified as popular.
We found that moxios demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.