metaltest/lib/suite.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-12-04 16:30:05 -05:00
import { readdir } from 'node:fs/promises'
import { join } from 'node:path'
const walkDir = async function* (dir = '.', exclude = []) {
2022-12-04 16:30:05 -05:00
const files = await readdir(dir, { withFileTypes: true })
for (const file of files) {
if (exclude.includes(file.name)) continue
2023-01-03 23:58:50 -05:00
2022-12-04 16:30:05 -05:00
if (file.isDirectory()) {
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'
2022-12-04 16:30:05 -05:00
const suite = async (folder = '.') => {
const absolutePaths = getStackAbsolutePaths()
const absolutePath = absolutePaths[2]
const fileIgnored = []
2022-12-04 16:30:05 -05:00
const total = totalreporter()
2023-01-03 23:58:50 -05:00
for await (const file of walkDir(folder, ['node_modules', '.git'])) {
2022-12-05 17:08:02 -05:00
if (!file.endsWith('.test.js')) continue
2022-12-04 16:30:05 -05:00
const module = join(absolutePath, file)
const { test } = await import(module)
if (test === undefined) {
fileIgnored.push(file)
continue
}
await test.run(summaryreporter(), errorreporter(), total)
2022-12-04 16:30:05 -05:00
}
for (const file of fileIgnored) {
console.log(`The file ${file} doesn't export the metaltest object`)
}
2022-12-04 16:30:05 -05:00
console.log(total.msg())
}
export { suite }