-
-
Save radiodario/782045a01b3f8cf427b199e4ab86c4fa to your computer and use it in GitHub Desktop.
Jest superagent mock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
//mock for superagent - __mocks__/superagent.js | |
var mockDelay; | |
var mockError; | |
var mockResponse = { | |
status() { | |
return 200; | |
}, | |
ok() { | |
return true; | |
}, | |
body: { | |
walla: true | |
}, | |
get: jest.genMockFunction(), | |
toError: jest.genMockFunction() | |
}; | |
var Request = { | |
post() { | |
return this; | |
}, | |
get() { | |
return this; | |
}, | |
send() { | |
return this; | |
}, | |
query() { | |
return this; | |
}, | |
field() { | |
return this; | |
}, | |
set() { | |
return this; | |
}, | |
accept() { | |
return this; | |
}, | |
auth() { | |
return this; | |
}, | |
timeout() { | |
return this; | |
}, | |
end: jest.genMockFunction().mockImplementation(function(callback) { | |
if (mockDelay) { | |
this.delayTimer = setTimeout(callback, 0, mockError, mockResponse); | |
return; | |
} | |
callback(mockError, mockResponse); | |
}), | |
//expose helper methods for tests to set | |
__setMockDelay(boolValue) { | |
mockDelay = boolValue; | |
}, | |
__setMockResponse(mockRes) { | |
mockResponse = mockRes; | |
}, | |
__setMockError(mockErr) { | |
mockError = mockErr; | |
} | |
}; | |
module.exports = Request; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment