From 95cf29bc4ac80cbf7e2946c8f0d760a36c4e6bf3 Mon Sep 17 00:00:00 2001 From: homer0 Date: Sun, 7 Apr 2024 04:01:30 +0200 Subject: [PATCH] fix: use ts-async-import --- src/app/jimpex.ts | 6 ++++-- tests/app/jimpex.test.ts | 11 +++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/app/jimpex.ts b/src/app/jimpex.ts index d8d42f7..b2bb508 100644 --- a/src/app/jimpex.ts +++ b/src/app/jimpex.ts @@ -8,6 +8,7 @@ import { envUtilsProvider } from '@homer0/env-utils'; import { packageInfoProvider } from '@homer0/package-info'; import { pathUtilsProvider } from '@homer0/path-utils'; import { rootFileProvider } from '@homer0/root-file'; +import { tsAsyncImport } from '@homer0/ts-async-import'; import { EventsHub } from '@homer0/events-hub'; import { simpleConfigProvider } from '@homer0/simple-config'; import compression from 'compression'; @@ -613,8 +614,9 @@ export class Jimpex extends Jimple { * the application so they'll be available for all the services. */ protected async _loadESMModules(): Promise { - const { default: nodeFetch } = await import('node-fetch'); - const { default: mime } = await import('mime'); + const { default: nodeFetch } = + await tsAsyncImport('node-fetch'); + const { default: mime } = await tsAsyncImport('mime'); this.set('node-fetch', () => nodeFetch); this.set('mime', () => mime); diff --git a/tests/app/jimpex.test.ts b/tests/app/jimpex.test.ts index 0b0cfdb..fba3d54 100644 --- a/tests/app/jimpex.test.ts +++ b/tests/app/jimpex.test.ts @@ -1,8 +1,8 @@ /* eslint-disable no-process-env, dot-notation */ -jest.mock('node-fetch', () => 'node-fetch'); -jest.mock('mime', () => 'mime'); +jest.mock('@homer0/ts-async-import'); import fs from 'fs/promises'; import * as path from 'path'; +import { tsAsyncImport } from '@homer0/ts-async-import'; import { https, spdy, @@ -28,7 +28,14 @@ import type { ExpressMiddleware, } from '@src/types'; +const tsAsyncImportMock = tsAsyncImport as jest.MockedFunction; + describe('Jimpex', () => { + beforeEach(() => { + tsAsyncImportMock.mockClear(); + tsAsyncImportMock.mockImplementation((name) => Promise.resolve({ default: name })); + }); + describe('class', () => { beforeEach(() => { delete process.env['NODE_TLS_REJECT_UNAUTHORIZED'];