| 57 | * except for the ones provided in `$cliOptions`. |
| 58 | */ |
| 59 | export async function runVitest( |
| 60 | config: RunVitestConfig, |
| 61 | cliFilters: string[] = config.$cliFilters || [], |
| 62 | runnerOptions: VitestRunnerCLIOptions = {}, |
| 63 | ) { |
| 64 | // Reset possible previous runs |
| 65 | process.exitCode = 0 |
| 66 | let exitCode = process.exitCode |
| 67 | |
| 68 | if (runnerOptions.printExitCode) { |
| 69 | globalThis.process = new Proxy(process_, { |
| 70 | set(target, p, newValue, receiver) { |
| 71 | if (p === 'exitCode') { |
| 72 | // eslint-disable-next-line no-console |
| 73 | console.trace('exitCode was set to', newValue) |
| 74 | } |
| 75 | return Reflect.set(target, p, newValue, receiver) |
| 76 | }, |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | // Prevent possible process.exit() calls, e.g. from --browser |
| 81 | const exit = process.exit |
| 82 | process.exit = (() => { }) as never |
| 83 | |
| 84 | const stdout = new Writable({ |
| 85 | write(chunk, __, callback) { |
| 86 | if (runnerOptions.std === 'inherit') { |
| 87 | process.stdout.write(chunk.toString()) |
| 88 | } |
| 89 | callback() |
| 90 | }, |
| 91 | }) |
| 92 | |
| 93 | if (runnerOptions?.tty) { |
| 94 | (stdout as typeof process.stdout).isTTY = true |
| 95 | } |
| 96 | |
| 97 | const stderr = new Writable({ |
| 98 | write(chunk, __, callback) { |
| 99 | if (runnerOptions.std === 'inherit') { |
| 100 | process.stderr.write(chunk.toString()) |
| 101 | } |
| 102 | callback() |
| 103 | }, |
| 104 | }) |
| 105 | |
| 106 | // "node:tty".ReadStream doesn't work on Github Windows CI, let's simulate it |
| 107 | const stdin = new Readable({ read: () => '' }) as NodeJS.ReadStream |
| 108 | stdin.isTTY = true |
| 109 | stdin.setRawMode = () => stdin |
| 110 | const cli = new Cli({ stdin, stdout, stderr, preserveAnsi: runnerOptions.preserveAnsi }) |
| 111 | // @ts-expect-error not typed global |
| 112 | const currentConfig: SerializedConfig = __vitest_worker__.ctx.config |
| 113 | |
| 114 | let ctx: Vitest | undefined |
| 115 | let thrown = false |
| 116 | |