[TestSuite] Clean up TestSuite code further

This commit is contained in:
Aleck Greenham 2017-05-06 12:47:31 +01:00
parent 591cf735d6
commit 6874c83efd
2 changed files with 33 additions and 33 deletions

View File

@ -306,7 +306,7 @@ class TestRun {
}
async _safelyRunFunction(func, timeOutDuration, description) {
const syncResultOrPromise = tryCatcher(func);
const syncResultOrPromise = captureThrownErrors(func);
if (syncResultOrPromise.error) {
// Synchronous Error
@ -314,49 +314,59 @@ class TestRun {
}
// Asynchronous Error
return promiseToCallBack(syncResultOrPromise.value, timeOutDuration, description);
return capturePromiseErrors(syncResultOrPromise.result, timeOutDuration, description);
}
}
/**
* Try catch to object
* @returns {{}}
* Call a function and capture any errors that are immediately thrown.
* @returns {Object} Object containing result of executing the function, or the error
* message that was captured
* @private
*/
function tryCatcher(func) {
function captureThrownErrors(func) {
const result = {};
try {
result.value = func();
} catch (e) {
result.error = e;
result.result = func();
} catch (error) {
result.error = error;
}
return result;
}
/**
* Make a promise callback-able to trap errors
* @param promise
* Wraps a promise so that if it's rejected or an error is thrown while it's being
* evaluated, it's captured and thrown no further
* @param {*} target - Target to wrap. If a thenable object, it's wrapped so if it's
* rejected or an error is thrown, it will be captured. If a non-thenable object,
* wrapped in resolved promise and returned.
* @param {Number} timeoutDuration - Number of milliseconds the promise is allowed
* to pend before it's considered timed out
* @param {String} description - Description of the context the promises is defined
* in, used for reporting where a timeout occurred in the resulting error message.
* @private
*/
function promiseToCallBack(promise, timeoutDuration, description) {
function capturePromiseErrors(target, timeoutDuration, description) {
let returnValue = null;
try {
returnValue = Promise.resolve(promise)
returnValue = Promise.resolve(target)
.then(() => {
return null;
}, (error) => {
return Promise.resolve(error);
})
.timeout(timeoutDuration, `${description} took longer than ${timeoutDuration}ms. This can be extended with the timeout option.`)
.catch((error) => {
return Promise.resolve(error);
});
})
.timeout(timeoutDuration,
`${description} took longer than ${timeoutDuration}ms. This can be extended with the timeout option.`
);
} catch (error) {
returnValue = Promise.resolve(error);
}

View File

@ -62,16 +62,16 @@ class Test extends React.Component {
<Text style={styles.testLabel}>{testContextName}:</Text><Text style={styles.description}>{description}</Text>
</ScrollView>
<ScrollView>
<Text style={styles.header}>Test Error</Text>
<Text style={styles.code}>
<Text style={styles.testLabel}>Test Error</Text>
<Text style={styles.description}>
<Text>{message || 'None'}</Text>
</Text>
</ScrollView>
<Text style={styles.header}>
<Text style={styles.testLabel}>
Test Code Preview
</Text>
<ScrollView>
<Text style={styles.code}>
<Text style={styles.description}>
{beautify(removeLastLine(removeFirstLine(func.toString())), { indent_size: 4, break_chained_methods: true })}
</Text>
</ScrollView>
@ -102,27 +102,17 @@ const styles = StyleSheet.create({
flex: 1,
backgroundColor: '#ffffff',
},
header: {
testLabel: {
padding: 5,
backgroundColor: '#0288d1',
fontWeight: '600',
fontSize: 18,
backgroundColor: '#000',
color: '#fff',
padding: 5,
},
code: {
backgroundColor: '#3F373A',
color: '#c3c3c3',
padding: 5,
fontSize: 12,
color: '#ffffff',
fontSize: 16
},
description: {
padding: 5,
fontSize: 14,
},
testLabel: {
fontWeight: '600',
fontSize: 16
}
});
function select({ tests, testContexts }, { navigation: { state: { params: { testId } } } }) {