Skip to content

Commit

Permalink
HACK: Fix race
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Nov 12, 2018
1 parent b922684 commit dae26d0
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions lib/httpception.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var resolveFromAfterEach;
var mocker;
var promise;
var mockDefinitionForTheCurrentTest;
var promiseLessModeAborted;

var afterEachRegistered = false;
function ensureAfterEachIsRegistered() {
Expand Down Expand Up @@ -73,25 +74,35 @@ module.exports = function httpception(mockDefinition, promiseFactory) {
});

if (promiseFactory) {
resolveFromAfterEach = false;
promiseLessModeAborted = true;
if (resolveFromAfterEach) {
resolveFromAfterEach();
}

return mocker
.mock(promiseFactory)
.then(function() {
return expect(
promiseFactory,
'to perform HTTP traffic',
mocker.requestDescriptions
);
})
.then(function() {
mocker = undefined;
return expect
.promise((resolve, reject) => {
setTimeout(resolve, 5);
})
.catch(function(e) {
mocker = undefined;
throw e;
.then(() => {
return mocker
.mock(promiseFactory)
.then(function() {
return expect(
promiseFactory,
'to perform HTTP traffic',
mocker.requestDescriptions
);
})
.then(function() {
mocker = undefined;
})
.catch(function(e) {
mocker = undefined;
throw e;
});
});
} else if (!promise) {
promiseLessModeAborted = false;
var resolvePromise = expect.promise(function(resolve, reject) {
resolveFromAfterEach = resolve;
});
Expand All @@ -102,21 +113,22 @@ module.exports = function httpception(mockDefinition, promiseFactory) {
});
});

promise = Promise.all([resolvePromise, mockerPromise])
.then(result => {
promise = Promise.all([resolvePromise, mockerPromise]).then(result => {
if (!promiseLessModeAborted) {
return expect(
function afterEach() {},
'to perform HTTP traffic',
mocker.requestDescriptions
);
})
.then(function() {
mocker = undefined;
})
.catch(function(e) {
mocker = undefined;
throw e;
});
)
.then(function() {
mocker = undefined;
})
.catch(function(e) {
mocker = undefined;
throw e;
});
}
});
}
};

Expand Down

0 comments on commit dae26d0

Please sign in to comment.