From 8fe10762927fb5aab5b40d628632a183b63a9762 Mon Sep 17 00:00:00 2001 From: alin Date: Sat, 5 Oct 2024 11:04:54 +0200 Subject: [PATCH] Add loader test (incomplete) There is a problem getting the dynamic import() to work here. Not sure what causes it, but more tinkering is required --- .../ConfigurationLoaderFactory.test.js | 68 ++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/tests/browser/packages/config/loaders/ConfigurationLoaderFactory.test.js b/tests/browser/packages/config/loaders/ConfigurationLoaderFactory.test.js index 40d41e11..b52a5577 100644 --- a/tests/browser/packages/config/loaders/ConfigurationLoaderFactory.test.js +++ b/tests/browser/packages/config/loaders/ConfigurationLoaderFactory.test.js @@ -1,6 +1,7 @@ import { UnsupportedSourceError, ItemsLoader, + PathLoader, } from "@aedart/config"; import makeLoaderFactory from "../helpers/makeLoaderFactory"; @@ -30,9 +31,50 @@ describe('@aedart/config', () => { .toThrowError(UnsupportedSourceError); }); - it('can make loaders and load items', async () => { + xit('can make loaders and load items', async () => { + + // ------------------------------------------------------------------------------------ // + + console.info('FILE URL', import.meta.url); + console.info('FILE URL # 2', (new URL('../fixtures/my-config.js', import.meta.url)).toString()); + console.info('FILE URL # 3', (new URL('../fixtures/my-config.js', import.meta.url)).href); + console.info('FILE URL # 4', (new URL('../fixtures/my-config.js', import.meta.url)).pathname); + + class MyLoader { + async load(path) { + try { + return await import(path).default; + } catch (e) { + throw new Error(`Unable to load: ${e.message}`); + } + } + } - const factory = makeLoaderFactory(); + const fn = async (p) => { + const c = await import(p); + return c?.default; + } + + // TODO: WORKS + const raw = (await import('../fixtures/my-config.js')).default; + console.log('Raw import', raw); + + // TODO: + const viaFn = await fn('../fixtures/my-config.js'); + console.log('Fn import', viaFn); + + // const tmp = await (new MyLoader()).load('../fixtures/my-config.js'); + // const tmp = await (new MyLoader()).load((new URL('../fixtures/my-config.js', import.meta.url)).href); + // const tmp = await (new MyLoader()).load( + // 'file://' + (new URL('../fixtures/my-config.js', import.meta.url)).href + // ); + // const tmp = await (new MyLoader()).load( + // (new URL('../fixtures/my-config.js', import.meta.url)).toString() + // ); + const tmp = await (new MyLoader()).load( + (new URL('../fixtures/my-config.js', import.meta.url)) + ); + console.log('LOADED via MyLoader', tmp); const data = [ { @@ -42,10 +84,32 @@ describe('@aedart/config', () => { }, loader: ItemsLoader, expected: { foo: 'bar' } + }, + { + name: 'PathLoader', + //source: '../fixtures/my-config.js', // Relative does not seem to work here... + // source: path.resolve('../fixtures/my-config.js'), + // source: import.meta.url + '/../fixtures/my-config.js', + // source: '/home/alin/code/ion/tests/browser/packages/config/fixtures/my-config.js', + //source: '/home/alin/code/ion/tests/browser/packages/config/fixtures/my-config.js', + // source: (new URL('../fixtures/my-config.js', import.meta.url)).toString(), + + // source: (new URL('../fixtures/my-config.js', import.meta.url)).href, + + //source: 'file://' + (new URL('../fixtures/my-config.js', import.meta.url)).href, + + source: (new URL('../fixtures/my-config.js', import.meta.url)).href, + loader: PathLoader, + expected: { + app: { + name: 'Foo' + } + } } ]; // ------------------------------------------------------------------------------------ // + const factory = makeLoaderFactory(); for (const entry of data) {