fix: test watching

This commit is contained in:
cha0s 2024-02-04 12:26:13 -06:00
parent 6df399a768
commit 3eb76b08e3
7 changed files with 31 additions and 77 deletions

27
package-lock.json generated
View File

@ -5585,21 +5585,6 @@
"webpack": ">=4.0.0 <6.0.0"
}
},
"node_modules/clear-module": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/clear-module/-/clear-module-4.1.2.tgz",
"integrity": "sha512-LWAxzHqdHsAZlPlEyJ2Poz6AIs384mPeqLVCru2p0BrP9G/kVGuhNyZYClLO6cXlnuJjzC8xtsJIuMjKqLXoAw==",
"dependencies": {
"parent-module": "^2.0.0",
"resolve-from": "^5.0.0"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/cli-cursor": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz",
@ -14063,17 +14048,6 @@
"tslib": "^2.0.3"
}
},
"node_modules/parent-module": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/parent-module/-/parent-module-2.0.0.tgz",
"integrity": "sha512-uo0Z9JJeWzv8BG+tRcapBKNJ0dro9cLyczGzulS6EfeyAdeC9sbojtW6XwvYxJkEne9En+J2XEl4zyglVeIwFg==",
"dependencies": {
"callsites": "^3.1.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/parse-json": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/parse-json/-/parse-json-5.2.0.tgz",
@ -19956,7 +19930,6 @@
"@flecks/core": "^3.2.1",
"babel-merge": "^3.0.0",
"chokidar": "^3.5.3",
"clear-module": "^4.1.2",
"debug": "^4.3.3",
"mocha": "^10.2.0"
},

View File

@ -179,9 +179,9 @@ exports.commands = (program, flecks) => {
const webpackConfig = await flecks.resolveBuildConfig('fleckspack.config.js');
const cmd = [
'npx', 'webpack',
...((watch || hot) ? ['watch'] : []),
'--config', webpackConfig,
'--mode', (production && !hot) ? 'production' : 'development',
...((watch || hot) ? ['--watch'] : []),
];
return spawnWith(
cmd,

View File

@ -37,7 +37,6 @@ const resolveValidModulePath = (source) => (path) => {
module.exports = async (env, argv, flecks) => {
const config = await configFn(env, argv, flecks);
config.externals = await externals();
config.output.clean = {keep: /test\.js(?:\.map)?/};
config.plugins.push(
new CopyPlugin({
patterns: [

View File

@ -11,7 +11,8 @@
"clean": "rm -rf dist node_modules yarn.lock",
"lint": "eslint --config ./build/eslint.config.js .",
"postversion": "npm run build",
"test": "webpack --config ../core/build/test.webpack.config.js --mode production && mocha --colors ./dist/test.js"
"test": "webpack --config ../core/build/test.webpack.config.js --mode production && mocha --colors ./dist/test.js",
"test:watch": "webpack watch --config ./build/test.webpack.config.js --mode development & mocha --parallel --watch --watch-files ./dist/test.js --colors ./dist/test.js"
},
"bin": {
"flecks": "./build/cli.js"

View File

@ -8,7 +8,8 @@
"clean": "rm -rf dist node_modules yarn.lock",
"lint": "eslint --config ./build/core.eslint.config.js .",
"postversion": "npm run build",
"test": "webpack --config ./build/test.webpack.config.js --mode production && mocha --colors ./dist/test.js"
"test": "webpack --config ./build/test.webpack.config.js --mode production && mocha --colors ./dist/test.js",
"test:watch": "webpack watch --config ./build/test.webpack.config.js --mode development & mocha --parallel --watch --watch-files ./dist/test.js --colors ./dist/test.js"
},
"repository": {
"type": "git",

View File

@ -1,13 +1,11 @@
const {stat, unlink} = require('fs/promises');
const {join} = require('path');
// eslint-disable-next-line import/no-extraneous-dependencies
const {commands: coreCommands} = require('@flecks/build/build/commands');
const {D} = require('@flecks/core/src');
const {glob} = require('@flecks/core/src/server');
const chokidar = require('chokidar');
const clearModule = require('clear-module');
const Mocha = require('mocha');
const {watchParallelRun} = require('mocha/lib/cli/watch-run');
const debug = D('@flecks/build.commands');
@ -33,17 +31,21 @@ module.exports = (program, flecks) => {
watch,
} = opts;
const {build} = coreCommands(program, flecks);
const child = await build.action('test', opts);
const testPaths = await glob(join(FLECKS_CORE_ROOT, 'test/**/*.js'));
if (0 === testPaths.length) {
// eslint-disable-next-line no-console
console.log('No fleck tests found.');
return child;
console.log('No tests found.');
return;
}
// Remove the previous test.
const testLocation = join(FLECKS_CORE_ROOT, 'dist', 'test.js');
if (watch) {
try {
await unlink(testLocation);
}
// eslint-disable-next-line no-empty
catch (error) {}
// Kick off building the test and wait for the file to exist.
await build.action('test', opts);
debug('Testing...', opts);
// eslint-disable-next-line no-constant-condition
while (true) {
@ -59,24 +61,7 @@ module.exports = (program, flecks) => {
});
}
}
const runMocha = async () => {
const mocha = new Mocha();
mocha.ui('bdd');
clearModule(testLocation);
mocha.addFile(testLocation);
mocha.loadFiles();
return new Promise((r, e) => {
mocha.run((code) => {
if (!code) {
r();
return;
}
const error = new Error('Tests failed');
error.code = code;
e(error);
});
});
};
// Magic.
require('@flecks/core/build/resolve')(
{
alias: flecks.resolver.aliases,
@ -84,28 +69,24 @@ module.exports = (program, flecks) => {
},
flecks.stubs,
);
if (!watch) {
await new Promise((resolve, reject) => {
child.on('exit', (code) => {
if (code !== 0) {
reject(code);
return;
}
resolve();
});
child.on('error', reject);
});
await runMocha();
return 0;
const mocha = new Mocha({parallel: true});
mocha.ui('bdd');
if (watch) {
watchParallelRun(mocha, {watchFiles: [testLocation]}, {file: [testLocation], spec: []});
return new Promise(() => {});
}
chokidar.watch(testLocation)
.on('all', async () => {
await new Promise((resolve) => {
setTimeout(resolve, 50);
});
runMocha();
mocha.files = [testLocation];
return new Promise((r, e) => {
mocha.run((code) => {
if (!code) {
r();
return;
}
const error = new Error('Tests failed');
error.code = code;
e(error);
});
return new Promise(() => {});
});
},
};
return commands;

View File

@ -23,7 +23,6 @@
"@flecks/core": "^3.2.1",
"babel-merge": "^3.0.0",
"chokidar": "^3.5.3",
"clear-module": "^4.1.2",
"debug": "^4.3.3",
"mocha": "^10.2.0"
},