1// Tests that require physical fixtures due to their complex nature.
2import { join, resolve } from 'path';
3
4import { getConfig } 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
45    const { exp } = getConfig(projectRoot, {
46      skipSDKVersionRequirement: true,
47    });
48    // @ts-ignore: foo property is not defined
49    expect(exp.foo).toBe('bar');
50  });
51
52  it('throws a useful error for a project with an external syntax error', () => {
53    const projectRoot = resolve(__dirname, './fixtures/external-error');
54
55    expect(() =>
56      getConfig(projectRoot, {
57        skipSDKVersionRequirement: true,
58      })
59    ).toThrow(/Error reading Expo config.*?app.config.js/);
60  });
61
62  it('resolves plugins', () => {
63    const projectRoot = resolve(__dirname, './fixtures/plugins');
64    const { exp } = getConfig(projectRoot, {
65      skipSDKVersionRequirement: true,
66    });
67
68    expect(exp.name).toBe('custom-name');
69    expect(exp.slug).toBe('from-custom-plugin');
70    expect(exp.plugins?.[0]).toStrictEqual('./my-plugin');
71    // Ensure the plugin method is serialized into its original name
72    expect(exp.plugins?.[1][0]).toBe('withCustom');
73  });
74});
75