2021-06-15 19:51:25 +02:00
|
|
|
const path = require('path');
|
|
|
|
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');
|
2023-08-17 15:43:01 +02:00
|
|
|
const { loadBuildTypesConfig } = require('../../development/lib/build-type');
|
2021-06-15 19:51:25 +02:00
|
|
|
|
2022-10-28 16:09:46 +02:00
|
|
|
const getTestPathsForTestDir = async (testDir) => {
|
2023-07-31 14:48:48 +02:00
|
|
|
const testFilenames = await fs.readdir(testDir, { withFileTypes: true });
|
|
|
|
const testPaths = [];
|
|
|
|
|
|
|
|
for (const itemInDirectory of testFilenames) {
|
|
|
|
const fullPath = path.join(testDir, itemInDirectory.name);
|
|
|
|
|
|
|
|
if (itemInDirectory.isDirectory()) {
|
|
|
|
const subDirPaths = await getTestPathsForTestDir(fullPath);
|
|
|
|
testPaths.push(...subDirPaths);
|
|
|
|
} else if (fullPath.endsWith('.spec.js')) {
|
|
|
|
testPaths.push(fullPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:09:46 +02:00
|
|
|
return testPaths;
|
|
|
|
};
|
|
|
|
|
2022-11-23 15:21:19 +01:00
|
|
|
// Heavily inspired by: https://stackoverflow.com/a/51514813
|
|
|
|
// Splits the array into totalChunks chunks with a decent spread of items in each chunk
|
|
|
|
function chunk(array, totalChunks) {
|
|
|
|
const copyArray = [...array];
|
2022-11-08 18:28:06 +01:00
|
|
|
const result = [];
|
2022-11-23 15:21:19 +01:00
|
|
|
for (let chunkIndex = totalChunks; chunkIndex > 0; chunkIndex--) {
|
|
|
|
result.push(copyArray.splice(0, Math.ceil(copyArray.length / chunkIndex)));
|
2022-11-08 18:28:06 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-15 19:51:25 +02:00
|
|
|
async function main() {
|
|
|
|
const { argv } = yargs(hideBin(process.argv))
|
|
|
|
.usage(
|
|
|
|
'$0 [options]',
|
|
|
|
'Run all E2E tests, with a variable number of retries.',
|
|
|
|
(_yargs) =>
|
|
|
|
_yargs
|
|
|
|
.option('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',
|
|
|
|
})
|
2022-03-10 23:01:55 +01:00
|
|
|
.option('snaps', {
|
|
|
|
description: `run snaps e2e tests`,
|
|
|
|
type: 'boolean',
|
|
|
|
})
|
2022-11-25 12:06:48 +01:00
|
|
|
.option('mv3', {
|
|
|
|
description: `run mv3 specific e2e tests`,
|
|
|
|
type: 'boolean',
|
|
|
|
})
|
2023-08-17 15:43:01 +02:00
|
|
|
.option('build-type', {
|
|
|
|
description: `Sets the build-type to test for. This may filter out tests.`,
|
|
|
|
type: 'string',
|
|
|
|
choices: Object.keys(loadBuildTypesConfig().buildTypes),
|
|
|
|
})
|
2021-06-15 19:51:25 +02:00
|
|
|
.option('retries', {
|
|
|
|
description:
|
|
|
|
'Set how many times the test should be retried upon failure.',
|
|
|
|
type: 'number',
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.strict()
|
|
|
|
.help('help');
|
|
|
|
|
2023-08-17 15:43:01 +02:00
|
|
|
const { browser, debug, retries, snaps, mv3, buildType } = argv;
|
2022-03-10 23:01:55 +01:00
|
|
|
|
2022-11-25 12:06:48 +01:00
|
|
|
let testPaths;
|
2021-06-15 19:51:25 +02:00
|
|
|
|
2022-03-10 23:01:55 +01:00
|
|
|
if (snaps) {
|
2022-11-25 12:06:48 +01:00
|
|
|
const testDir = path.join(__dirname, 'snaps');
|
|
|
|
testPaths = await getTestPathsForTestDir(testDir);
|
2023-08-17 15:43:01 +02:00
|
|
|
|
|
|
|
if (buildType && buildType !== 'flask') {
|
|
|
|
// These tests should only be ran on Flask for now
|
|
|
|
const filteredTests = [
|
|
|
|
'test-snap-manageAccount.spec.js',
|
|
|
|
'test-snap-rpc.spec.js',
|
|
|
|
'test-snap-lifecycle.spec.js',
|
|
|
|
];
|
|
|
|
testPaths = testPaths.filter((p) =>
|
|
|
|
filteredTests.every((filteredTest) => !p.endsWith(filteredTest)),
|
|
|
|
);
|
|
|
|
}
|
2022-11-25 12:06:48 +01:00
|
|
|
} else {
|
|
|
|
const testDir = path.join(__dirname, 'tests');
|
2022-10-28 16:09:46 +02:00
|
|
|
testPaths = [
|
2022-11-25 12:06:48 +01:00
|
|
|
...(await getTestPathsForTestDir(testDir)),
|
2022-11-03 14:54:49 +01:00
|
|
|
...(await getTestPathsForTestDir(path.join(__dirname, 'swaps'))),
|
2023-03-14 16:21:24 +01:00
|
|
|
...(await getTestPathsForTestDir(path.join(__dirname, 'nft'))),
|
2023-06-23 16:48:44 +02:00
|
|
|
...(await getTestPathsForTestDir(path.join(__dirname, 'metrics'))),
|
2022-10-28 16:09:46 +02:00
|
|
|
path.join(__dirname, 'metamask-ui.spec.js'),
|
|
|
|
];
|
2022-11-25 12:06:48 +01:00
|
|
|
|
|
|
|
if (mv3) {
|
|
|
|
testPaths.push(
|
|
|
|
...(await getTestPathsForTestDir(path.join(__dirname, 'mv3'))),
|
|
|
|
);
|
|
|
|
}
|
2022-03-10 23:01:55 +01:00
|
|
|
}
|
2021-06-15 19:51:25 +02:00
|
|
|
|
|
|
|
const runE2eTestPath = path.join(__dirname, 'run-e2e-test.js');
|
|
|
|
|
|
|
|
const args = [runE2eTestPath];
|
|
|
|
if (browser) {
|
|
|
|
args.push('--browser', browser);
|
|
|
|
}
|
|
|
|
if (retries) {
|
|
|
|
args.push('--retries', retries);
|
|
|
|
}
|
2022-12-13 20:30:47 +01:00
|
|
|
if (debug) {
|
|
|
|
args.push('--debug');
|
|
|
|
}
|
2021-06-15 19:51:25 +02:00
|
|
|
|
2022-11-08 18:28:06 +01:00
|
|
|
// For running E2Es in parallel in CI
|
|
|
|
const currentChunkIndex = process.env.CIRCLE_NODE_INDEX ?? 0;
|
|
|
|
const totalChunks = process.env.CIRCLE_NODE_TOTAL ?? 1;
|
2022-11-23 15:21:19 +01:00
|
|
|
const chunks = chunk(testPaths, totalChunks);
|
2022-11-08 18:28:06 +01:00
|
|
|
const currentChunk = chunks[currentChunkIndex];
|
|
|
|
|
|
|
|
for (const testPath of currentChunk) {
|
2023-02-10 20:34:01 +01:00
|
|
|
const dir = 'test/test-results/e2e';
|
|
|
|
fs.mkdir(dir, { recursive: true });
|
2021-06-15 19:51:25 +02:00
|
|
|
await runInShell('node', [...args, testPath]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((error) => {
|
|
|
|
exitWithError(error);
|
|
|
|
});
|