/
create-result.ts
51 lines (44 loc) · 1.27 KB
/
create-result.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
import chalk from 'chalk';
import { matcherHint, printExpected } from 'jest-matcher-utils';
import { AnyFunction, Collection, GeneratorCreator, IGenerator } from './gen';
export type ResultCreator = (options: {
pass: boolean;
message: () => string;
notMessage: () => string;
}) => {
message: () => string;
pass: boolean;
};
export const createResult: ResultCreator = ({ pass, message, notMessage }) => ({
message: () => (pass ? notMessage() : message()).trim(),
pass
});
export const createToHandleComparer = (matcherName: string, createGenerator: GeneratorCreator) => (
fn: AnyFunction,
shape: Collection
) => {
const generator: IGenerator = createGenerator(shape);
const result = generator.assert(fn);
const message = (hint, permutation, error) => `
${hint}
When called with:
${permutation}
Throws:
${error}
`;
const notMessage = (hint, msg) => `
${hint}
${msg}
`;
return createResult({
message: () =>
message(
matcherHint(`.${matcherName}`, `Function ${fn.name}`),
printExpected(result.permutation),
chalk.red(result.error.stack)
),
notMessage: () =>
notMessage(matcherHint(`.not.${matcherName}`, `Function ${fn.name}`), chalk.red('No Error was thrown')),
pass: result.pass
});
};