Last active
February 18, 2018 03:18
-
-
Save musicin3d/d2d0d63ff7062f40776d27db64ea77ba to your computer and use it in GitHub Desktop.
Test case utility for supertests, featuring a dirty monkey patch for printing response body when expect() fails.
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
/* | |
* Inspired by a patch by github.com/RyanMcDonald. | |
* https://github.com/visionmedia/supertest/issues/367#issuecomment-250627577 | |
* No rights reserved. :) | |
*/ | |
const request = require('supertest') | |
let oldAssertStatus = null | |
module.exports = { | |
patchExpect(){ | |
if(oldAssertStatus) return | |
// noinspection JSUnresolvedVariable | |
oldAssertStatus = request.Test.prototype._assertStatus | |
// noinspection JSUnresolvedVariable | |
request.Test.prototype._assertStatus = function (status, res) { | |
let result = oldAssertStatus(status, res) | |
if(result instanceof Error && typeof res.body == 'object'){ | |
result.message += `. Response body: \'${JSON.stringify(res.body)}\'` | |
} | |
return result | |
} | |
}, | |
unpatchExpect(){ | |
// noinspection JSUnresolvedVariable | |
request.Test.prototype._assertStatus = oldAssertStatus | |
oldAssertStatus = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment