xref: /expo/tools/src/commands/NativeUnitTests.ts (revision 782a85d5)
1import chalk from 'chalk';
2import inquirer from 'inquirer';
3
4import { androidNativeUnitTests } from './AndroidNativeUnitTests';
5import { iosNativeUnitTests } from './IosNativeUnitTests';
6
7type PlatformName = 'android' | 'ios' | 'both';
8type TestType = 'local' | 'instrumented';
9
10async function thisAction({
11  platform,
12  type = 'local',
13  packages,
14}: {
15  platform?: PlatformName;
16  type: TestType;
17  packages?: string;
18}) {
19  if (!platform) {
20    console.log(chalk.yellow("You haven't specified platform to run unit tests for!"));
21    const result = await inquirer.prompt<{ platform: PlatformName }>([
22      {
23        name: 'platform',
24        type: 'list',
25        message: 'Which platform do you want to run native tests ?',
26        choices: ['android', 'ios', 'both'],
27        default: 'android',
28      },
29    ]);
30    platform = result.platform;
31  }
32  const runAndroid = platform === 'android' || platform === 'both';
33  const runIos = platform === 'ios' || platform === 'both';
34  if (runIos) {
35    await iosNativeUnitTests({ packages });
36  }
37  if (runAndroid) {
38    await androidNativeUnitTests({ type, packages });
39  }
40}
41
42export default (program: any) => {
43  program
44    .command('native-unit-tests')
45    .option(
46      '-p, --platform <string>',
47      'Determine for which platform we should run native tests: android, ios or both'
48    )
49    .option(
50      '-t, --type <string>',
51      'Type of unit test to run, if supported by this platform. local (default) or instrumented'
52    )
53    .option(
54      '--packages <string>',
55      '[optional] Comma-separated list of package names to run unit tests for. Defaults to all packages with unit tests.'
56    )
57    .description('Runs native unit tests for each unimodules that provides them.')
58    .asyncAction(thisAction);
59};
60