Skip to content

Commit 91dad03

Browse files
committed
build: add option for running all component tests
Adds a `--all` option to the `yarn test` script. This option can be used to run _all_ component tests in the repository. Flags like `--no-watch` and `--firefox` can be used in combination with the new `-all` flag.
1 parent 12edc0b commit 91dad03

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

scripts/run-component-tests.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* --local | If specified, no browser will be launched.
1515
* --firefox | Instead of Chrome being used for tests, Firefox will be used.
1616
* --no-watch | Watch mode is enabled by default. This flag opts-out to standard Bazel.
17+
* --all | Runs tests for all components in the project.
1718
*/
1819

1920
const minimist = require('minimist');
@@ -38,36 +39,51 @@ shelljs.set('-e');
3839
shelljs.cd(projectDir);
3940

4041
// Extracts the supported command line options.
41-
const {_: components, local, firefox, watch} = minimist(args, {
42-
boolean: ['local', 'firefox', 'watch'],
42+
const {_: components, local, firefox, watch, all} = minimist(args, {
43+
boolean: ['local', 'firefox', 'watch', 'all'],
4344
default: {watch: true},
4445
});
4546

46-
// Exit if no component has been specified.
47-
if (!components.length) {
48-
console.error(chalk.red(
49-
'No component specified. Specify a component name, or pass a ' +
50-
'path to the component directory.'));
51-
process.exit(1);
52-
}
53-
5447
// We can only run a single target with "--local". Running multiple targets within the
5548
// same Karma server is not possible since each test target runs isolated from the others.
56-
if (local && components.length > 1) {
49+
if (local && (components.length > 1 || all)) {
5750
console.error(chalk.red(
5851
'Unable to run multiple components tests in local mode. ' +
5952
'Only one component at a time can be run with "--local"'));
6053
process.exit(1);
6154
}
6255

63-
const bazelBinary = watch ? 'ibazel' : 'bazel';
64-
const bazelAction = local ? 'run' : 'test';
56+
const bazelBinary = `yarn -s ${watch ? 'ibazel' : 'bazel'}`;
6557
const testTargetName =
6658
`unit_tests_${local ? 'local' : firefox ? 'firefox-local' : 'chromium-local'}`;
59+
60+
// If the `--all` flag has been specified, we run tests for all components in the
61+
// repository. The `--firefox` flag can be still specified.
62+
if (all) {
63+
if (components.length) {
64+
console.error(
65+
chalk.red('Script cannot be run with `--all` if individual components are specified.'));
66+
process.exit(1);
67+
}
68+
shelljs.exec(
69+
`${bazelBinary} test //src/... --test_tag_filters=-e2e,-browser:${testTargetName} ` +
70+
`--build_tag_filters=-browser:${testTargetName} --build_tests_only`);
71+
return;
72+
}
73+
74+
// Exit if no component has been specified.
75+
if (!components.length) {
76+
console.error(chalk.red(
77+
'No component specified. Specify a component name, or pass a ' +
78+
'path to the component directory.'));
79+
process.exit(1);
80+
}
81+
82+
const bazelAction = local ? 'run' : 'test';
6783
const testLabels = components.map(t => `${getBazelPackageOfComponentName(t)}:${testTargetName}`);
6884

6985
// Runs Bazel for the determined test labels.
70-
shelljs.exec(`yarn -s ${bazelBinary} ${bazelAction} ${testLabels.join(' ')}`);
86+
shelljs.exec(`${bazelBinary} ${bazelAction} ${testLabels.join(' ')}`);
7187

7288
/**
7389
* Gets the Bazel package label for the specified component name. Throws if
@@ -89,8 +105,9 @@ function getBazelPackageOfComponentName(name) {
89105
return guessTargetName;
90106
}
91107
}
92-
throw Error(chalk.red(`Could not find test target for specified component: ` +
93-
`${chalk.yellow(name)}. Looked in packages: ${orderedGuessPackages.join(', ')}`));
108+
throw Error(chalk.red(
109+
`Could not find test target for specified component: ` +
110+
`${chalk.yellow(name)}. Looked in packages: ${orderedGuessPackages.join(', ')}`));
94111
}
95112

96113
/** Converts a path to a Bazel label. */

0 commit comments

Comments
 (0)