[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) { async _safelyRunFunction(func, timeOutDuration, description) {
const syncResultOrPromise = tryCatcher(func); const syncResultOrPromise = captureThrownErrors(func);
if (syncResultOrPromise.error) { if (syncResultOrPromise.error) {
// Synchronous Error // Synchronous Error
@ -314,49 +314,59 @@ class TestRun {
} }
// Asynchronous Error // Asynchronous Error
return promiseToCallBack(syncResultOrPromise.value, timeOutDuration, description); return capturePromiseErrors(syncResultOrPromise.result, timeOutDuration, description);
} }
} }
/** /**
* Try catch to object * Call a function and capture any errors that are immediately thrown.
* @returns {{}} * @returns {Object} Object containing result of executing the function, or the error
* message that was captured
* @private * @private
*/ */
function tryCatcher(func) { function captureThrownErrors(func) {
const result = {}; const result = {};
try { try {
result.value = func(); result.result = func();
} catch (e) { } catch (error) {
result.error = e; result.error = error;
} }
return result; return result;
} }
/** /**
* Make a promise callback-able to trap errors * Wraps a promise so that if it's rejected or an error is thrown while it's being
* @param promise * 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 * @private
*/ */
function promiseToCallBack(promise, timeoutDuration, description) { function capturePromiseErrors(target, timeoutDuration, description) {
let returnValue = null; let returnValue = null;
try { try {
returnValue = Promise.resolve(promise) returnValue = Promise.resolve(target)
.then(() => { .then(() => {
return null; return null;
}, (error) => { }, (error) => {
return Promise.resolve(error); return Promise.resolve(error);
}) })
.timeout(timeoutDuration, `${description} took longer than ${timeoutDuration}ms. This can be extended with the timeout option.`)
.catch((error) => { .catch((error) => {
return Promise.resolve(error); return Promise.resolve(error);
}); })
.timeout(timeoutDuration,
`${description} took longer than ${timeoutDuration}ms. This can be extended with the timeout option.`
);
} catch (error) { } catch (error) {
returnValue = Promise.resolve(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> <Text style={styles.testLabel}>{testContextName}:</Text><Text style={styles.description}>{description}</Text>
</ScrollView> </ScrollView>
<ScrollView> <ScrollView>
<Text style={styles.header}>Test Error</Text> <Text style={styles.testLabel}>Test Error</Text>
<Text style={styles.code}> <Text style={styles.description}>
<Text>{message || 'None'}</Text> <Text>{message || 'None'}</Text>
</Text> </Text>
</ScrollView> </ScrollView>
<Text style={styles.header}> <Text style={styles.testLabel}>
Test Code Preview Test Code Preview
</Text> </Text>
<ScrollView> <ScrollView>
<Text style={styles.code}> <Text style={styles.description}>
{beautify(removeLastLine(removeFirstLine(func.toString())), { indent_size: 4, break_chained_methods: true })} {beautify(removeLastLine(removeFirstLine(func.toString())), { indent_size: 4, break_chained_methods: true })}
</Text> </Text>
</ScrollView> </ScrollView>
@ -102,27 +102,17 @@ const styles = StyleSheet.create({
flex: 1, flex: 1,
backgroundColor: '#ffffff', backgroundColor: '#ffffff',
}, },
header: { testLabel: {
padding: 5,
backgroundColor: '#0288d1',
fontWeight: '600', fontWeight: '600',
fontSize: 18, color: '#ffffff',
backgroundColor: '#000', fontSize: 16
color: '#fff',
padding: 5,
},
code: {
backgroundColor: '#3F373A',
color: '#c3c3c3',
padding: 5,
fontSize: 12,
}, },
description: { description: {
padding: 5, padding: 5,
fontSize: 14, fontSize: 14,
}, },
testLabel: {
fontWeight: '600',
fontSize: 16
}
}); });
function select({ tests, testContexts }, { navigation: { state: { params: { testId } } } }) { function select({ tests, testContexts }, { navigation: { state: { params: { testId } } } }) {