1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 15:50:28 +01:00
metamask-extension/test/e2e/run-e2e-test.js
Mark Stacey e4cf12674e
Simplify Mocha npm scripts (#12313)
The npm scripts used to run Mocha scripts have been greatly simplified.
As we transition more tests from Mocha to Jest it was becoming
increasingly difficult to update the CLI arguments to keep all of these
scripts working correctly. This reorganization should make that process
much simpler.

The base Mocha options are in `.mocharc.js` - all except for the target
tests to run. Those are still given via the CLI. There is a second
config file specifically for the `test:unit:lax` tests (i.e. the Mocha
tests that have no coverage requirements) because it requires a change
to the `ignored` configuration property. We can create an additional
configuration file for each test script we add that needs further
configuration changes.

The `test:unit:path` script used to be used to run Mocha tests at a
given path. Now that can be done using `yarn mocha` instead, so this
script has been removed.

The `yarn watch` command has been broken for some time now, so it has
been removed as well. Mocha tests can still be run with a file watcher
using `yarn mocha --watch <path>` or `yarn test:unit:mocha --watch`.

The README has been updated to remove references about the `watch`
command that was removed. I considered explaining the other test
scripts there as well, but they were difficult to explain I will
attempt to update the README after making further simplifications
instead.
2021-10-12 10:10:33 -02:30

89 lines
2.5 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',
})
.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',
})
.positional('e2e-test-path', {
describe: 'The path for the E2E test to run.',
type: 'string',
normalize: true,
}),
)
.strict()
.help('help');
const { browser, e2eTestPath, retries, leaveRunning } = 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;
}
if (leaveRunning) {
process.env.E2E_LEAVE_RUNNING = 'true';
}
await retry({ retries }, async () => {
await runInShell('yarn', [
'mocha',
'--no-config',
'--no-timeouts',
e2eTestPath,
]);
});
}
main().catch((error) => {
exitWithError(error);
});