UNPKG

@whook/whook

Version:

Build strong and efficient REST web services.

115 lines 3.06 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ import { describe, test, beforeEach, jest, expect } from '@jest/globals'; import initHOST from './HOST.js'; describe('initHOST', () => { const log = jest.fn(); const importer = jest.fn(); const internalIp = { internalIpV4: jest.fn() }; beforeEach(() => { log.mockReset(); importer.mockReset(); internalIp.internalIpV4.mockReset(); }); test('should use the env HOST first', async () => { importer.mockResolvedValueOnce(internalIp); const HOST = await initHOST({ ENV: { HOST: '192.168.1.11' }, log, importer, }); expect(HOST).toMatchInlineSnapshot(`"192.168.1.11"`); expect({ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')), requireCalls: importer.mock.calls, internalIpV4Calls: internalIp.internalIpV4.mock.calls, }).toMatchInlineSnapshot(` { "internalIpV4Calls": [], "logCalls": [ [ "debug", "🏭 - Initializing the HOST service.", ], [ "warning", "♻️ - Using ENV host "192.168.1.11"", ], ], "requireCalls": [], } `); }); test('should find a HOST by itself if no env HOST', async () => { importer.mockResolvedValueOnce(internalIp); internalIp.internalIpV4.mockResolvedValueOnce('192.168.1.10'); const HOST = await initHOST({ ENV: {}, log, importer, }); expect(HOST).toMatchInlineSnapshot(`"192.168.1.10"`); expect({ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')), requireCalls: importer.mock.calls, internalIpV4Calls: internalIp.internalIpV4.mock.calls, }).toMatchInlineSnapshot(` { "internalIpV4Calls": [ [], ], "logCalls": [ [ "debug", "🏭 - Initializing the HOST service.", ], [ "warning", "✔ - Using detected host "192.168.1.10".", ], ], "requireCalls": [ [ "internal-ip", ], ], } `); }); test('should fallback to localhost', async () => { importer.mockResolvedValueOnce(internalIp); internalIp.internalIpV4.mockResolvedValueOnce(''); const HOST = await initHOST({ ENV: {}, log, importer, }); expect(HOST).toMatchInlineSnapshot(`"localhost"`); expect({ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')), requireCalls: importer.mock.calls, internalIpV4Calls: internalIp.internalIpV4.mock.calls, }).toMatchInlineSnapshot(` { "internalIpV4Calls": [ [], ], "logCalls": [ [ "debug", "🏭 - Initializing the HOST service.", ], [ "warning", "🚫 - Could not detect any host. Fallback to "localhost".", ], ], "requireCalls": [ [ "internal-ip", ], ], } `); }); }); //# sourceMappingURL=HOST.test.js.map