Skip to content

Commit

Permalink
Initial completion of the function
Browse files Browse the repository at this point in the history
  • Loading branch information
TickHeart committed Jun 28, 2022
1 parent d34e522 commit 1202415
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 18 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"build": "tsup",
"dev": "tsup --watch"
Expand All @@ -20,11 +21,16 @@
},
"devDependencies": {
"@antfu/eslint-config": "^0.25.2",
"@types/chai": "^4.3.1",
"@types/node": "^18.0.0",
"eslint": "^8.18.0",
"esno": "^0.16.3",
"tsup": "^6.1.2",
"typescript": "^4.7.4"
},
"dependencies": {
"chai": "^4.3.6"
"chai": "^4.3.6",
"fast-glob": "^3.2.11",
"picocolors": "^1.0.0"
}
}
95 changes: 79 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added src/cli.ts
Empty file.
6 changes: 6 additions & 0 deletions src/context.ts
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,
}
5 changes: 4 additions & 1 deletion src/index.ts
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()
81 changes: 81 additions & 0 deletions src/run.ts
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
}

Loading

0 comments on commit 1202415

Please sign in to comment.