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