-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
290 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { GlobalContext } from './types' | ||
|
||
export const context: GlobalContext = { | ||
suites: [], | ||
currentSuite: null, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export const a = 1 | ||
import { run } from './run' | ||
export * from './suite' | ||
export { assert, should, expect } from 'chai' | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import fg from 'fast-glob' | ||
import c from 'picocolors' | ||
import { context } from './context' | ||
import { defaultSuite } from './suite' | ||
import type { File, Suite, Task, TaskResult } from './types' | ||
const { log } = console | ||
export async function run() { | ||
const rootDir = process.cwd() | ||
|
||
const files = await fg(['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], { | ||
cwd: rootDir, | ||
absolute: true, | ||
ignore: ['**/node_modules/**', '**/dist/**'], | ||
}) | ||
|
||
for (const file of files) | ||
await runFile(file) | ||
} | ||
async function runFile(filepath: string) { | ||
const file = await parseFile(filepath) | ||
for (const [suite, tasks] of file.tasks) { | ||
let indent = 1 | ||
if (suite.name) { | ||
log(suite.name) | ||
indent += 1 | ||
} | ||
|
||
const result = await runTasks(tasks) | ||
for (const r of result) { | ||
if (r.error === undefined) { | ||
log(`${' '.repeat(indent * 2)}${c.inverse(c.green(' PASS '))} ${c.green(r.task.name)}`) | ||
} | ||
else { | ||
console.error(`${' '.repeat(indent * 2)}${c.inverse(c.red(' FAIL '))} ${c.red(r.task.name)}`) | ||
console.error(' '.repeat((indent + 2) * 2) + c.red(String(r.error))) | ||
process.exitCode = 1 | ||
} | ||
} | ||
if (suite.name) | ||
indent -= 1 | ||
} | ||
} | ||
|
||
export async function parseFile(filepath: string) { | ||
await import(filepath) | ||
const suites = [defaultSuite, ...context.suites] | ||
|
||
const tasks = await Promise.all(suites.map(async (suite) => { | ||
context.currentSuite = suite | ||
return [suite, await suite.collect()] as [Suite, Task[]] | ||
})) | ||
|
||
const file: File = { | ||
filepath, | ||
suites, | ||
tasks, | ||
} | ||
|
||
file.tasks.forEach(([, tasks]) => | ||
tasks.forEach(task => task.file = file), | ||
) | ||
|
||
return file | ||
} | ||
|
||
async function runTasks(tasks: Task[]) { | ||
const results: TaskResult[] = [] | ||
for (const task of tasks) { | ||
const result: TaskResult = { task } | ||
try { | ||
await task.fn() | ||
} | ||
catch (e) { | ||
result.error = e | ||
} | ||
results.push(result) | ||
} | ||
|
||
return results | ||
} | ||
|
Oops, something went wrong.