UNPKG

2.45 kBJavaScriptView Raw
1const AssertionString = require('./AssertionString');
2
3module.exports = function createStandardErrorMessage(
4 output,
5 subject,
6 testDescription,
7 args,
8 options = {}
9) {
10 const preamble = 'expected';
11
12 const subjectOutput = output.clone();
13 if (subject) {
14 subject.call(subjectOutput, subjectOutput);
15 }
16
17 const argsOutput = output.clone();
18 if (typeof args === 'function') {
19 args.call(argsOutput, argsOutput);
20 } else {
21 if (args.length > 0) {
22 let previousWasAssertion = false;
23 args.forEach((arg, index) => {
24 const isAssertion =
25 arg && typeof arg === 'object' && arg instanceof AssertionString;
26 if (index > 0) {
27 if (!isAssertion && !previousWasAssertion) {
28 argsOutput.text(',');
29 }
30 argsOutput.sp();
31 }
32 if (isAssertion) {
33 argsOutput.error(arg.text);
34 } else {
35 arg.call(argsOutput, argsOutput);
36 }
37 previousWasAssertion = isAssertion;
38 });
39 }
40 }
41
42 const subjectSize = subjectOutput.size();
43 const argsSize = argsOutput.size();
44 const width =
45 preamble.length +
46 subjectSize.width +
47 argsSize.width +
48 testDescription.length;
49 const height = Math.max(subjectSize.height, argsSize.height);
50
51 if ('omitSubject' in output && output.omitSubject === options.subject) {
52 const matchTestDescription = /^(not )?to (.*)/.exec(testDescription);
53 if (matchTestDescription) {
54 output.error('should ');
55 if (matchTestDescription[1]) {
56 output.error('not ');
57 }
58 testDescription = matchTestDescription[2];
59 } else {
60 testDescription = `expected: ${testDescription}`;
61 }
62 } else if (
63 options.compact &&
64 options.compactSubject &&
65 (subjectSize.height > 1 || subjectSize.width > (options.compactWidth || 35))
66 ) {
67 output.error('expected').sp();
68 options.compactSubject.call(output, output);
69 output.sp();
70 } else {
71 output.error(preamble);
72 if (subjectSize.height > 1) {
73 output.nl();
74 } else {
75 output.sp();
76 }
77 output.append(subjectOutput);
78 if (
79 subjectSize.height > 1 ||
80 (height === 1 && width > output.preferredWidth)
81 ) {
82 output.nl();
83 } else {
84 output.sp();
85 }
86 }
87
88 output.error(testDescription);
89
90 if (argsSize.height > 1) {
91 output.nl();
92 } else if (argsSize.width > 0) {
93 output.sp();
94 }
95
96 output.append(argsOutput);
97
98 return output;
99};