-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.utils.ts
More file actions
46 lines (41 loc) · 1 KB
/
Copy pathvitest.utils.ts
File metadata and controls
46 lines (41 loc) · 1 KB
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
import { execaSync } from "execa"
import { join } from "path"
import strip from "strip-ansi"
type CreateLogsMatcherOutput = {
should: {
contain: (match: string) => void
}
}
const createLogsMatcher = (output: string): CreateLogsMatcherOutput => {
return {
should: {
contain: (match) => {
expect(output).toMatch(new RegExp(match.replace(/\s+/g, `\\s+`)))
},
},
}
}
const cliBinLocation = join(__dirname, `bin.js`)
type InvokeCliResult = {
exitCode: number
stdout: string
logs: CreateLogsMatcherOutput
}
export function invokeCli(args: Array<string>): InvokeCliResult {
try {
const results = execaSync(process.execPath, [cliBinLocation].concat(args), {
cwd: __dirname,
})
return {
exitCode: results.exitCode,
stdout: strip(results.stdout.toString()),
logs: createLogsMatcher(strip(results.stdout.toString())),
}
} catch (err) {
return {
exitCode: err.exitCode,
stdout: strip(err.stderr.toString()),
logs: createLogsMatcher(strip(err.stderr?.toString() || ``)),
}
}
}