1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00
metamask-extension/test/e2e/run-e2e-test.js
Mark Stacey 5857b85225
Add e2e debugging logs (#16937)
A `debug` flag has been added to our e2e test runner scripts, enabling
e2e debug logs. When this flag is enabled, all driver interactions will
be logged to the console. This is extremely useful when debugging e2e
tests, because it lets you known how far the test had progressed before
failing.

This flag should work with all existing e2e test scripts, including
both `test:e2e:single` and all of the test commands that run entire
test suites.

The README has been updated to reference this flag in the section
regarding how to run a single e2e test. To ensure this wasn't totally
missed for the other scripts, I added a line suggesting that users use
`--help` to see all supported options.
2022-12-13 16:00:47 -03:30

106 lines
2.9 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('debug', {
default: process.env.E2E_DEBUG === 'true',
description:
'Run tests in debug mode, logging each driver interaction',
type: 'boolean',
})
.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, debug, 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 (debug) {
process.env.E2E_DEBUG = 'true';
}
let testTimeoutInMilliseconds = 60 * 1000;
let exit = '--exit';
if (leaveRunning) {
process.env.E2E_LEAVE_RUNNING = 'true';
testTimeoutInMilliseconds = 0;
exit = '--no-exit';
}
await retry({ retries }, async () => {
await runInShell('yarn', [
'mocha',
'--no-config',
'--timeout',
testTimeoutInMilliseconds,
e2eTestPath,
exit,
]);
});
}
main().catch((error) => {
exitWithError(error);
});