-
-
Save charleskorn/1d8a3d728b32f1f75768 to your computer and use it in GitHub Desktop.
A Jest mock for superagent. Place in your __mocks__ directory.
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'; | |
var mockDelay; | |
var mockError; | |
var mockResponse = { | |
status: function () { | |
return 200; | |
}, | |
ok: true, | |
get: jest.genMockFunction(), | |
toError: jest.genMockFunction() | |
}; | |
var Request = { | |
post: jest.genMockFunction().mockReturnThis(), | |
get: jest.genMockFunction().mockReturnThis(), | |
send: jest.genMockFunction().mockReturnThis(), | |
query: jest.genMockFunction().mockReturnThis(), | |
field: jest.genMockFunction().mockReturnThis(), | |
set: jest.genMockFunction().mockReturnThis(), | |
accept: jest.genMockFunction().mockReturnThis(), | |
timeout: jest.genMockFunction().mockReturnThis(), | |
end: jest.genMockFunction().mockImplementation(function (callback) { | |
if (mockDelay) { | |
this.delayTimer = setTimeout(callback, 0, mockError, mockResponse); | |
return; | |
} | |
callback(mockError, mockResponse); | |
}), | |
__setMockDelay: function (boolValue) { | |
mockDelay = boolValue; | |
}, | |
__setMockResponse: function (mockRes) { | |
mockResponse = mockRes; | |
}, | |
__setMockError: function (mockErr) { | |
mockError = mockErr; | |
}, | |
__setMockResponseBody: function (body) { | |
mockResponse.body = body; | |
} | |
}; | |
module.exports = Request; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did an update to support es6 and the last version of jest
You can find it here: https://gist.github.com/gbalbuena/3ec499535d435712ce16c1eced9f5502