diff --git a/doc-gen-service/package-lock.json b/doc-gen-service/package-lock.json index 921a9f7a8..41f2ac68b 100644 --- a/doc-gen-service/package-lock.json +++ b/doc-gen-service/package-lock.json @@ -52,7 +52,8 @@ "ts-jest": "^29.1.1", "ts-loader": "^9.5.1", "ts-node": "^10.4.0", - "typescript": "^5.3.3" + "typescript": "^5.3.3", + "wait-for-expect": "^3.0.2" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -9479,6 +9480,12 @@ "node": ">= 0.8" } }, + "node_modules/wait-for-expect": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/wait-for-expect/-/wait-for-expect-3.0.2.tgz", + "integrity": "sha512-cfS1+DZxuav1aBYbaO/kE06EOS8yRw7qOFoD3XtjTkYvCvh3zUvNST8DXK/nPaeqIzIv3P3kL3lRJn8iwOiSag==", + "dev": true + }, "node_modules/walker": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz", diff --git a/doc-gen-service/package.json b/doc-gen-service/package.json index eb6df368b..9552992d7 100644 --- a/doc-gen-service/package.json +++ b/doc-gen-service/package.json @@ -56,7 +56,8 @@ "ts-jest": "^29.1.1", "ts-loader": "^9.5.1", "ts-node": "^10.4.0", - "typescript": "^5.3.3" + "typescript": "^5.3.3", + "wait-for-expect": "^3.0.2" }, "jest": { "moduleFileExtensions": [ diff --git a/doc-gen-service/src/server.spec.ts b/doc-gen-service/src/server.spec.ts index f01c3fb81..6348b483e 100644 --- a/doc-gen-service/src/server.spec.ts +++ b/doc-gen-service/src/server.spec.ts @@ -1,5 +1,6 @@ import { Application } from 'express'; import EventEmitter from 'node:events'; +import WaitFor from 'wait-for-expect'; jest.mock('./app', () => ({ app: { @@ -41,9 +42,9 @@ const mock_initBrowser = jest.fn(); const mock_browserDisconnect = jest.fn(); jest.mock('./v1/services/puppeteer-service', () => ({ initBrowser: () => mock_initBrowser(), - browser: { - disconnect: () => mock_browserDisconnect(), - }, + getBrowser: jest.fn().mockResolvedValue({ + disconnect: mock_browserDisconnect, + }), })); const mock_loggerInfo = jest.fn(); @@ -59,11 +60,6 @@ jest.mock('./logger', () => ({ let configMap: any; const mock_getConfig = jest.fn(); -// jest.mock('./config', () => ({ -// config: { -// get: jest.fn((key) => mock_getConfig(key)), -// }, -// })); jest.mock('./config', () => ({ config: { get: (key) => { @@ -74,7 +70,7 @@ jest.mock('./config', () => ({ describe('server', () => { beforeEach(() => { jest.resetModules(); - }) + }); describe('Start server on specified environment', () => { describe('local', () => { @@ -139,34 +135,34 @@ describe('server', () => { configMap = { 'server:apiKey': 'api-key', 'server:port': 3000, - 'environment': 'prod', + environment: 'prod', }; }); it('should start listening on the port and use puppeteer', async () => { - mock_initBrowser.mockImplementation(() => Promise.resolve()); - mock_browserDisconnect.mockImplementation(() => Promise.resolve()); + mock_initBrowser.mockResolvedValue(0); + mock_browserDisconnect.mockResolvedValue(0); mock_listen.mockImplementation((port) => { expect(port).toBe(3000); }); await require('./server'); expect(mock_initBrowser).toHaveBeenCalled(); - expect(mock_browserDisconnect).toHaveBeenCalled(); + await WaitFor(() => { + expect(mock_browserDisconnect).toHaveBeenCalled(); + }); }); it('should handle initBrowser catch', async () => { - mock_initBrowser.mockImplementation(() => Promise.reject()); - mock_browserDisconnect.mockImplementation(() => Promise.resolve()); - jest.spyOn(process, 'exit').mockImplementation() + mock_initBrowser.mockResolvedValue(0); + mock_browserDisconnect.mockResolvedValue(0); + jest.spyOn(process, 'exit').mockImplementation(); await require('./server'); expect(mock_initBrowser).toHaveBeenCalled(); }); it('should handle browser disconnect catch', async () => { - mock_initBrowser.mockImplementation(() => Promise.resolve()); - mock_browserDisconnect.mockImplementation(() => Promise.reject()); - jest.spyOn(process, 'exit').mockImplementation() + mock_initBrowser.mockResolvedValue(0); + mock_browserDisconnect.mockResolvedValue(0); + jest.spyOn(process, 'exit').mockImplementation(); await require('./server'); }); }); }); - - }); diff --git a/doc-gen-service/src/server.ts b/doc-gen-service/src/server.ts index 8c7bc951c..96eeaee16 100644 --- a/doc-gen-service/src/server.ts +++ b/doc-gen-service/src/server.ts @@ -2,7 +2,7 @@ import http from 'http'; import { config } from './config/index'; import { logger } from './logger'; import { app } from './app'; -import { browser, initBrowser } from './v1/services/puppeteer-service'; +import { getBrowser, initBrowser } from './v1/services/puppeteer-service'; // run inside `async` function const port = config.get('server:port'); @@ -18,15 +18,22 @@ if (env === 'local') { logger.info(`Running in ${env} environment`); initBrowser() .then(() => { - logger.info('Browser initialized') - browser - .disconnect() - .then(() => { - logger.info('Browser disconnected') - app.set('port', port); - server.listen(port); - server.on('error', onError); - server.on('listening', onListening); + logger.info('Browser initialized'); + getBrowser() + .then((browser) => { + browser + .disconnect() + .then(() => { + logger.info('Browser disconnected'); + app.set('port', port); + server.listen(port); + server.on('error', onError); + server.on('listening', onListening); + }) + .catch((e) => { + logger.error(e); + process.exit(1); + }); }) .catch((e) => { logger.error(e); @@ -39,7 +46,6 @@ if (env === 'local') { }); } - /** * Event listener for HTTP server "error" event. */ @@ -96,7 +102,7 @@ process.on( // Prevent unhandled promise errors from crashing application process.on( 'unhandledRejection', - /* istanbul ignore next */ + /* istanbul ignore next */ (err: Error) => { if (err?.stack) { logger.error(err); diff --git a/doc-gen-service/src/v1/services/puppeteer-service.ts b/doc-gen-service/src/v1/services/puppeteer-service.ts index c4f68297e..63e1cc4e6 100644 --- a/doc-gen-service/src/v1/services/puppeteer-service.ts +++ b/doc-gen-service/src/v1/services/puppeteer-service.ts @@ -24,4 +24,4 @@ async function getBrowser() { return browser; } -export { browser, initBrowser, getBrowser }; +export { initBrowser, getBrowser }; diff --git a/frontend/src/components/Header.vue b/frontend/src/components/Header.vue index 2679fac6c..f7090f6a7 100644 --- a/frontend/src/components/Header.vue +++ b/frontend/src/components/Header.vue @@ -31,66 +31,49 @@ -