diff --git a/lib/unexpectedMitm.js b/lib/unexpectedMitm.js index a078f60..5aa0d48 100644 --- a/lib/unexpectedMitm.js +++ b/lib/unexpectedMitm.js @@ -829,9 +829,20 @@ module.exports = { .addAssertion(' with http mocked out [and verified] [with extra info] ', function (expect, subject, requestDescriptions) { // ... expect.errorMode = 'nested'; var mitm = createMitm(); + var cleanedUpMitm = false; var shouldBeVerified = checkEnvFlag('UNEXPECTED_MITM_VERIFY') || expect.flags['and verified']; var shouldReturnExtraInfo = expect.flags['with extra info']; + // XXX: Register footgun detection in case an uncaught exception + // breaks the test in such a way that our finally block will not + // be called, and mitm thus will not be cleaned up. + afterEach(function () { + if (!cleanedUpMitm) { + cleanedUpMitm = true; + mitm.disable(); + } + }); + if (!Array.isArray(requestDescriptions)) { if (typeof requestDescriptions === 'undefined') { requestDescriptions = []; @@ -1120,6 +1131,7 @@ module.exports = { } }); }).finally(function () { + cleanedUpMitm = true; mitm.disable(); });