Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Ensure --host option changes the network host #13521

Merged
merged 1 commit into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/core/src/server/dev-server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import ip from 'ip';
import { getServerAddresses } from './dev-server';

jest.mock('ip');
const mockedIp = ip as jest.Mocked<typeof ip>;

describe('getServerAddresses', () => {
beforeEach(() => {
mockedIp.address.mockReturnValue('192.168.0.5');
});

it('builds addresses with a specified host', () => {
const { address, networkAddress } = getServerAddresses(9009, '192.168.89.89', 'http');
expect(address).toEqual('http://localhost:9009/');
expect(networkAddress).toEqual('http://192.168.89.89:9009/');
});

it('builds addresses with local IP when host is not specified', () => {
const { address, networkAddress } = getServerAddresses(9009, '', 'http');
expect(address).toEqual('http://localhost:9009/');
expect(networkAddress).toEqual('http://192.168.0.5:9009/');
});
});
12 changes: 9 additions & 3 deletions lib/core/src/server/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ const startPreview = async ({
return { previewStats, previewTotalTime: process.hrtime(startTime) };
};

export function getServerAddresses(port: number, host: string, proto: string) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this logic to its own function is useful for unit tests

return {
address: `${proto}://localhost:${port}/`,
networkAddress: `${proto}://${host || ip.address()}:${port}/`,
};
}

export async function storybookDevServer(options: any) {
const app = express();
const server = await getServer(app, options);
Expand Down Expand Up @@ -385,8 +392,7 @@ export async function storybookDevServer(options: any) {

const { port, host } = options;
const proto = options.https ? 'https' : 'http';
const address = `${proto}://${host || 'localhost'}:${port}/`;
const networkAddress = `${proto}://${ip.address()}:${port}/`;
const { address, networkAddress } = getServerAddresses(port, host, proto);

await new Promise((resolve, reject) => {
// FIXME: Following line doesn't match TypeScript signature at all 🤔
Expand Down Expand Up @@ -414,7 +420,7 @@ export async function storybookDevServer(options: any) {
]);

// TODO #13083 Remove this when compiling the preview is fast enough
if (!options.ci) openInBrowser(address);
if (!options.ci) openInBrowser(networkAddress);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yannbf - I think it's the case now that we'll want the browser to open to networkAddress, given that's what the user is configuring with the --host option


return { ...previewResult, ...managerResult, address, networkAddress };
}