1import { vol } from 'memfs';
2
3import { resolvePortAsync } from '../../utils/port';
4import { resolveBundlerPropsAsync } from '../resolveBundlerProps';
5
6const asMock = <T extends (...args: any[]) => any>(fn: T): jest.MockedFunction<T> =>
7  fn as jest.MockedFunction<T>;
8
9jest.mock('../../utils/port');
10
11describe(resolveBundlerPropsAsync, () => {
12  afterEach(() => vol.reset());
13
14  it(`asserts bad args`, async () => {
15    await expect(
16      resolveBundlerPropsAsync('/', { bundler: false, port: 3000 })
17    ).rejects.toThrowError(/mutually exclusive arguments/);
18  });
19  it(`skips bundling if the port is busy`, async () => {
20    asMock(resolvePortAsync).mockResolvedValueOnce(null);
21
22    expect(await resolveBundlerPropsAsync('/', {})).toEqual({
23      port: 8081,
24      shouldStartBundler: false,
25    });
26  });
27  it(`resolves headless port`, async () => {
28    expect(
29      await resolveBundlerPropsAsync('/', {
30        port: 3000,
31      })
32    ).toEqual({
33      port: 3000,
34      shouldStartBundler: true,
35    });
36  });
37  it(`resolves default port`, async () => {
38    asMock(resolvePortAsync).mockResolvedValueOnce(19006);
39
40    expect(
41      await resolveBundlerPropsAsync('/', {
42        bundler: true,
43      })
44    ).toEqual({
45      port: 19006,
46      shouldStartBundler: true,
47    });
48  });
49});
50