Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Hanno J. Gödecke <[email protected]>
  • Loading branch information
hannojg committed Sep 27, 2022
1 parent b9447c8 commit a0966e1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 14 deletions.
28 changes: 27 additions & 1 deletion e2e/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const OUTPUT_DIR = 'e2e/.results';

/**
* @typedef TestConfig
* @property {string} name
*/

// add your test name here …
const TEST_NAMES = {
AppStartTime: 'App start time',
};

module.exports = {
APP_PACKAGE: 'com.expensify.chat',

Expand All @@ -22,6 +32,22 @@ module.exports = {
// The file we write logs to
LOG_FILE: `${OUTPUT_DIR}/debug.log`,

// The time in milliseconds after which a operation fails due to timeout
// The time in milliseconds after which an operation fails due to timeout
INTERACTION_TIMEOUT: 30_000,

TEST_NAMES,

/**
* Add your test configurations here. At least,
* you need to add a name for your test.
*
* @type {Object.<string, TestConfig>}
*/
TESTS_CONFIG: {
[TEST_NAMES.AppStartTime]: {
name: TEST_NAMES.AppStartTime,

// ... any additional config you might need
},
},
};
19 changes: 19 additions & 0 deletions e2e/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,27 @@ const createServerInstance = () => {
};
};

let activeTestConfig;

/**
* @param {TestConfig} testConfig
*/
const setTestConfig = (testConfig) => {
activeTestConfig = testConfig;
};

const server = createServer(async (req, res) => {
res.statusCode = 200;
switch (req.url) {
case Routes.testConfig: {
if (activeTestConfig == null) {
res.statusCode = 404;
res.end('No test config available');
return;
}
return res.end(JSON.stringify(activeTestConfig));
}

case Routes.testResults: {
const data = await getPostJSONRequestData(req, res);
if (data == null) {
Expand Down Expand Up @@ -120,6 +138,7 @@ const createServerInstance = () => {
});

return {
setTestConfig,
addTestResultListener,
addTestDoneListener,
start: () => new Promise(resolve => server.listen(PORT, resolve)),
Expand Down
6 changes: 6 additions & 0 deletions e2e/server/routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
module.exports = {
// The app calls this endpoint to know which test to run
testConfig: '/test_config',

// when running a test the app reports the results to this endpoint
testResults: '/test_results',

// when the app is done running a test it calls this endpoint
testDone: '/test_done',
};
8 changes: 2 additions & 6 deletions e2e/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
OUTPUT_DIR,
LOG_FILE,
RUNS,
TESTS,
} = require('./config');
const compare = require('./compare/compare');
const Logger = require('./utils/logger');
Expand All @@ -31,12 +32,6 @@ const args = process.argv.slice(2);

const baselineBranch = process.env.baseline || DEFAULT_BASELINE_BRANCH;

const TESTS = [
{
name: 'App start time',
},
];

// clear all files from previous jobs
try {
fs.rmSync(OUTPUT_DIR, {recursive: true, force: true});
Expand Down Expand Up @@ -98,6 +93,7 @@ const runTestsOnBranch = async (branch, baselineOrCompare) => {
// run the tests
for (const test of TESTS) {
const testLog = Logger.progressInfo(`Running test '${test.name}'`);
server.setTestConfig(test);

// We run each test multiple time to average out the results
for (let i = 0; i < RUNS; i++) {
Expand Down
8 changes: 8 additions & 0 deletions src/libs/E2E/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ const submitTestResults = testResult => fetch(`${SERVER_ADDRESS}${Routes.testRes

const submitTestDone = () => fetch(`${SERVER_ADDRESS}${Routes.testDone}`);

/**
* @returns {Promise<TestConfig>}
*/
const getTestConfig = () => fetch(`${SERVER_ADDRESS}${Routes.testConfig}`)
.then(res => res.json())
.then(config => config);

export default {
submitTestResults,
submitTestDone,
getTestConfig,
};
28 changes: 21 additions & 7 deletions src/libs/E2E/reactNativeLaunchingTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
// start the usual app
import '../../../index';
import Performance from '../Performance';
import E2EConfig from '../../../e2e/config';
import E2EClient from './client';

console.debug('==========================');
console.debug('==== Running e2e test ====');
console.debug('==========================');

const tests = [
require('./tests/appStartTimeTest.e2e').default,
];
// import your test here, define its name and config first in e2e/config.js
const tests = {
[E2EConfig.TEST_NAMES.AppStartTime]: require('./tests/appStartTimeTest.e2e'),
};

// Once we receive the TII measurement we know that the app is initialized and ready to be used:
const appReady = new Promise((resolve) => {
Expand All @@ -27,10 +30,21 @@ const appReady = new Promise((resolve) => {
});
});

const testConfig = E2EClient.getTestConfig();

console.debug('[E2E] App startup time test launched, waiting for app to become ready…');
appReady.then(() => {
// TODO: when supporting multiple test cases, this file will decide which test to run.
// The test cases will then be separated in a accompanying /tests folder.
tests[0]();
testConfig.then((config) => {
const test = tests[config.name];
if (!test) {
// instead of throwing, report the error to the server, which is better for UX
return E2EClient.submitTestResults({
name: config.name,
error: `Test ${config.name} not found`,
});
}

appReady.then(() => {
test();
});
});

0 comments on commit a0966e1

Please sign in to comment.