-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
parseAsssetion.ts
131 lines (108 loc) · 3.39 KB
/
parseAsssetion.ts
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
import process from 'node:process';
import path from 'node:path';
import assert from 'node:assert';
import { EOL } from 'node:os';
import { format } from './format.js';
import { hr } from './hr.js';
export type ParseAssertionOptions = {
message?: string | Error;
defaultMessage?: string;
actual?: string;
expected?: string;
throw?: boolean;
hideDiff?: boolean;
};
const findFile = (error: Error) => {
const stackLines = error.stack?.split(EOL) || [];
let file = '';
const basePath = 'poku/lib/';
for (const line of stackLines) {
if (!line.includes(basePath)) {
const match = line.match(
/at\s(\/.+|file:.+)|^(\s+)at\smodule\scode\s\((\/.+|file:.+)\)/i
);
// Node and Deno
if (match && match[1]) {
file = match[1];
break;
}
// Bun
if (match && match[3]) {
file = match[3];
break;
}
}
}
return file;
};
const formatFail = (str: string) => format.bold(format.fail(`✘ ${str}`));
export const parseAssertion = (
cb: () => void,
options: ParseAssertionOptions
) => {
const isPoku =
typeof process.env?.FILE === 'string' && process.env?.FILE.length > 0;
const FILE = process.env.FILE;
try {
cb();
if (typeof options.message === 'string') {
const message = isPoku
? `${format.bold(format.success(`✔ ${options.message}`))} ${format.dim(format.success(`› ${FILE}`))}`
: format.bold(format.success(`✔ ${options.message}`));
console.log(message);
}
} catch (error) {
if (error instanceof assert.AssertionError) {
const { code, actual, expected, operator } = error;
const absoultePath = findFile(error).replace(/file:/, '');
const file = path.relative(path.resolve(process.cwd()), absoultePath);
let message: string = '';
if (typeof options.message === 'string') message = options.message;
else if (options.message instanceof Error)
message = options.message.message;
else if (typeof options.defaultMessage === 'string')
message = options.defaultMessage;
const finalMessage =
message?.trim().length > 0
? `${formatFail(message)}`
: `${formatFail('No Message')}`;
console.log(
isPoku
? `${finalMessage} ${format.dim(format.fail(`› ${FILE}`))}`
: finalMessage
);
file && console.log(`${format.dim(' File')} ${file}`);
console.log(`${format.dim(' Code')} ${code}`);
console.log(`${format.dim(' Operator')} ${operator}${EOL}`);
if (!options?.hideDiff) {
console.log(format.dim(` ${options?.actual || 'Actual'}:`));
console.log(
format.bold(
typeof actual === 'function' || actual instanceof RegExp
? ` ${String(actual)}`
: ` ${format.fail(JSON.stringify(actual))}`
)
);
console.log(
`${EOL} ${format.dim(`${options?.expected || 'Expected'}:`)}`
);
console.log(
format.bold(
`${
typeof expected === 'function' || expected instanceof RegExp
? ` ${String(expected)}`
: ` ${format.success(JSON.stringify(expected))}`
}`
)
);
}
if (options.throw) {
console.log(error);
hr();
}
process.exit(1);
}
// Non-assertion errors
throw error;
}
};