1/* eslint-env jest */
2import fs from 'fs/promises';
3import path from 'path';
4
5import { execute, projectRoot, getRoot, getLoadedModulesAsync } 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/config').expoConfig`);
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/config/index.js',
26    '@expo/cli/build/src/log.js',
27    '@expo/cli/build/src/utils/args.js',
28  ]);
29});
30
31it('runs `npx expo config --help`', async () => {
32  const results = await execute('config', '--help');
33  expect(results.stdout).toMatchInlineSnapshot(`
34    "
35          Description
36            Show the project config
37
38          Usage
39            $ npx expo config <dir>
40
41          <dir> is the directory of the Expo project.
42          Defaults to the current working directory.
43
44          Options
45          --full                                   Include all project config data
46          --json                                   Output in JSON format
47          -t, --type <public|prebuild|introspect>  Type of config to show
48          -h, --help                               Output usage information
49        "
50  `);
51});
52
53it('runs `npx expo config --json`', async () => {
54  const projectName = 'basic-config';
55  const projectRoot = getRoot(projectName);
56  // Create the project root aot
57  await fs.mkdir(projectRoot, { recursive: true });
58  // Create a fake package.json -- this is a terminal file that cannot be overwritten.
59  await fs.writeFile(path.join(projectRoot, 'package.json'), '{ "version": "1.0.0" }');
60  await fs.writeFile(path.join(projectRoot, 'app.json'), '{ "expo": { "name": "foobar" } }');
61
62  const results = await execute('config', projectName, '--json');
63  // @ts-ignore
64  const exp = JSON.parse(results.stdout);
65
66  expect(exp.name).toEqual('foobar');
67  expect(exp.slug).toEqual('foobar');
68  expect(exp.platforms).toStrictEqual([]);
69  expect(exp.version).toBe('1.0.0');
70  expect(exp._internal.dynamicConfigPath).toBe(null);
71  expect(exp._internal.staticConfigPath).toMatch(/\/basic-config\/app\.json$/);
72});
73
74it('throws on invalid project root', async () => {
75  expect.assertions(1);
76  try {
77    await execute('config', 'very---invalid', '--json');
78  } catch (e) {
79    expect(e.stderr).toMatch(/Invalid project root: \//);
80  }
81});
82