1// Tests that require physical fixtures due to their complex nature.
2import { join, resolve } from 'path';
3
4import { getConfig, setCustomConfigPath } from '../Config';
5import { getDynamicConfig } from '../getConfig';
6
7const mockConfigContext = {} as any;
8
9jest.unmock('resolve-from');
10
11describe(getDynamicConfig, () => {
12  describe('process.cwd in a child process', () => {
13    const originalCwd = process.cwd();
14    const projectRoot = join(__dirname, 'fixtures/dynamic-cwd');
15
16    beforeEach(() => {
17      process.chdir(__dirname);
18    });
19
20    afterEach(() => {
21      process.chdir(originalCwd);
22    });
23
24    // Test that hot evaluation is spawned in the expected location
25    // https://github.com/expo/expo-cli/pull/2220
26    it('process.cwd in read-config script is not equal to the project root', () => {
27      const configPath = join(projectRoot, 'app.config.ts');
28
29      const { config } = getDynamicConfig(configPath, mockConfigContext);
30
31      expect(config.extra.processCwd).toBe(__dirname);
32      expect(
33        getDynamicConfig(configPath, {
34          projectRoot,
35        } as any).config.extra.processCwd
36      ).toBe(__dirname);
37    });
38  });
39});
40
41describe(getConfig, () => {
42  it('parses a js config with import', () => {
43    const projectRoot = resolve(__dirname, './fixtures/require-file');
44    const configPath = resolve(projectRoot, 'with-import_app.config.js');
45
46    setCustomConfigPath(projectRoot, configPath);
47    const { exp } = getConfig(projectRoot, {
48      skipSDKVersionRequirement: true,
49    });
50    // @ts-ignore: foo property is not defined
51    expect(exp.foo).toBe('bar');
52  });
53
54  it('throws a useful error for a project with an external syntax error', () => {
55    const projectRoot = resolve(__dirname, './fixtures/external-error');
56    const configPath = resolve(projectRoot, 'app.config.js');
57
58    setCustomConfigPath(projectRoot, configPath);
59    expect(() =>
60      getConfig(projectRoot, {
61        skipSDKVersionRequirement: true,
62      })
63    ).toThrow(/Error reading Expo config.*?app.config.js/);
64  });
65
66  it('resolves plugins', () => {
67    const projectRoot = resolve(__dirname, './fixtures/plugins');
68    const { exp } = getConfig(projectRoot, {
69      skipSDKVersionRequirement: true,
70    });
71
72    expect(exp.name).toBe('custom-name');
73    expect(exp.slug).toBe('from-custom-plugin');
74    expect(exp.plugins[0]).toStrictEqual('./my-plugin');
75    // Ensure the plugin method is serialized into its original name
76    expect(exp.plugins[1][0]).toBe('withCustom');
77  });
78});
79