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