diff --git a/package.json b/package.json index cbf0d2c..585df32 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "private": true, "scripts": { "start": "node scripts/start-electron.js", - "test": "node --test \"tests/unit/*.test.js\"", + "test": "node scripts/run-unit-tests.js", "sample": "node scripts/make-sample-guide.js", "package:windows": "node scripts/package-windows.js", "build": "bash scripts/build-release.sh", diff --git a/scripts/run-unit-tests.js b/scripts/run-unit-tests.js new file mode 100644 index 0000000..a7dbcd0 --- /dev/null +++ b/scripts/run-unit-tests.js @@ -0,0 +1,42 @@ +'use strict'; + +const fs = require('node:fs'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); + +function collectTestFiles(rootDir) { + const files = []; + if (!fs.existsSync(rootDir)) return files; + + const walk = (dir) => { + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { + const full = path.join(dir, entry.name); + if (entry.isDirectory()) { + walk(full); + continue; + } + if (/\.(test|spec)\.(js|mjs|cjs)$/.test(entry.name)) files.push(full); + } + }; + + walk(rootDir); + files.sort((a, b) => a.localeCompare(b)); + return files; +} + +const root = path.join(process.cwd(), 'tests', 'unit'); +const tests = collectTestFiles(root); + +if (!tests.length) { + console.log('No unit test files found under tests/unit, skipping unit tests.'); + process.exit(0); +} + +const result = spawnSync(process.execPath, ['--test', ...tests], { stdio: 'inherit' }); + +if (result.error) { + console.error(result.error.message); + process.exit(result.status ?? 1); +} + +process.exit(result.status ?? 0);