|
| 1 | +import { createRequire } from 'node:module' |
| 2 | +import path from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import { execa } from 'execa' |
| 5 | +import fs from 'fs-extra' |
| 6 | +import merge from '../utils/merge-options.js' |
| 7 | +import { killIfProcessHangs } from './utils.js' |
| 8 | + |
| 9 | +const require = createRequire(import.meta.url) |
| 10 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 11 | + |
| 12 | +const mochaPackageFile = require.resolve('mocha/package.json') |
| 13 | +const mochaManifest = fs.readJSONSync(mochaPackageFile) |
| 14 | +const mochaBinary = path.join(path.dirname(mochaPackageFile), mochaManifest.bin._mocha) |
| 15 | + |
| 16 | +const bunPackageFile = require.resolve('bun/package.json') |
| 17 | +const bunManifest = fs.readJSONSync(bunPackageFile) |
| 18 | +const bunBinary = path.join(path.dirname(bunPackageFile), bunManifest.bin.bun) |
| 19 | + |
| 20 | +/** |
| 21 | + * @typedef {import("execa").Options} ExecaOptions |
| 22 | + * @typedef {import('../types.js').TestOptions} TestOptions |
| 23 | + * @typedef {import('../types.js').GlobalOptions} GlobalOptions |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {TestOptions & GlobalOptions} argv |
| 28 | + * @param {ExecaOptions} execaOptions |
| 29 | + */ |
| 30 | +export default async function testBun (argv, execaOptions) { |
| 31 | + const progress = argv.progress ? ['--reporter=progress'] : [] |
| 32 | + const files = argv.files.length > 0 |
| 33 | + ? argv.files |
| 34 | + : [ |
| 35 | + 'test/bun.*js', |
| 36 | + 'test/node.*js', |
| 37 | + 'test/**/*.spec.*js', |
| 38 | + 'test/bun.*ts', |
| 39 | + 'test/node.*ts', |
| 40 | + 'test/**/*.spec.*ts' |
| 41 | + ] |
| 42 | + |
| 43 | + const args = [ |
| 44 | + mochaBinary, |
| 45 | + ...files, |
| 46 | + ...progress, |
| 47 | + '--ui', 'bdd', |
| 48 | + '--require', 'source-map-support/register', |
| 49 | + `--timeout=${argv.timeout}`, |
| 50 | + '--color', 'true' |
| 51 | + ] |
| 52 | + |
| 53 | + if (argv.grep) { |
| 54 | + args.push(`--grep=${argv.grep}`) |
| 55 | + } |
| 56 | + |
| 57 | + if (argv.watch) { |
| 58 | + args.push('--watch') |
| 59 | + } |
| 60 | + |
| 61 | + if (argv.bail) { |
| 62 | + args.push('--bail') |
| 63 | + } |
| 64 | + |
| 65 | + if (argv['--']) { |
| 66 | + args.push(...argv['--']) |
| 67 | + } |
| 68 | + |
| 69 | + // before hook |
| 70 | + const before = await argv.fileConfig.test.before(argv) |
| 71 | + const beforeEnv = before && before.env ? before.env : {} |
| 72 | + |
| 73 | + // run mocha |
| 74 | + const proc = execa(bunBinary, args, |
| 75 | + merge( |
| 76 | + { |
| 77 | + env: { |
| 78 | + AEGIR_RUNNER: 'bun', |
| 79 | + NODE_ENV: process.env.NODE_ENV || 'test', |
| 80 | + ...beforeEnv |
| 81 | + }, |
| 82 | + preferLocal: true, |
| 83 | + localDir: path.join(__dirname, '../..'), |
| 84 | + stdio: argv.cov ? 'pipe' : 'inherit', |
| 85 | + forceKillAfterDelay: 1_000 |
| 86 | + }, |
| 87 | + execaOptions |
| 88 | + ) |
| 89 | + ) |
| 90 | + |
| 91 | + await killIfProcessHangs(proc, argv) |
| 92 | + |
| 93 | + // after hook |
| 94 | + await argv.fileConfig.test.after(argv, before) |
| 95 | +} |
0 commit comments