Skip to content

Instantly share code, notes, and snippets.

@musicin3d
Last active February 18, 2018 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musicin3d/d2d0d63ff7062f40776d27db64ea77ba to your computer and use it in GitHub Desktop.
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.
/*
* 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