1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/test/e2e/run-e2e-test.js
Mark Stacey 7535d63466
Add run-e2e-test.js script (#11301)
This script makes it easier to run an individual E2E test. In the past
I've run individual scripts by editing `run-all.sh` manually, but now
that can be done more easily with this script. It also allows setting
the number of retries to use and the browser to use from the CLI.

This script has been added as an npm script as well, called
'test:e2e:single'.

The `run-all.sh` script was rewritten in JavaScript to make it easier
to pass through a `--retries` argument.

The default number of retries has been set to zero to make local
testing easier. It has been set to 2 on CI.

This was mainly done to consolidate the code used to run an E2E test in
one place, to make later improvements easier.
2021-06-15 15:21:25 -02:30

74 lines
2.1 KiB
JavaScript

const { promises: fs } = require('fs');
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'],
})
.option('retries', {
default: 0,
description:
'Set how many times the test should be retried upon failure.',
type: 'number',
})
.positional('e2e-test-path', {
describe: 'The path for the E2E test to run.',
type: 'string',
normalize: true,
}),
)
.strict()
.help('help');
const { browser, e2eTestPath, retries } = argv;
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;
}
await retry(retries, async () => {
await runInShell('yarn', ['mocha', '--no-timeouts', e2eTestPath]);
});
}
main().catch((error) => {
exitWithError(error);
});