metaltest/lib/suite.js

62 lines
1.7 KiB
JavaScript

import { readdir } from 'node:fs/promises'
import { join } from 'node:path'
const walkDir = async function* (dir = '.', exclude = []) {
const files = await readdir(dir, { withFileTypes: true })
for (const file of files) {
if (exclude.includes(file.name)) continue
if (file.isDirectory()) {
yield* walkDir(join(dir, file.name))
} else if (file.isSymbolicLink()) {
yield* walkDir(join(dir, file.name))
} else {
yield join(dir, file.name)
}
}
}
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
const getStackAbsolutePaths = () => {
const old = Error.prepareStackTrace
Error.prepareStackTrace = (_, stack) => stack
const stack = new Error().stack
Error.prepareStackTrace = old
return stack
.map(cs => cs.getFileName())
.filter(filename => filename?.startsWith('file://'))
.map(filename => dirname(fileURLToPath(filename)))
}
import { summaryreporter, errorreporter, totalreporter } from 'metaltest'
const suite = async (folder = '.') => {
const absolutePaths = getStackAbsolutePaths()
const absolutePath = absolutePaths[2]
const fileIgnored = []
const total = totalreporter()
for await (const file of walkDir(folder, ['node_modules', '.git'])) {
if (!file.endsWith('.test.js')) continue
const module = join(absolutePath, file)
const { test } = await import(module)
if (test === undefined) {
fileIgnored.push(file)
continue
}
await test.run(summaryreporter(), errorreporter(), total)
}
for (const file of fileIgnored) {
console.log(`The file ${file} doesn't export the metaltest object`)
}
console.log(total.msg())
}
export { suite }