Skip to content

Commit 8fe1076

Browse files
committed
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
1 parent db1a799 commit 8fe1076

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

Diff for: tests/browser/packages/config/loaders/ConfigurationLoaderFactory.test.js

+66-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
UnsupportedSourceError,
33
ItemsLoader,
4+
PathLoader,
45
} from "@aedart/config";
56
import makeLoaderFactory from "../helpers/makeLoaderFactory";
67

@@ -30,9 +31,50 @@ describe('@aedart/config', () => {
3031
.toThrowError(UnsupportedSourceError);
3132
});
3233

33-
it('can make loaders and load items', async () => {
34+
xit('can make loaders and load items', async () => {
35+
36+
// ------------------------------------------------------------------------------------ //
37+
38+
console.info('FILE URL', import.meta.url);
39+
console.info('FILE URL # 2', (new URL('../fixtures/my-config.js', import.meta.url)).toString());
40+
console.info('FILE URL # 3', (new URL('../fixtures/my-config.js', import.meta.url)).href);
41+
console.info('FILE URL # 4', (new URL('../fixtures/my-config.js', import.meta.url)).pathname);
42+
43+
class MyLoader {
44+
async load(path) {
45+
try {
46+
return await import(path).default;
47+
} catch (e) {
48+
throw new Error(`Unable to load: ${e.message}`);
49+
}
50+
}
51+
}
3452

35-
const factory = makeLoaderFactory();
53+
const fn = async (p) => {
54+
const c = await import(p);
55+
return c?.default;
56+
}
57+
58+
// TODO: WORKS
59+
const raw = (await import('../fixtures/my-config.js')).default;
60+
console.log('Raw import', raw);
61+
62+
// TODO:
63+
const viaFn = await fn('../fixtures/my-config.js');
64+
console.log('Fn import', viaFn);
65+
66+
// const tmp = await (new MyLoader()).load('../fixtures/my-config.js');
67+
// const tmp = await (new MyLoader()).load((new URL('../fixtures/my-config.js', import.meta.url)).href);
68+
// const tmp = await (new MyLoader()).load(
69+
// 'file://' + (new URL('../fixtures/my-config.js', import.meta.url)).href
70+
// );
71+
// const tmp = await (new MyLoader()).load(
72+
// (new URL('../fixtures/my-config.js', import.meta.url)).toString()
73+
// );
74+
const tmp = await (new MyLoader()).load(
75+
(new URL('../fixtures/my-config.js', import.meta.url))
76+
);
77+
console.log('LOADED via MyLoader', tmp);
3678

3779
const data = [
3880
{
@@ -42,10 +84,32 @@ describe('@aedart/config', () => {
4284
},
4385
loader: ItemsLoader,
4486
expected: { foo: 'bar' }
87+
},
88+
{
89+
name: 'PathLoader',
90+
//source: '../fixtures/my-config.js', // Relative does not seem to work here...
91+
// source: path.resolve('../fixtures/my-config.js'),
92+
// source: import.meta.url + '/../fixtures/my-config.js',
93+
// source: '/home/alin/code/ion/tests/browser/packages/config/fixtures/my-config.js',
94+
//source: '/home/alin/code/ion/tests/browser/packages/config/fixtures/my-config.js',
95+
// source: (new URL('../fixtures/my-config.js', import.meta.url)).toString(),
96+
97+
// source: (new URL('../fixtures/my-config.js', import.meta.url)).href,
98+
99+
//source: 'file://' + (new URL('../fixtures/my-config.js', import.meta.url)).href,
100+
101+
source: (new URL('../fixtures/my-config.js', import.meta.url)).href,
102+
loader: PathLoader,
103+
expected: {
104+
app: {
105+
name: 'Foo'
106+
}
107+
}
45108
}
46109
];
47110

48111
// ------------------------------------------------------------------------------------ //
112+
const factory = makeLoaderFactory();
49113

50114
for (const entry of data) {
51115

0 commit comments

Comments
 (0)