Skip to content

Commit

Permalink
test: fix bundle with assets paths
Browse files Browse the repository at this point in the history
Signed-off-by: euberdeveloper <[email protected]>
  • Loading branch information
euberdeveloper committed Jun 20, 2024
1 parent 9f72f88 commit 8021441
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 131 deletions.
2 changes: 1 addition & 1 deletion build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function buildModule() {
plugins: [importMap.plugin()],
define: {
'__VERSION__': `"${packageJson.version}"`
}
}
});
}

Expand Down
42 changes: 32 additions & 10 deletions build.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import * as importMap from "esbuild-plugin-import-map";

import packageJson from './package.json' assert { type: 'json' };

importMap.load({
imports: {
'../dist/lib/index.js': '../../bundled/lib/esm/index.esm.js'
}
});
async function loadImportMapForBundle(bundledPath) {
importMap.clear();
await importMap.load({
imports: {
'../dist/lib/index.js': bundledPath
}
});
}

function getExternalDependencies(allow = []) {
const deps = packageJson.dependencies ? Object.keys(packageJson.dependencies).filter(dep => !allow.includes(dep)) : [];
Expand All @@ -23,16 +26,35 @@ async function buildModule() {
bundle: true,
minify: false,
treeShaking: false,
sourcemap: true
sourcemap: true,
external: getExternalDependencies(),
define: {
'process.env.__PARSE_TEST_RELATIVE_PATH__': `'../../../test/parse'`,
'process.env.__PARSE_TREE_TEST_RELATIVE_PATH__': `'../../../test/parseTree'`,
}
};

loadImportMapForBundle('../../../bundled/lib/commonjs/index.js');
await build({
...shared,
outfile: 'bundled/test/commonjs/index.js',
format: 'cjs',
plugins: [importMap.plugin()]
});

loadImportMapForBundle('../../../bundled/lib/esm/index.esm.js');
await build({
...shared,
outfile: 'bundled/test/index.esm.js',
outfile: 'bundled/test/esm/index.esm.js',
format: 'esm',
plugins: [importMap.plugin()],
external: getExternalDependencies()
plugins: [importMap.plugin()]
});
}

await buildModule();
function generateCommonjsPackageJson() {
const packageJsonCommonJs = JSON.stringify({ ...packageJson, type: undefined }, null, 2);
fs.writeFileSync('./bundled/test/commonjs/package.json', packageJsonCommonJs);
}

await buildModule();
generateCommonjsPackageJson();
32 changes: 17 additions & 15 deletions test/parse/parse.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function (expect, dree, path) {
export default function (expect, dree, path, samplePath) {

let platform = null;
switch (process.platform) {
Expand All @@ -14,13 +14,15 @@ export default function (expect, dree, path) {
}

async function importSample(filePath) {
return (await import (filePath)).default
const pathPrefix = process.env.__PARSE_TEST_RELATIVE_PATH__;
const pathToImport = pathPrefix ? path.join(pathPrefix, filePath) : filePath;
return (await import (pathToImport)).default
}

describe('Test: parse function', function () {

it('Should return the exported content of "test/parse/first.test.js"', async function () {
const result = dree.parse(path);
const result = dree.parse(samplePath);
const expected = await importSample(`./${platform}/first.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -32,7 +34,7 @@ export default function (expect, dree, path) {
symbolicLinks: false
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/second.test.js`);
expect(result).to.equal(expected);

Expand All @@ -46,7 +48,7 @@ export default function (expect, dree, path) {
showHidden: false
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/third.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -58,7 +60,7 @@ export default function (expect, dree, path) {
exclude: [/firebase/]
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/fourth.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -69,7 +71,7 @@ export default function (expect, dree, path) {
followLinks: true
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/fifth.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -80,7 +82,7 @@ export default function (expect, dree, path) {
sorted: true
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/sixth.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -91,7 +93,7 @@ export default function (expect, dree, path) {
sorted: (x, y) => y.localeCompare(x)
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/seventh.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -102,7 +104,7 @@ export default function (expect, dree, path) {
exclude: [/firebase/, '/**/notes.*']
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/eighth.test.js`);

expect(result).to.equal(expected);
Expand All @@ -115,7 +117,7 @@ export default function (expect, dree, path) {
exclude: '/**/firebase.*'
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/ninth.test.js`);

expect(result).to.equal(expected);
Expand All @@ -128,7 +130,7 @@ export default function (expect, dree, path) {
sorted: 'alpha'
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/tenth.test.js`);

expect(result).to.equal(expected);
Expand All @@ -141,7 +143,7 @@ export default function (expect, dree, path) {
sorted: 'antialpha'
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/eleventh.test.js`);

expect(result).to.equal(expected);
Expand All @@ -154,7 +156,7 @@ export default function (expect, dree, path) {
sorted: 'alpha-insensitive'
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/twelfth.test.js`);

expect(result).to.equal(expected);
Expand Down Expand Up @@ -182,7 +184,7 @@ export default function (expect, dree, path) {
sorted: 'antialpha-insensitive'
};

const result = dree.parse(path, options);
const result = dree.parse(samplePath, options);
const expected = await importSample(`./${platform}/thirteenth.test.js`);

expect(result).to.equal(expected);
Expand Down
32 changes: 17 additions & 15 deletions test/parse/parseAsync.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function (expect, dree, path) {
export default function (expect, dree, path, samplePath) {

let platform = null;
switch (process.platform) {
Expand All @@ -14,14 +14,16 @@ export default function (expect, dree, path) {
}

async function importSample(filePath) {
return (await import (filePath)).default
const pathPrefix = process.env.__PARSE_TEST_RELATIVE_PATH__;
const pathToImport = pathPrefix ? path.join(pathPrefix, filePath) : filePath;
return (await import (pathToImport)).default
}

describe('Test: parseAsync function', async function () {

it('Should return the exported content of "test/parse/first.test.js"', async function () {

const result = await dree.parseAsync(path);
const result = await dree.parseAsync(samplePath);
const expected = await importSample(`./${platform}/first.test.js`);
expect(result).to.equal(expected);

Expand All @@ -34,7 +36,7 @@ export default function (expect, dree, path) {
symbolicLinks: false
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/second.test.js`);
expect(result).to.equal(expected);

Expand All @@ -48,7 +50,7 @@ export default function (expect, dree, path) {
showHidden: false
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/third.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -60,7 +62,7 @@ export default function (expect, dree, path) {
exclude: [/firebase/]
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/fourth.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -71,7 +73,7 @@ export default function (expect, dree, path) {
followLinks: true
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/fifth.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -82,7 +84,7 @@ export default function (expect, dree, path) {
sorted: true
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/sixth.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -93,7 +95,7 @@ export default function (expect, dree, path) {
sorted: (x, y) => y.localeCompare(x)
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/seventh.test.js`);
expect(result).to.equal(expected);
});
Expand All @@ -104,7 +106,7 @@ export default function (expect, dree, path) {
exclude: [/firebase/, '/**/notes.*']
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/eighth.test.js`);

expect(result).to.equal(expected);
Expand All @@ -117,7 +119,7 @@ export default function (expect, dree, path) {
exclude: '/**/firebase.*'
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/ninth.test.js`);

expect(result).to.equal(expected);
Expand All @@ -130,7 +132,7 @@ export default function (expect, dree, path) {
sorted: 'alpha'
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/tenth.test.js`);

expect(result).to.equal(expected);
Expand All @@ -143,7 +145,7 @@ export default function (expect, dree, path) {
sorted: 'antialpha'
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/eleventh.test.js`);

expect(result).to.equal(expected);
Expand All @@ -156,7 +158,7 @@ export default function (expect, dree, path) {
sorted: 'alpha-insensitive'
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/twelfth.test.js`);

expect(result).to.equal(expected);
Expand All @@ -169,7 +171,7 @@ export default function (expect, dree, path) {
sorted: 'antialpha-insensitive'
};

const result = await dree.parseAsync(path, options);
const result = await dree.parseAsync(samplePath, options);
const expected = await importSample(`./${platform}/thirteenth.test.js`);

expect(result).to.equal(expected);
Expand Down
Loading

0 comments on commit 8021441

Please sign in to comment.