1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = expectationResultFactory;
|
7 |
|
8 | var _prettyFormat = require('pretty-format');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function messageFormatter({error, message, passed}) {
|
17 | if (passed) {
|
18 | return 'Passed.';
|
19 | }
|
20 |
|
21 | if (message) {
|
22 | return message;
|
23 | }
|
24 |
|
25 | if (typeof error === 'string') {
|
26 | return error;
|
27 | }
|
28 |
|
29 | if (
|
30 |
|
31 | error &&
|
32 | typeof error === 'object' &&
|
33 | typeof error.message === 'string' &&
|
34 | typeof error.name === 'string'
|
35 | ) {
|
36 | if (error.message === '') {
|
37 | return error.name;
|
38 | }
|
39 |
|
40 | return `${error.name}: ${error.message}`;
|
41 | }
|
42 |
|
43 | return `thrown: ${(0, _prettyFormat.format)(error, {
|
44 | maxDepth: 3
|
45 | })}`;
|
46 | }
|
47 |
|
48 | function stackFormatter(options, initError, errorMessage) {
|
49 | if (options.passed) {
|
50 | return '';
|
51 | }
|
52 |
|
53 | if (options.error) {
|
54 | if (typeof options.error.stack === 'string') {
|
55 | return options.error.stack;
|
56 | }
|
57 |
|
58 | if (options.error === errorMessage) {
|
59 | return errorMessage;
|
60 | }
|
61 | }
|
62 |
|
63 | if (initError) {
|
64 | return errorMessage.trimRight() + '\n\n' + initError.stack;
|
65 | }
|
66 |
|
67 | return new Error(errorMessage).stack;
|
68 | }
|
69 |
|
70 | function expectationResultFactory(options, initError) {
|
71 | const message = messageFormatter(options);
|
72 | const stack = stackFormatter(options, initError, message);
|
73 |
|
74 | if (options.passed) {
|
75 | return {
|
76 | error: options.error,
|
77 | matcherName: options.matcherName,
|
78 | message,
|
79 | passed: options.passed,
|
80 | stack
|
81 | };
|
82 | }
|
83 |
|
84 | return {
|
85 | actual: options.actual,
|
86 | error: options.error,
|
87 | expected: options.expected,
|
88 | matcherName: options.matcherName,
|
89 | message,
|
90 | passed: options.passed,
|
91 | stack
|
92 | };
|
93 | }
|