1/* eslint-env jest */
2import { ExecaError } from 'execa';
3import fs from 'fs/promises';
4import os from 'os';
5
6import { execute, getLoadedModulesAsync, projectRoot } from './utils';
7
8const originalForceColor = process.env.FORCE_COLOR;
9beforeAll(async () => {
10  await fs.mkdir(projectRoot, { recursive: true });
11  process.env.FORCE_COLOR = '0';
12});
13afterAll(() => {
14  process.env.FORCE_COLOR = originalForceColor;
15});
16
17it('loads expected modules by default', async () => {
18  const modules = await getLoadedModulesAsync(`require('../../build/src/whoami');`);
19  expect(modules).toStrictEqual([
20    '../node_modules/arg/index.js',
21    '../node_modules/chalk/node_modules/ansi-styles/index.js',
22    '../node_modules/chalk/source/index.js',
23    '../node_modules/chalk/source/util.js',
24    '../node_modules/has-flag/index.js',
25    '../node_modules/supports-color/index.js',
26    '@expo/cli/build/src/log.js',
27    '@expo/cli/build/src/utils/args.js',
28    '@expo/cli/build/src/utils/errors.js',
29    '@expo/cli/build/src/whoami/index.js',
30  ]);
31});
32
33it('runs `npx expo whoami --help`', async () => {
34  const results = await execute('whoami', '--help');
35  expect(results.stdout).toMatchInlineSnapshot(`
36    "
37      Info
38        Show the currently authenticated username
39
40      Usage
41        $ npx expo whoami
42
43      Options
44        -h, --help    Usage info
45    "
46  `);
47});
48
49it('throws on invalid project root', async () => {
50  expect.assertions(1);
51  try {
52    await execute('very---invalid', 'whoami');
53  } catch (e) {
54    const error = e as ExecaError;
55    expect(error.stderr).toMatch(/Invalid project root: \//);
56  }
57});
58
59it('runs `npx expo whoami`', async () => {
60  const results = await execute('whoami').catch((e) => e);
61
62  // Test logged in or logged out.
63  if (results.stderr) {
64    expect(results.stderr.trim()).toBe('Not logged in');
65  } else {
66    expect(results.stdout.trim()).toBeTruthy();
67    // Ensure this can always be used as a means of automation.
68    expect(results.stdout.trim().split(os.EOL).length).toBe(1);
69  }
70});
71
72if (process.env.CI) {
73  it('runs `npx expo whoami` and throws logged out error', async () => {
74    expect.assertions(1);
75    try {
76      console.log(await execute('whoami'));
77    } catch (e) {
78      const error = e as ExecaError;
79      expect(error.stderr).toMatch(/Not logged in/);
80    }
81  });
82}
83