1/* eslint-env jest */
2import { ExecaError } from 'execa';
3import fs from 'fs/promises';
4
5import { execute, getLoadedModulesAsync, projectRoot } from './utils';
6
7const originalForceColor = process.env.FORCE_COLOR;
8const originalCI = process.env.CI;
9beforeAll(async () => {
10  await fs.mkdir(projectRoot, { recursive: true });
11  process.env.FORCE_COLOR = '0';
12  process.env.CI = '1';
13});
14afterAll(() => {
15  process.env.FORCE_COLOR = originalForceColor;
16  process.env.CI = originalCI;
17});
18
19it('loads expected modules by default', async () => {
20  const modules = await getLoadedModulesAsync(`require('../../build/src/login');`);
21  expect(modules).toStrictEqual([
22    '../node_modules/ansi-styles/index.js',
23    '../node_modules/arg/index.js',
24    '../node_modules/chalk/source/index.js',
25    '../node_modules/chalk/source/util.js',
26    '../node_modules/has-flag/index.js',
27    '../node_modules/supports-color/index.js',
28    '@expo/cli/build/src/log.js',
29    '@expo/cli/build/src/login/index.js',
30    '@expo/cli/build/src/utils/args.js',
31    '@expo/cli/build/src/utils/errors.js',
32  ]);
33});
34
35it('runs `npx expo login --help`', async () => {
36  const results = await execute('login', '--help');
37  expect(results.stdout).toMatchInlineSnapshot(`
38    "
39      Info
40        Log in to an Expo account
41
42      Usage
43        $ npx expo login
44
45      Options
46        -u, --username <string>  Username
47        -p, --password <string>  Password
48        --otp <string>           One-time password from your 2FA device
49        -h, --help               Usage info
50    "
51  `);
52});
53
54it('throws on invalid project root', async () => {
55  expect.assertions(1);
56  try {
57    await execute('very---invalid', 'login');
58  } catch (e) {
59    const error = e as ExecaError;
60    expect(error.stderr).toMatch(/Invalid project root: \//);
61  }
62});
63
64it('runs `npx expo login` and throws due to CI', async () => {
65  expect.assertions(2);
66  try {
67    console.log(await execute('login'));
68  } catch (e) {
69    const error = e as ExecaError;
70    expect(error.stderr).toMatch(/Input is required/);
71    expect(error.stderr).toMatch(/Use the EXPO_TOKEN environment variable to authenticate in CI/);
72  }
73});
74
75it('runs `npx expo login` and throws due to invalid credentials', async () => {
76  expect.assertions(1);
77  try {
78    console.log(await execute('login', '--username', 'bacon', '--password', 'invalid'));
79  } catch (e) {
80    const error = e as ExecaError;
81    expect(error.stderr).toMatch(/Your username, email, or password was incorrect/);
82  }
83});
84