2021-06-15 19:51:25 +02:00
|
|
|
const { promises: fs } = require('fs');
|
2023-01-12 15:38:12 +01:00
|
|
|
const path = require('path');
|
2021-06-15 19:51:25 +02:00
|
|
|
const yargs = require('yargs/yargs');
|
|
|
|
const { hideBin } = require('yargs/helpers');
|
|
|
|
const { runInShell } = require('../../development/lib/run-command');
|
|
|
|
const { exitWithError } = require('../../development/lib/exit-with-error');
|
|
|
|
const { retry } = require('../../development/lib/retry');
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const { argv } = yargs(hideBin(process.argv))
|
|
|
|
.usage(
|
|
|
|
'$0 [options] <e2e-test-path>',
|
|
|
|
'Run a single E2E test, with a variable number of retries.',
|
|
|
|
(_yargs) =>
|
|
|
|
_yargs
|
|
|
|
.option('browser', {
|
|
|
|
default: process.env.SELENIUM_BROWSER,
|
|
|
|
description: `Set the browser used; either 'chrome' or 'firefox'.`,
|
|
|
|
type: 'string',
|
|
|
|
choices: ['chrome', 'firefox'],
|
|
|
|
})
|
2022-12-13 20:30:47 +01:00
|
|
|
.option('debug', {
|
|
|
|
default: process.env.E2E_DEBUG === 'true',
|
|
|
|
description:
|
|
|
|
'Run tests in debug mode, logging each driver interaction',
|
|
|
|
type: 'boolean',
|
|
|
|
})
|
2021-06-15 19:51:25 +02:00
|
|
|
.option('retries', {
|
|
|
|
default: 0,
|
|
|
|
description:
|
|
|
|
'Set how many times the test should be retried upon failure.',
|
|
|
|
type: 'number',
|
|
|
|
})
|
2023-07-10 21:40:13 +02:00
|
|
|
.option('retry-until-failure', {
|
|
|
|
default: false,
|
|
|
|
description: 'Retries until the test fails',
|
|
|
|
type: 'boolean',
|
|
|
|
})
|
2021-06-16 17:12:20 +02:00
|
|
|
.option('leave-running', {
|
|
|
|
default: false,
|
|
|
|
description:
|
|
|
|
'Leaves the browser running after a test fails, along with anything else that the test used (ganache, the test dapp, etc.)',
|
|
|
|
type: 'boolean',
|
|
|
|
})
|
2021-06-15 19:51:25 +02:00
|
|
|
.positional('e2e-test-path', {
|
|
|
|
describe: 'The path for the E2E test to run.',
|
|
|
|
type: 'string',
|
|
|
|
normalize: true,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.strict()
|
|
|
|
.help('help');
|
|
|
|
|
2023-07-10 21:40:13 +02:00
|
|
|
const {
|
|
|
|
browser,
|
|
|
|
debug,
|
|
|
|
e2eTestPath,
|
|
|
|
retries,
|
|
|
|
retryUntilFailure,
|
|
|
|
leaveRunning,
|
|
|
|
} = argv;
|
2021-06-15 19:51:25 +02:00
|
|
|
|
|
|
|
if (!browser) {
|
|
|
|
exitWithError(
|
|
|
|
`"The browser must be set, via the '--browser' flag or the SELENIUM_BROWSER environment variable`,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
} else if (browser !== process.env.SELENIUM_BROWSER) {
|
|
|
|
process.env.SELENIUM_BROWSER = browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const stat = await fs.stat(e2eTestPath);
|
|
|
|
if (!stat.isFile()) {
|
|
|
|
exitWithError('Test path must be a file');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ENOENT') {
|
|
|
|
exitWithError('Test path specified does not exist');
|
|
|
|
return;
|
|
|
|
} else if (error.code === 'EACCES') {
|
|
|
|
exitWithError(
|
|
|
|
'Access to test path is forbidden by file access permissions',
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:34:44 +01:00
|
|
|
const testFileName = path.basename(e2eTestPath);
|
|
|
|
|
2022-12-13 20:30:47 +01:00
|
|
|
if (debug) {
|
|
|
|
process.env.E2E_DEBUG = 'true';
|
|
|
|
}
|
|
|
|
|
2022-12-19 16:27:47 +01:00
|
|
|
let testTimeoutInMilliseconds = 80 * 1000;
|
2022-07-19 17:22:38 +02:00
|
|
|
let exit = '--exit';
|
2022-04-21 17:51:03 +02:00
|
|
|
|
2021-06-16 17:12:20 +02:00
|
|
|
if (leaveRunning) {
|
|
|
|
process.env.E2E_LEAVE_RUNNING = 'true';
|
2022-04-21 17:51:03 +02:00
|
|
|
testTimeoutInMilliseconds = 0;
|
2022-07-19 17:22:38 +02:00
|
|
|
exit = '--no-exit';
|
2021-06-16 17:12:20 +02:00
|
|
|
}
|
|
|
|
|
2023-01-12 15:38:12 +01:00
|
|
|
const configFile = path.join(__dirname, '.mocharc.js');
|
2023-02-20 18:13:12 +01:00
|
|
|
const extraArgs = process.env.E2E_ARGS?.split(' ') || [];
|
2023-01-12 15:38:12 +01:00
|
|
|
|
2023-02-10 20:34:01 +01:00
|
|
|
const dir = 'test/test-results/e2e';
|
|
|
|
fs.mkdir(dir, { recursive: true });
|
|
|
|
|
2023-07-10 21:40:13 +02:00
|
|
|
await retry({ retries, retryUntilFailure }, async () => {
|
2023-02-10 20:34:01 +01:00
|
|
|
await runInShell(
|
|
|
|
'yarn',
|
|
|
|
[
|
|
|
|
'mocha',
|
|
|
|
`--config=${configFile}`,
|
|
|
|
`--timeout=${testTimeoutInMilliseconds}`,
|
|
|
|
'--reporter=xunit',
|
2023-02-20 18:13:12 +01:00
|
|
|
...extraArgs,
|
2023-02-10 20:34:01 +01:00
|
|
|
e2eTestPath,
|
|
|
|
exit,
|
|
|
|
],
|
|
|
|
`${dir}/${testFileName}.xml`,
|
|
|
|
);
|
2021-06-15 19:51:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((error) => {
|
|
|
|
exitWithError(error);
|
|
|
|
});
|