refactor: test compilation

This commit is contained in:
cha0s 2022-02-26 11:58:54 -06:00
parent 04d7094cea
commit 686eb162e8
4 changed files with 51 additions and 16 deletions

View File

@ -34,6 +34,7 @@
"src",
"start.js",
"start.js.map",
"test",
"test.js",
"test.js.map"
],

View File

@ -19,7 +19,10 @@ const config = require('../../../../core/src/bootstrap/fleck.neutrinorc');
config.use.push((neutrino) => {
// Test entrypoint.
const testPaths = glob.sync(join(FLECKS_ROOT, 'test/*.js'));
const testPaths = glob.sync(join(FLECKS_ROOT, 'test/*.js'), {ignore: 'platforms'});
for (let i = 0; i < this.platforms.length; ++i) {
testPaths.push(...glob.sync(join(FLECKS_ROOT, `test/platforms/${this.platforms[i]}/*.js`)));
}
if (testPaths.length > 0) {
const testEntry = neutrino.config.entry('test').clear();
testPaths.forEach((path) => testEntry.add(path));

View File

@ -35,6 +35,7 @@
"compression": "^1.7.4",
"debug": "4.3.1",
"express": "^4.17.1",
"glob": "^7.2.0",
"html-webpack-plugin": "^4.5.0",
"http-proxy": "^1.17.0",
"loader-utils": "^1.4.0",

View File

@ -1,8 +1,12 @@
const {realpathSync} = require('fs');
const {join} = require('path');
const {
dirname,
join,
} = require('path');
const {Flecks, require: R} = require('@flecks/core/server');
const D = require('debug');
const glob = require('glob');
const debug = D('@flecks/http/runtime');
@ -70,17 +74,33 @@ module.exports = (flecks) => (neutrino) => {
});
}
// Tests.
const testPaths = paths
.map(([path, resolved]) => [path, join(resolved, 'test')])
.filter(([, path]) => {
try {
R.resolve(path);
return true;
}
catch (error) {
return false;
}
});
const testRoots = Array.from(new Set(
Object.keys(httpFlecks.resolver)
.map((fleck) => [fleck, httpFlecks.root(fleck)]),
))
.map(([fleck, root]) => (
[fleck, dirname(R.resolve(join(root, 'package.json')))]
));
const testPaths = [];
testRoots.forEach(([fleck, root]) => {
testPaths.push(...(
glob.sync(join(root, 'test/*.js'))
.map((path) => [fleck, path])
));
for (let i = 0; i < httpFlecks.platforms.length; ++i) {
testPaths.push(
...(
glob.sync(join(root, `test/platforms/${httpFlecks.platforms[i]}/*.js`))
.map((path) => [fleck, path])
),
);
}
});
// Test entrypoint.
if (testPaths.length > 0) {
const testEntry = neutrino.config.entry('test').clear();
testPaths.forEach(([, path]) => testEntry.add(path));
}
const tests = realpathSync(R.resolve(join(flecks.resolve('@flecks/http'), 'tests')));
neutrino.config.module
.rule(tests)
@ -88,8 +108,18 @@ module.exports = (flecks) => (neutrino) => {
.use('runtime/test')
.loader(runtime)
.options({
source: testPaths.map(
([original, path]) => `describe('${original}', () => require('${path}'));`,
).join(''),
source: Object.entries(
testPaths
.reduce(
(r, [fleck, path]) => ({
...r,
[fleck]: [...(r[fleck] || []), `require('${path}');`],
}),
{},
),
)
.map(
([original, paths]) => `describe('${original}', () => { ${paths.join(' ')} });`,
).join(''),
});
};