mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
fix: live reload port fallback if port is used (#899)
* Move start reload server into separate module * Find an unused port when starting the live reload server * Move findUnusedPort into module * Add tests for findUnusedPort module * Refactor findUnusedPort * Move starting of servers into separate module and add tests * Remove unused constants.js * Zap extra line breaks * Add tests for liveReloadServer * Rename serverController to start * Move start into lib/server * Add portfinder package * Replace findUnusedPort with portfinder * nits
This commit is contained in:
parent
c4740f7af2
commit
bbef20d345
10 changed files with 291 additions and 84 deletions
138
lib/server/__tests__/start.test.js
Normal file
138
lib/server/__tests__/start.test.js
Normal file
|
@ -0,0 +1,138 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const program = require('commander');
|
||||
const openBrowser = require('react-dev-utils/openBrowser');
|
||||
const portFinder = require('portfinder');
|
||||
const liveReloadServer = require('../liveReloadServer.js');
|
||||
const server = require('../server.js');
|
||||
|
||||
const siteConfig = require(`${process.cwd()}/siteConfig.js`);
|
||||
|
||||
// When running Jest the siteConfig import fails because siteConfig doesn't exist
|
||||
// relative to the cwd of the tests. Rather than mocking out cwd just mock
|
||||
// siteConfig virtually.
|
||||
jest.mock(`${process.cwd()}/siteConfig.js`, () => jest.fn(), {virtual: true});
|
||||
|
||||
jest.mock('commander');
|
||||
jest.mock('react-dev-utils/openBrowser');
|
||||
jest.mock('portfinder');
|
||||
jest.mock('../liveReloadServer.js');
|
||||
jest.mock('../server.js');
|
||||
jest.mock('process');
|
||||
|
||||
console.log = jest.fn();
|
||||
|
||||
const start = require('../start.js');
|
||||
|
||||
beforeEach(() => jest.resetAllMocks());
|
||||
|
||||
describe('start live reload', () => {
|
||||
test('uses inital port 35729', () => {
|
||||
portFinder.getPortPromise.mockResolvedValue();
|
||||
start.startLiveReloadServer();
|
||||
expect(portFinder.getPortPromise).toHaveBeenCalledWith({port: 35729});
|
||||
});
|
||||
|
||||
test('when an unused port is found, starts the live reload server on that port', () => {
|
||||
expect.assertions(1);
|
||||
const unusedPort = 1234;
|
||||
portFinder.getPortPromise.mockResolvedValue(unusedPort);
|
||||
return start.startLiveReloadServer().then(() => {
|
||||
expect(liveReloadServer.start).toHaveBeenCalledWith(unusedPort);
|
||||
});
|
||||
});
|
||||
|
||||
test('when no unused port found, returns error', () => {
|
||||
expect.assertions(1);
|
||||
const unusedPortError = new Error('no unused port');
|
||||
portFinder.getPortPromise.mockRejectedValue(unusedPortError);
|
||||
return expect(start.startLiveReloadServer()).rejects.toEqual(
|
||||
unusedPortError
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('start server', () => {
|
||||
test('when custom port provided as parameter, uses as inital port', () => {
|
||||
const customPort = 1234;
|
||||
program.port = customPort;
|
||||
portFinder.getPortPromise.mockResolvedValue();
|
||||
start.startServer();
|
||||
expect(portFinder.getPortPromise).toBeCalledWith({port: customPort});
|
||||
delete program.port;
|
||||
});
|
||||
|
||||
test('when port environment variable set and no custom port, used as inital port', () => {
|
||||
const customPort = '4321';
|
||||
process.env.PORT = customPort;
|
||||
portFinder.getPortPromise.mockResolvedValue();
|
||||
start.startServer();
|
||||
expect(portFinder.getPortPromise).toBeCalledWith({port: customPort});
|
||||
delete process.env.PORT;
|
||||
});
|
||||
|
||||
test('when no custom port specified, uses port 3000', () => {
|
||||
portFinder.getPortPromise.mockResolvedValue();
|
||||
start.startServer();
|
||||
expect(portFinder.getPortPromise).toBeCalledWith({port: 3000});
|
||||
});
|
||||
|
||||
test('when unused port found, starts server on that port', () => {
|
||||
expect.assertions(1);
|
||||
const port = 1357;
|
||||
portFinder.getPortPromise.mockResolvedValue(port);
|
||||
return start.startServer().then(() => {
|
||||
expect(server).toHaveBeenCalledWith(port);
|
||||
});
|
||||
});
|
||||
|
||||
test('when unused port found, opens browser to server address', () => {
|
||||
expect.assertions(1);
|
||||
const baseUrl = '/base_url';
|
||||
siteConfig.baseUrl = baseUrl;
|
||||
const port = 2468;
|
||||
portFinder.getPortPromise.mockResolvedValue(port);
|
||||
const expectedServerAddress = `http://localhost:${port}${baseUrl}`;
|
||||
return start.startServer().then(() => {
|
||||
expect(openBrowser).toHaveBeenCalledWith(expectedServerAddress);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('start docusaurus', () => {
|
||||
test('when watch enabled, starts live reload server', () => {
|
||||
expect.assertions(1);
|
||||
program.watch = true;
|
||||
portFinder.getPortPromise.mockResolvedValue();
|
||||
return start.startDocusaurus().then(() => {
|
||||
expect(liveReloadServer.start).toBeCalled();
|
||||
});
|
||||
});
|
||||
|
||||
test('when live reload fails to start, server still started', () => {
|
||||
expect.assertions(1);
|
||||
program.watch = true;
|
||||
console.warn = jest.fn();
|
||||
portFinder.getPortPromise
|
||||
.mockRejectedValueOnce('could not find live reload port')
|
||||
.mockResolvedValueOnce();
|
||||
return start.startDocusaurus().then(() => {
|
||||
expect(server).toBeCalled();
|
||||
});
|
||||
});
|
||||
|
||||
test('live reload disabled, only starts docusarus server', () => {
|
||||
expect.assertions(2);
|
||||
program.watch = false;
|
||||
portFinder.getPortPromise.mockResolvedValue();
|
||||
return start.startDocusaurus().then(() => {
|
||||
expect(liveReloadServer.start).not.toBeCalled();
|
||||
expect(server).toBeCalled();
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue