Skip to content

Commit

Permalink
Add loader test (incomplete)
Browse files Browse the repository at this point in the history
There is a problem getting the dynamic import() to work here. Not sure what causes it, but more tinkering is required
  • Loading branch information
aedart committed Oct 5, 2024
1 parent db1a799 commit 8fe1076
Showing 1 changed file with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
UnsupportedSourceError,
ItemsLoader,
PathLoader,
} from "@aedart/config";
import makeLoaderFactory from "../helpers/makeLoaderFactory";

Expand Down Expand Up @@ -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 = [
{
Expand All @@ -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) {

Expand Down

0 comments on commit 8fe1076

Please sign in to comment.