1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264 |
x1
x10
x1
x10
x10
x10
x10
x10
x20
x1
x10
x10
x10
x10
x10
x10
x10
x10
x10
x10
x10
x10
x10
x10
x10
x1
x10
x50
x1
x10
x10
x65
x65
x66
x66
x65
x306
x102
x65
x65
x67
x536
x201
x67
x68
x204
x68
x67
x65
x66
x67
x67
x67
x67
x67
x67
x66
x66
x119
x65
x67
x536
x670
x201
x67
x68
x204
x68
x67
x65
x66
x67
x67
x67
x67
x67
x67
x66
x66
x118
x118
x456
x169
x1338
x223
x227
x227
x2134
x804
x268
x268
x269
x271
x272
x272
x271
x271
x269
x268
x268
x268
x269
x271
x271
x272
x272
x271
x269
x268
x223
x223
x118
x65
x10
x10
x10
x10
x14
x15
x15
x170
x17
x187
x17
x17
x17
x187
x14
x10
x10
x58
x58
x413
x1062
x59
x58
x354
x826
x59
x58
x104
x104
x58
x58
x10
x10
x63
x63
x63
x63
x63
x63
x63
x63
|
I
I
|
import type { Nullable, Promisable, rw } from "@libs/typing"
import { command } from "@libs/run/command"
export type { Nullable, Promisable }
let format = (name: string, { header = "" } = {} as { header?: string; underline?: boolean; type?: string }) => `${header} ${name}`
if (globalThis.Deno) {
const { highlight } = await import("./highlight.ts")
format = highlight
}
export type testing = any
export class TestingError extends Error {}
export const available = {
deno: true,
bun: null,
node: null,
} as Readonly<Record<runtime, Nullable<boolean>>>
export const paths = {
deno: "deno",
bun: "bun",
node: "node",
npx: "npx",
}
export const placeholder = () => void null
export type runtime = "deno" | "bun" | "node"
export type options = {
ignore?: Deno.TestDefinition["ignore"]
only?: Deno.TestDefinition["only"]
permissions?: Deno.TestDefinition["permissions"]
sanitizeResources?: Deno.TestDefinition["sanitizeResources"]
sanitizeOps?: Deno.TestDefinition["sanitizeOps"]
sanitizeExit?: Deno.TestDefinition["sanitizeExit"]
env?: Record<string, string>
}
export type tester = (...runtimes: Array<runtime | "all">) => (name: string, fn: () => Promisable<void>, options?: options) => Promisable<void>
export type todo_tester = (...runtimes: Array<runtime | "all">) => (name: string, fn?: () => Promisable<void>, options?: options) => Promisable<void>
export const test = Object.assign(_test.bind(null, "test"), { skip: _test.bind(null, "skip"), only: _test.bind(null, "only"), todo: _test.bind(null, "todo") }) as (tester & { skip: tester; only: tester; todo: todo_tester })
function _test(mode: mode, ...runtimes: Array<runtime | "all">): (name: string, fn: () => Promisable<void>, options?: options) => Promisable<void> {
const global = globalThis as rw
if (runtimes.includes("all")) {
runtimes = Object.keys(available) as runtime[]
}
if (!runtimes.length) {
runtimes = ["deno"]
}
if ((global.Deno) && (runtimes.includes("bun")) && (available.bun === null)) {
try {
command(paths.bun, ["--version"], { stdout: null, stderr: null, sync: true, throw: true })
Object.assign(available, { bun: true })
} catch (error) {
if (error instanceof Deno.errors.NotCapable) {
Object.assign(available, { bun: false })
} else {
throw error
}
}
}
if (global.Bun) {
return async function (name: string, fn = placeholder) {
try {
const { test } = await import(`${"bun"}:test`)
test(name, fn)
} catch (error) {
throw error
}
}
}
if ((global.Deno) && (runtimes.includes("node")) && (available.node === null)) {
try {
command(paths.node, ["--version"], { stdout: null, stderr: null, sync: true, throw: true })
command(paths.npx, ["tsx", "--version"], { stdout: null, stderr: null, sync: true, throw: true, winext: ".cmd" })
Object.assign(available, { node: true })
} catch (error) {
if (error instanceof Deno.errors.NotCapable) {
Object.assign(available, { node: false })
} else {
throw error
}
}
}
if ((!global.Deno) && (global.process?.versions?.node)) {
return async function (name: string, fn = placeholder) {
try {
const { test } = await import(`${"node"}:test`)
test(name, fn)
} catch (error) {
throw error
}
}
}
const filename = caller()
return function (name: string, fn = placeholder, options = { permissions: "none" } as options) {
for (const runtime of runtimes as runtime[]) {
const runner = { test: Deno.test, skip: Deno.test.ignore, todo: Deno.test.ignore, only: Deno.test.only }[available[runtime as runtime] ? mode : "skip"]
if ((options as { __norun?: boolean })?.__norun) {
continue
}
runner(format(name, { header: runtime.padEnd(4).toLocaleUpperCase(), underline: true, type: ({ skip: "warn", todo: "debug" } as Record<PropertyKey, string>)[mode] }), runtime === "deno" ? options : {}, function () {
const original = { env: {} as NonNullable<typeof options["env"]> }
try {
if ((runtime === "deno") && options.env) {
for (const [key, value] of Object.entries(options.env)) {
if (Deno.env.has(key)) {
original.env[key] = Deno.env.get(key)!
}
Deno.env.set(key, value)
}
}
return testcase(runtime, filename, name, fn, (options as { __dryrun?: boolean })?.__dryrun)
} finally {
if ((runtime === "deno") && options.env) {
for (const key of Object.keys(options.env)) {
Deno.env.delete(key)
if (original.env[key] !== undefined) {
Deno.env.set(key, original.env[key])
}
}
}
}
})
}
}
}
export const cache = new Set<string>() as Set<string>
export function install([bin, ...args]: string[], filename: string, { winext = "" } = {}) {
if (cache.has(`${bin}:${filename}`)) {
return
}
const { stdout } = command(paths.deno, ["info", "--json", filename], { stdout: "piped", stderr: null, sync: true, throw: true })
const { packages, npmPackages: _ } = JSON.parse(stdout)
command(bin, [...args, ...Object.keys(packages)], { stdout: null, stderr: null, sync: true, throw: true, winext, dryrun: !bin })
cache.add(`${bin}:${filename}`)
const payload = "let meta = JSON.parse(await Deno.readTextFile('package.json')); if (meta.type !== 'module') await Deno.writeTextFile('package.json', JSON.stringify((meta.type = 'module', meta)))"
command(paths.deno, ["eval", payload], { stdout: null, stderr: null, sync: true, throw: true, winext, dryrun: !bin })
}
export async function testcase(runtime: runtime, filename: string, name: string, fn: () => Promisable<void>, dryrun = false) {
switch (runtime) {
case "node":
install([paths.npx, "jsr", "add"], filename, { winext: ".cmd" })
await command(paths.npx, ["tsx", "--test-reporter", "spec", "--test-name-pattern", name, "--test", filename], { stdout: "piped", stderr: "piped", throw: true, env: { FORCE_COLOR: "true" }, winext: ".cmd", dryrun })
break
case "bun":
install([paths.bun, "x", "jsr", "add"], filename)
await command(paths.bun, ["test", "--test-name-pattern", name, filename], { stdout: "piped", stderr: "piped", throw: true, env: { FORCE_COLOR: "1" }, dryrun })
break
case "deno":
await fn()
break
}
}
function caller() {
const Trace = Error as unknown as { prepareStackTrace: (error: Error, stack: CallSite[]) => unknown }
const _ = Trace.prepareStackTrace
Trace.prepareStackTrace = (_, stack) => stack
const { stack } = new Error()
Trace.prepareStackTrace = _
const caller = (stack as unknown as CallSite[])[2]
return caller.getFileName().replaceAll("\\", "/")
}
type CallSite = { getFileName: () => string }
type mode = "test" | "skip" | "only" | "todo"
|