From 37ff876f2f7e33fa6cc9c4e03713486a1092784c Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 16 Jul 2022 14:06:34 -0400 Subject: [PATCH] refactor: export internals --- src/ServerlessOffline.js | 43 ++++++++++++++----- .../manual/websocket/main/test/e2e/ws.e2e.js | 6 +-- tests/old-unit/support/OfflineBuilder.js | 16 ++++--- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/ServerlessOffline.js b/src/ServerlessOffline.js index 47567e732..0e15f254d 100644 --- a/src/ServerlessOffline.js +++ b/src/ServerlessOffline.js @@ -62,19 +62,19 @@ export default class ServerlessOffline { // Put here so available everywhere, not just in handlers env.IS_OFFLINE = true - this._mergeOptions() + this.#mergeOptions() const { httpEvents, lambdas, scheduleEvents, webSocketEvents } = - this._getEvents() + this.#getEvents() // if (lambdas.length > 0) { - await this._createLambda(lambdas) + await this.#createLambda(lambdas) // } const eventModules = [] if (httpEvents.length > 0) { - eventModules.push(this._createHttp(httpEvents)) + eventModules.push(this.#createHttp(httpEvents)) } if (!this.#options.disableScheduledEvents && scheduleEvents.length > 0) { @@ -154,7 +154,7 @@ export default class ServerlessOffline { log.info(`Got ${command} signal. Offline Halting...`) } - async _createLambda(lambdas, skipStart) { + async #createLambda(lambdas, skipStart) { const { default: Lambda } = await import('./lambda/index.js') this.#lambda = new Lambda(this.#serverless, this.#options) @@ -166,7 +166,7 @@ export default class ServerlessOffline { } } - async _createHttp(events, skipStart) { + async #createHttp(events, skipStart) { const { default: Http } = await import('./events/http/index.js') this.#http = new Http(this.#serverless, this.#options, this.#lambda) @@ -213,7 +213,7 @@ export default class ServerlessOffline { return this.#webSocket.start() } - _mergeOptions() { + #mergeOptions() { const { service: { custom = {}, provider }, } = this.#serverless @@ -260,7 +260,7 @@ export default class ServerlessOffline { log.debug('options:', this.#options) } - _getEvents() { + #getEvents() { const { service } = this.#serverless const httpEvents = [] @@ -384,8 +384,29 @@ export default class ServerlessOffline { } } - // TEMP FIXME quick fix to expose gateway server for testing, look for better solution - getApiGatewayServer() { - return this.#http.getServer() + // TODO FIXME + // TEMP quick fix to expose for testing, look for better solution + internals() { + return { + createHttp: (events, skipStart) => { + return this.#createHttp(events, skipStart) + }, + + createLambda: (lambdas, skipStart) => { + return this.#createLambda(lambdas, skipStart) + }, + + getApiGatewayServer: () => { + return this.#http.getServer() + }, + + getEvents: () => { + return this.#getEvents() + }, + + mergeOptions: () => { + this.#mergeOptions() + }, + } } } diff --git a/tests/manual/websocket/main/test/e2e/ws.e2e.js b/tests/manual/websocket/main/test/e2e/ws.e2e.js index c1be2d4b6..e17c92d78 100644 --- a/tests/manual/websocket/main/test/e2e/ws.e2e.js +++ b/tests/manual/websocket/main/test/e2e/ws.e2e.js @@ -92,18 +92,18 @@ describe('serverless', () => { }) it('should request to upgade to WebSocket when receving an HTTP request', async () => { - const _req = chai + const request = chai .request( `${endpoint .replace('ws://', 'http://') .replace('wss://', 'https://')}`, ) .keepOpen() - let res = await _req.get(`/${Date.now()}`) // .set('Authorization', user.accessToken); + let res = await request.get(`/${Date.now()}`) // .set('Authorization', user.accessToken); expect(res).to.have.status(426) - res = await _req.get(`/${Date.now()}/${Date.now()}`) // .set('Authorization', user.accessToken); + res = await request.get(`/${Date.now()}/${Date.now()}`) // .set('Authorization', user.accessToken); expect(res).to.have.status(426) }).timeout(timeout) diff --git a/tests/old-unit/support/OfflineBuilder.js b/tests/old-unit/support/OfflineBuilder.js index 4214556ae..a7098b0b1 100644 --- a/tests/old-unit/support/OfflineBuilder.js +++ b/tests/old-unit/support/OfflineBuilder.js @@ -36,9 +36,9 @@ export default class OfflineBuilder { functionConfig.handler, ) - const _handlerPath = join('.', handlerPath) + const handlerpath = join('.', handlerPath) - this.#handlers[_handlerPath] = { + this.#handlers[handlerpath] = { [handlerName]: handler, } @@ -51,13 +51,15 @@ export default class OfflineBuilder { this.#options, ) - this.#serverlessOffline._mergeOptions() + this.#serverlessOffline.internals().mergeOptions() - const { httpEvents, lambdas } = this.#serverlessOffline._getEvents() - await this.#serverlessOffline._createLambda(lambdas, true) - await this.#serverlessOffline._createHttp(httpEvents, true) + const { httpEvents, lambdas } = this.#serverlessOffline + .internals() + .getEvents() + await this.#serverlessOffline.internals().createLambda(lambdas, true) + await this.#serverlessOffline.internals().createHttp(httpEvents, true) - return this.#serverlessOffline.getApiGatewayServer() + return this.#serverlessOffline.internals().getApiGatewayServer() } end(skipExit) {