Move revertible methods to finally

This commit is contained in:
Ilya Goncharov
2019-09-27 18:39:49 +03:00
parent 7680c21fb6
commit 71963de70c
@@ -77,6 +77,9 @@ export function runWithTeamCityReporter(
} else { } else {
const startTime = timer ? timer.start() : null; const startTime = timer ? timer.start() : null;
teamCity.sendMessage('testStarted', withName(name)); teamCity.sendMessage('testStarted', withName(name));
const revertLogMethods: CallableFunction[] = [];
try { try {
runner.test(name, isIgnored, () => { runner.test(name, isIgnored, () => {
const log = (type: string) => function (message?: any, ...optionalParams: any[]) { const log = (type: string) => function (message?: any, ...optionalParams: any[]) {
@@ -97,13 +100,14 @@ export function runWithTeamCityReporter(
[method: string]: (message?: any, ...optionalParams: any[]) => void [method: string]: (message?: any, ...optionalParams: any[]) => void
}; };
const revertLogMethods = logMethods.map(method => { logMethods
const realMethod = globalConsole[method]; .map(method => {
globalConsole[method] = log(method); const realMethod = globalConsole[method];
return () => globalConsole[method] = realMethod globalConsole[method] = log(method);
}); return () => globalConsole[method] = realMethod
})
.forEach(method => revertLogMethods.push(method));
fn(); fn();
revertLogMethods.forEach(revert => revert());
}); });
} catch (e) { } catch (e) {
const data: TeamCityMessageData = withName(name, { const data: TeamCityMessageData = withName(name, {
@@ -118,6 +122,7 @@ export function runWithTeamCityReporter(
} }
teamCity.sendMessage('testFailed', data); teamCity.sendMessage('testFailed', data);
} finally { } finally {
revertLogMethods.forEach(revert => revert());
currentAssertionResult = null; currentAssertionResult = null;
const data: TeamCityMessageData = withName(name); const data: TeamCityMessageData = withName(name);
if (startTime) { if (startTime) {