diff --git a/src/core/server/root/index.test.ts b/src/core/server/root/index.test.ts index 4eba2133dce285..2a31c32f3a42cf 100644 --- a/src/core/server/root/index.test.ts +++ b/src/core/server/root/index.test.ts @@ -195,3 +195,19 @@ test('stops services if consequent logger upgrade fails', async () => { expect(mockConsoleError.mock.calls).toMatchSnapshot(); }); + +test(`doesn't create server if config validation fails`, async () => { + configService.validateAll.mockImplementation(() => { + throw new Error('invalid config'); + }); + const ServerMock = jest.fn(); + jest.doMock('../server', () => ({ + Server: ServerMock, + })); + const onShutdown = jest.fn(); + const root = new Root(config$, env, onShutdown); + + await expect(root.setup()).rejects.toThrowErrorMatchingInlineSnapshot(`"invalid config"`); + expect(ServerMock).not.toHaveBeenCalled(); + expect(onShutdown).toHaveBeenCalledTimes(1); +}); diff --git a/src/core/server/root/index.ts b/src/core/server/root/index.ts index c8298ff2b7fcc5..19f6bf71202246 100644 --- a/src/core/server/root/index.ts +++ b/src/core/server/root/index.ts @@ -57,18 +57,18 @@ export class Root { } public async setup() { - const newPlatformPluginDefinitions = await this.discoverPlugins(); - const schemas = this.getSchemas(newPlatformPluginDefinitions); + try { + const newPlatformPluginDefinitions = await this.discoverPlugins(); + const schemas = this.getSchemas(newPlatformPluginDefinitions); - this.configService = new ConfigService(this.config$, this.env, this.logger, schemas); + this.configService = new ConfigService(this.config$, this.env, this.logger, schemas); - await this.configService.validateAll(); + await this.configService.validateAll(); - this.server = new Server(this.configService, this.logger, this.env); + this.server = new Server(this.configService, this.logger, this.env); - this.log.debug('setting up root'); + this.log.debug('setting up root'); - try { await this.setupLogging(); await this.server.setup(newPlatformPluginDefinitions); } catch (e) {