1/* eslint-env jest */
2import { ExecaError } from 'execa';
3import fs from 'fs/promises';
4import path from 'path';
5
6import { execute, projectRoot, getRoot, getLoadedModulesAsync } from './utils';
7
8const originalForceColor = process.env.FORCE_COLOR;
9
10beforeAll(async () => {
11  await fs.mkdir(projectRoot, { recursive: true });
12  process.env.FORCE_COLOR = '0';
13});
14
15afterAll(() => {
16  process.env.FORCE_COLOR = originalForceColor;
17});
18
19it('loads expected modules by default', async () => {
20  const modules = await getLoadedModulesAsync(`require('../../build/src/config').expoConfig`);
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/config/index.js',
29    '@expo/cli/build/src/log.js',
30    '@expo/cli/build/src/utils/args.js',
31  ]);
32});
33
34it('runs `npx expo config --help`', async () => {
35  const results = await execute('config', '--help');
36  expect(results.stdout).toMatchInlineSnapshot(`
37    "
38      Info
39        Show the project config
40
41      Usage
42        $ npx expo config <dir>
43
44      Options
45        <dir>                                    Directory of the Expo project. Default: Current working directory
46        --full                                   Include all project config data
47        --json                                   Output in JSON format
48        -t, --type <public|prebuild|introspect>  Type of config to show
49        -h, --help                               Usage info
50    "
51  `);
52});
53
54it(
55  'runs `npx expo config --json`',
56  async () => {
57    const projectName = 'basic-config';
58    const projectRoot = getRoot(projectName);
59    // Create the project root aot
60    await fs.mkdir(projectRoot, { recursive: true });
61    // Create a fake package.json -- this is a terminal file that cannot be overwritten.
62    await fs.writeFile(path.join(projectRoot, 'package.json'), '{ "version": "1.0.0" }');
63    await fs.writeFile(path.join(projectRoot, 'app.json'), '{ "expo": { "name": "foobar" } }');
64    // Add an environment variable file to test that it's not included in the config.
65    await fs.writeFile(path.join(projectRoot, '.env'), 'FOOBAR=1');
66
67    const results = await execute('config', projectName, '--json');
68    // @ts-ignore
69    const exp = JSON.parse(results.stdout);
70
71    expect(exp.name).toEqual('foobar');
72    expect(exp.slug).toEqual('foobar');
73    expect(exp.platforms).toStrictEqual([]);
74    expect(exp.version).toBe('1.0.0');
75    expect(exp._internal.dynamicConfigPath).toBe(null);
76    expect(exp._internal.staticConfigPath).toMatch(/\/basic-config\/app\.json$/);
77  }, // Could take 45s depending on how fast npm installs
78  120 * 1000
79);
80
81it('throws on invalid project root', async () => {
82  expect.assertions(1);
83  try {
84    await execute('config', 'very---invalid', '--json');
85  } catch (e) {
86    const error = e as ExecaError;
87    expect(error.stderr).toMatch(/Invalid project root: \//);
88  }
89});
90