1/* eslint-env jest */
2import JsonFile from '@expo/json-file';
3import execa from 'execa';
4import klawSync from 'klaw-sync';
5import path from 'path';
6
7import { runExportSideEffects } from './export-side-effects';
8import { bin, getRouterE2ERoot } from '../utils';
9
10runExportSideEffects();
11
12describe('exports with url-polyfill', () => {
13  const projectRoot = getRouterE2ERoot();
14  const outputName = 'dist-url-polyfill';
15  const outputDir = path.join(projectRoot, outputName);
16
17  beforeAll(
18    async () => {
19      await execa('node', [bin, 'export', '-p', 'ios', '--output-dir', outputName], {
20        cwd: projectRoot,
21        env: {
22          NODE_ENV: 'production',
23          EXPO_USE_STATIC: 'static',
24          E2E_ROUTER_SRC: 'url-polyfill',
25          E2E_ROUTER_ASYNC: 'development',
26        },
27      });
28    },
29    // Could take 45s depending on how fast the bundler resolves
30    560 * 1000
31  );
32
33  it('has expected files', async () => {
34    // List output files with sizes for snapshotting.
35    // This is to make sure that any changes to the output are intentional.
36    // Posix path formatting is used to make paths the same across OSes.
37    const files = klawSync(outputDir)
38      .map((entry) => {
39        if (entry.path.includes('node_modules') || !entry.stats.isFile()) {
40          return null;
41        }
42        return path.posix.relative(outputDir, entry.path);
43      })
44      .filter(Boolean);
45
46    const metadata = await JsonFile.readAsync(path.resolve(outputDir, 'metadata.json'));
47
48    expect(metadata).toEqual({
49      bundler: 'metro',
50      fileMetadata: {
51        ios: {
52          assets: expect.anything(),
53          bundle: expect.stringMatching(/bundles\/ios-.*\.js/),
54        },
55      },
56      version: 0,
57    });
58
59    // The wrapper should not be included as a route.
60    expect(files).not.toContain('+html.html');
61    expect(files).not.toContain('index.html');
62
63    const iosBundle = files.find((v) => v?.startsWith('bundles/ios'));
64    expect(iosBundle).toBeDefined();
65  });
66});
67