How to use formatError method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

json_schema_test.js

Source: json_schema_test.js Github

copy
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5var AssertTrue = requireNative('assert').AssertTrue;
6var JSONSchemaValidator = require('json_schema').JSONSchemaValidator;
7var LOG = requireNative('logging').LOG;
8
9function assertValid(type, instance, schema, types) {
10  var validator = new JSONSchemaValidator();
11  if (types)
12    validator.addTypes(types);
13  validator["validate" + type](instance, schema, "");
14  var success = true;
15  if (validator.errors.length != 0) {
16    LOG("Got unexpected errors");
17    for (var i = 0; i < validator.errors.length; i++) {
18      LOG(validator.errors[i].message + "  path: " + validator.errors[i].path);
19    }
20    success = false;
21  }
22  AssertTrue(success);
23}
24
25function assertNotValid(type, instance, schema, errors, types) {
26  var validator = new JSONSchemaValidator();
27  if (types)
28    validator.addTypes(types);
29  validator["validate" + type](instance, schema, "");
30  AssertTrue(validator.errors.length === errors.length);
31  var success = true;
32  for (var i = 0; i < errors.length; i++) {
33    if (validator.errors[i].message == errors[i]) {
34      LOG("Got expected error: " + validator.errors[i].message +
35          " for path: " + validator.errors[i].path);
36    } else {
37      LOG("Missed expected error: " + errors[i] + ". Got: " +
38          validator.errors[i].message + " instead.");
39      success = false;
40    }
41  }
42  AssertTrue(success);
43}
44
45function assertListConsistsOfElements(list, elements) {
46  var success = true;
47  for (var li = 0; li < list.length; li++) {
48    for (var ei = 0; ei < elements.length && list[li] != elements[ei]; ei++) { }
49    if (ei == elements.length) {
50      LOG("Expected type not found: " + list[li]);
51      success = false;
52    }
53  }
54  AssertTrue(success);
55}
56
57function assertEqualSets(set1, set2) {
58  assertListConsistsOfElements(set1, set2);
59  assertListConsistsOfElements(set2, set1);
60}
61
62function formatError(key, replacements) {
63  return JSONSchemaValidator.formatError(key, replacements);
64}
65
66function testFormatError() {
67  AssertTrue(formatError("propertyRequired") == "Property is required.");
68  AssertTrue(formatError("invalidEnum", ["foo, bar"]) ==
69         "Value must be one of: [foo, bar].");
70  AssertTrue(formatError("invalidType", ["foo", "bar"]) ==
71         "Expected 'foo' but got 'bar'.");
72}
73
74function testComplex() {
75  var schema = {
76    type: "array",
77    items: [
78      {
79        type: "object",
80        properties: {
81          id: {
82            type: "integer",
83            minimum: 1
84          },
85          url: {
86            type: "string",
87            pattern: /^https?\:/,
88            optional: true
89          },
90          index: {
91            type: "integer",
92            minimum: 0,
93            optional: true
94          },
95          selected: {
96            type: "boolean",
97            optional: true
98          }
99        }
100      },
101      { type: "function", optional: true },
102      { type: "function", optional: true }
103    ]
104  };
105
106  var instance = [
107    {
108      id: 42,
109      url: "http://www.google.com/",
110      index: 2,
111      selected: true
112    },
113    function(){},
114    function(){}
115  ];
116
117  assertValid("", instance, schema);
118  instance.length = 2;
119  assertValid("", instance, schema);
120  instance.length = 1;
121  instance.push({});
122  assertNotValid("", instance, schema,
123                 [formatError("invalidType", ["function", "object"])]);
124  instance[1] = function(){};
125
126  instance[0].url = "foo";
127  assertNotValid("", instance, schema,
128                 [formatError("stringPattern",
129                              [schema.items[0].properties.url.pattern])]);
130  delete instance[0].url;
131  assertValid("", instance, schema);
132
133  instance[0].id = 0;
134  assertNotValid("", instance, schema,
135                 [formatError("numberMinValue",
136                              [schema.items[0].properties.id.minimum])]);
137}
138
139function testEnum() {
140  var schema = {
141    enum: ["foo", 42, false]
142  };
143
144  assertValid("", "foo", schema);
145  assertValid("", 42, schema);
146  assertValid("", false, schema);
147  assertNotValid("", "42", schema, [formatError("invalidEnum",
148                                                [schema.enum.join(", ")])]);
149  assertNotValid("", null, schema, [formatError("invalidEnum",
150                                                [schema.enum.join(", ")])]);
151}
152
153function testChoices() {
154  var schema = {
155    choices: [
156      { type: "null" },
157      { type: "undefined" },
158      { type: "integer", minimum:42, maximum:42 },
159      { type: "object", properties: { foo: { type: "string" } } }
160    ]
161  }
162    assertValid("", null, schema);
163    assertValid("", undefined, schema);
164    assertValid("", 42, schema);
165    assertValid("", {foo: "bar"}, schema);
166
167    assertNotValid("", "foo", schema, [formatError("invalidChoice", [])]);
168    assertNotValid("", [], schema, [formatError("invalidChoice", [])]);
169    assertNotValid("", {foo: 42}, schema, [formatError("invalidChoice", [])]);
170}
171
172function testExtends() {
173  var parent = {
174    type: "number"
175  }
176  var schema = {
177    extends: parent
178  };
179
180  assertValid("", 42, schema);
181  assertNotValid("", "42", schema,
182                 [formatError("invalidType", ["number", "string"])]);
183
184  // Make the derived schema more restrictive
185  parent.minimum = 43;
186  assertNotValid("", 42, schema, [formatError("numberMinValue", [43])]);
187  assertValid("", 43, schema);
188}
189
190function testObject() {
191  var schema = {
192    properties: {
193      "foo": {
194        type: "string"
195      },
196      "bar": {
197        type: "integer"
198      }
199    }
200  };
201
202  assertValid("Object", {foo:"foo", bar:42}, schema);
203  assertNotValid("Object", {foo:"foo", bar:42,"extra":true}, schema,
204                 [formatError("unexpectedProperty")]);
205  assertNotValid("Object", {foo:"foo"}, schema,
206                 [formatError("propertyRequired")]);
207  assertNotValid("Object", {foo:"foo", bar:"42"}, schema,
208                 [formatError("invalidType", ["integer", "string"])]);
209
210  schema.additionalProperties = { type: "any" };
211  assertValid("Object", {foo:"foo", bar:42, "extra":true}, schema);
212  assertValid("Object", {foo:"foo", bar:42, "extra":"foo"}, schema);
213
214  schema.additionalProperties = { type: "boolean" };
215  assertValid("Object", {foo:"foo", bar:42, "extra":true}, schema);
216  assertNotValid("Object", {foo:"foo", bar:42, "extra":"foo"}, schema,
217                 [formatError("invalidType", ["boolean", "string"])]);
218
219  schema.properties.bar.optional = true;
220  assertValid("Object", {foo:"foo", bar:42}, schema);
221  assertValid("Object", {foo:"foo"}, schema);
222  assertValid("Object", {foo:"foo", bar:null}, schema);
223  assertValid("Object", {foo:"foo", bar:undefined}, schema);
224  assertNotValid("Object", {foo:"foo", bar:"42"}, schema,
225                 [formatError("invalidType", ["integer", "string"])]);
226}
227
228function testTypeReference() {
229  var referencedTypes = [
230    {
231      id: "MinLengthString",
232      type: "string",
233      minLength: 2
234    },
235    {
236      id: "Max10Int",
237      type: "integer",
238      maximum: 10
239    }
240  ];
241
242  var schema = {
243    type: "object",
244    properties: {
245      "foo": {
246        type: "string"
247      },
248      "bar": {
249        $ref: "Max10Int"
250      },
251      "baz": {
252        $ref: "MinLengthString"
253      }
254    }
255  };
256
257  var schemaInlineReference = {
258    type: "object",
259    properties: {
260      "foo": {
261        type: "string"
262      },
263      "bar": {
264        id: "NegativeInt",
265        type: "integer",
266        maximum: 0
267      },
268      "baz": {
269        $ref: "NegativeInt"
270      }
271    }
272  }
273
274  // Valid type references externally added.
275  assertValid("", {foo:"foo",bar:4,baz:"ab"}, schema, referencedTypes);
276
277  // Valida type references internally defined.
278  assertValid("", {foo:"foo",bar:-4,baz:-2}, schemaInlineReference);
279
280  // Failures in validation, but succesful schema reference.
281  assertNotValid("", {foo:"foo",bar:4,baz:"a"}, schema,
282                [formatError("stringMinLength", [2])], referencedTypes);
283  assertNotValid("", {foo:"foo",bar:20,baz:"abc"}, schema,
284                [formatError("numberMaxValue", [10])], referencedTypes);
285
286  // Remove MinLengthString type.
287  referencedTypes.shift();
288  assertNotValid("", {foo:"foo",bar:4,baz:"ab"}, schema,
289                 [formatError("unknownSchemaReference", ["MinLengthString"])],
290                 referencedTypes);
291
292  // Remove internal type "NegativeInt"
293  delete schemaInlineReference.properties.bar;
294  assertNotValid("", {foo:"foo",baz:-2}, schemaInlineReference,
295                 [formatError("unknownSchemaReference", ["NegativeInt"])]);
296}
297
298function testArrayTuple() {
299  var schema = {
300    items: [
301      {
302        type: "string"
303      },
304      {
305        type: "integer"
306      }
307    ]
308  };
309
310  assertValid("Array", ["42", 42], schema);
311  assertNotValid("Array", ["42", 42, "anything"], schema,
312                 [formatError("arrayMaxItems", [schema.items.length])]);
313  assertNotValid("Array", ["42"], schema, [formatError("itemRequired")]);
314  assertNotValid("Array", [42, 42], schema,
315                 [formatError("invalidType", ["string", "integer"])]);
316
317  schema.additionalProperties = { type: "any" };
318  assertValid("Array", ["42", 42, "anything"], schema);
319  assertValid("Array", ["42", 42, []], schema);
320
321  schema.additionalProperties = { type: "boolean" };
322  assertNotValid("Array", ["42", 42, "anything"], schema,
323                 [formatError("invalidType", ["boolean", "string"])]);
324  assertValid("Array", ["42", 42, false], schema);
325
326  schema.items[0].optional = true;
327  assertValid("Array", ["42", 42], schema);
328  assertValid("Array", [, 42], schema);
329  assertValid("Array", [null, 42], schema);
330  assertValid("Array", [undefined, 42], schema);
331  assertNotValid("Array", [42, 42], schema,
332                 [formatError("invalidType", ["string", "integer"])]);
333}
334
335function testArrayNonTuple() {
336  var schema = {
337    items: {
338      type: "string"
339    },
340    minItems: 2,
341    maxItems: 3
342  };
343
344  assertValid("Array", ["x", "x"], schema);
345  assertValid("Array", ["x", "x", "x"], schema);
346
347  assertNotValid("Array", ["x"], schema,
348                 [formatError("arrayMinItems", [schema.minItems])]);
349  assertNotValid("Array", ["x", "x", "x", "x"], schema,
350                 [formatError("arrayMaxItems", [schema.maxItems])]);
351  assertNotValid("Array", [42], schema,
352                 [formatError("arrayMinItems", [schema.minItems]),
353                  formatError("invalidType", ["string", "integer"])]);
354}
355
356function testString() {
357  var schema = {
358    minLength: 1,
359    maxLength: 10,
360    pattern: /^x/
361  };
362
363  assertValid("String", "x", schema);
364  assertValid("String", "xxxxxxxxxx", schema);
365
366  assertNotValid("String", "y", schema,
367                 [formatError("stringPattern", [schema.pattern])]);
368  assertNotValid("String", "xxxxxxxxxxx", schema,
369                 [formatError("stringMaxLength", [schema.maxLength])]);
370  assertNotValid("String", "", schema,
371                 [formatError("stringMinLength", [schema.minLength]),
372                  formatError("stringPattern", [schema.pattern])]);
373}
374
375function testNumber() {
376  var schema = {
377    minimum: 1,
378    maximum: 100,
379    maxDecimal: 2
380  };
381
382  assertValid("Number", 1, schema);
383  assertValid("Number", 50, schema);
384  assertValid("Number", 100, schema);
385  assertValid("Number", 88.88, schema);
386
387  assertNotValid("Number", 0.5, schema,
388                 [formatError("numberMinValue", [schema.minimum])]);
389  assertNotValid("Number", 100.1, schema,
390                 [formatError("numberMaxValue", [schema.maximum])]);
391  assertNotValid("Number", 100.111, schema,
392                 [formatError("numberMaxValue", [schema.maximum]),
393                  formatError("numberMaxDecimal", [schema.maxDecimal])]);
394
395  var nan = 0/0;
396  AssertTrue(isNaN(nan));
397  assertNotValid("Number", nan, schema,
398                 [formatError("numberFiniteNotNan", ["NaN"])]);
399
400  assertNotValid("Number", Number.POSITIVE_INFINITY, schema,
401                 [formatError("numberFiniteNotNan", ["Infinity"]),
402                  formatError("numberMaxValue", [schema.maximum])
403                 ]);
404
405  assertNotValid("Number", Number.NEGATIVE_INFINITY, schema,
406                 [formatError("numberFiniteNotNan", ["-Infinity"]),
407                  formatError("numberMinValue", [schema.minimum])
408                 ]);
409}
410
411function testIntegerBounds() {
412  assertValid("Number",           0, {type:"integer"});
413  assertValid("Number",          -1, {type:"integer"});
414  assertValid("Number",  2147483647, {type:"integer"});
415  assertValid("Number", -2147483648, {type:"integer"});
416  assertNotValid("Number",           0.5, {type:"integer"},
417                 [formatError("numberIntValue", [])]);
418  assertNotValid("Number", 10000000000,   {type:"integer"},
419                 [formatError("numberIntValue", [])]);
420  assertNotValid("Number",  2147483647.5, {type:"integer"},
421                 [formatError("numberIntValue", [])]);
422  assertNotValid("Number",  2147483648,   {type:"integer"},
423                 [formatError("numberIntValue", [])]);
424  assertNotValid("Number",  2147483649,   {type:"integer"},
425                 [formatError("numberIntValue", [])]);
426  assertNotValid("Number", -2147483649,   {type:"integer"},
427                 [formatError("numberIntValue", [])]);
428}
429
430function testType() {
431  // valid
432  assertValid("Type", {}, {type:"object"});
433  assertValid("Type", [], {type:"array"});
434  assertValid("Type", function(){}, {type:"function"});
435  assertValid("Type", "foobar", {type:"string"});
436  assertValid("Type", "", {type:"string"});
437  assertValid("Type", 88.8, {type:"number"});
438  assertValid("Type", 42, {type:"number"});
439  assertValid("Type", 0, {type:"number"});
440  assertValid("Type", 42, {type:"integer"});
441  assertValid("Type", 0, {type:"integer"});
442  assertValid("Type", true, {type:"boolean"});
443  assertValid("Type", false, {type:"boolean"});
444  assertValid("Type", null, {type:"null"});
445  assertValid("Type", undefined, {type:"undefined"});
446
447  // not valid
448  assertNotValid("Type", [], {type: "object"},
449                 [formatError("invalidType", ["object", "array"])]);
450  assertNotValid("Type", null, {type: "object"},
451                 [formatError("invalidType", ["object", "null"])]);
452  assertNotValid("Type", function(){}, {type: "object"},
453                 [formatError("invalidType", ["object", "function"])]);
454  assertNotValid("Type", 42, {type: "array"},
455                 [formatError("invalidType", ["array", "integer"])]);
456  assertNotValid("Type", 42, {type: "string"},
457                 [formatError("invalidType", ["string", "integer"])]);
458  assertNotValid("Type", "42", {type: "number"},
459                 [formatError("invalidType", ["number", "string"])]);
460  assertNotValid("Type", 88.8, {type: "integer"},
461                 [formatError("invalidTypeIntegerNumber")]);
462  assertNotValid("Type", 1, {type: "boolean"},
463                 [formatError("invalidType", ["boolean", "integer"])]);
464  assertNotValid("Type", false, {type: "null"},
465                 [formatError("invalidType", ["null", "boolean"])]);
466  assertNotValid("Type", undefined, {type: "null"},
467                 [formatError("invalidType", ["null", "undefined"])]);
468  assertNotValid("Type", {}, {type: "function"},
469                 [formatError("invalidType", ["function", "object"])]);
470}
471
472function testGetAllTypesForSchema() {
473  var referencedTypes = [
474    {
475      id: "ChoicesRef",
476      choices: [
477        { type: "integer" },
478        { type: "string" }
479      ]
480    },
481    {
482      id: "ObjectRef",
483      type: "object",
484    }
485  ];
486
487  var arraySchema = {
488    type: "array"
489  };
490
491  var choicesSchema = {
492    choices: [
493      { type: "object" },
494      { type: "function" }
495    ]
496  };
497
498  var objectRefSchema = {
499    $ref: "ObjectRef"
500  };
501
502  var complexSchema = {
503    choices: [
504      { $ref: "ChoicesRef" },
505      { type: "function" },
506      { $ref: "ObjectRef" }
507    ]
508  };
509
510  var validator = new JSONSchemaValidator();
511  validator.addTypes(referencedTypes);
512
513  var arraySchemaTypes = validator.getAllTypesForSchema(arraySchema);
514  assertEqualSets(arraySchemaTypes, ["array"]);
515
516  var choicesSchemaTypes = validator.getAllTypesForSchema(choicesSchema);
517  assertEqualSets(choicesSchemaTypes, ["object", "function"]);
518
519  var objectRefSchemaTypes = validator.getAllTypesForSchema(objectRefSchema);
520  assertEqualSets(objectRefSchemaTypes, ["object"]);
521
522  var complexSchemaTypes = validator.getAllTypesForSchema(complexSchema);
523  assertEqualSets(complexSchemaTypes,
524      ["integer", "string", "function", "object"]);
525}
526
527function testIsValidSchemaType() {
528  var referencedTypes = [
529    {
530      id: "ChoicesRef",
531      choices: [
532        { type: "integer" },
533        { type: "string" }
534      ]
535    }
536  ];
537
538  var objectSchema = {
539    type: "object",
540    optional: true
541  };
542
543  var complexSchema = {
544    choices: [
545      { $ref: "ChoicesRef" },
546      { type: "function" },
547    ]
548  };
549
550  var validator = new JSONSchemaValidator();
551  validator.addTypes(referencedTypes);
552
553  AssertTrue(validator.isValidSchemaType("object", objectSchema));
554  AssertTrue(!validator.isValidSchemaType("integer", objectSchema));
555  AssertTrue(!validator.isValidSchemaType("array", objectSchema));
556  AssertTrue(validator.isValidSchemaType("null", objectSchema));
557  AssertTrue(validator.isValidSchemaType("undefined", objectSchema));
558
559  AssertTrue(validator.isValidSchemaType("integer", complexSchema));
560  AssertTrue(validator.isValidSchemaType("function", complexSchema));
561  AssertTrue(validator.isValidSchemaType("string", complexSchema));
562  AssertTrue(!validator.isValidSchemaType("object", complexSchema));
563  AssertTrue(!validator.isValidSchemaType("null", complexSchema));
564  AssertTrue(!validator.isValidSchemaType("undefined", complexSchema));
565}
566
567function testCheckSchemaOverlap() {
568  var referencedTypes = [
569    {
570      id: "ChoicesRef",
571      choices: [
572        { type: "integer" },
573        { type: "string" }
574      ]
575    },
576    {
577      id: "ObjectRef",
578      type: "object",
579    }
580  ];
581
582  var arraySchema = {
583    type: "array"
584  };
585
586  var choicesSchema = {
587    choices: [
588      { type: "object" },
589      { type: "function" }
590    ]
591  };
592
593  var objectRefSchema = {
594    $ref: "ObjectRef"
595  };
596
597  var complexSchema = {
598    choices: [
599      { $ref: "ChoicesRef" },
600      { type: "function" },
601      { $ref: "ObjectRef" }
602    ]
603  };
604
605  var validator = new JSONSchemaValidator();
606  validator.addTypes(referencedTypes);
607
608  AssertTrue(!validator.checkSchemaOverlap(arraySchema, choicesSchema));
609  AssertTrue(!validator.checkSchemaOverlap(arraySchema, objectRefSchema));
610  AssertTrue(!validator.checkSchemaOverlap(arraySchema, complexSchema));
611  AssertTrue(validator.checkSchemaOverlap(choicesSchema, objectRefSchema));
612  AssertTrue(validator.checkSchemaOverlap(choicesSchema, complexSchema));
613  AssertTrue(validator.checkSchemaOverlap(objectRefSchema, complexSchema));
614}
615
616function testInstanceOf() {
617  function Animal() {};
618  function Cat() {};
619  function Dog() {};
620  Cat.prototype = new Animal;
621  Cat.prototype.constructor = Cat;
622  Dog.prototype = new Animal;
623  Dog.prototype.constructor = Dog;
624  var cat = new Cat();
625  var dog = new Dog();
626  var num = new Number(1);
627
628  // instanceOf should check type by walking up prototype chain.
629  assertValid("", cat, {type:"object", isInstanceOf:"Cat"});
630  assertValid("", cat, {type:"object", isInstanceOf:"Animal"});
631  assertValid("", cat, {type:"object", isInstanceOf:"Object"});
632  assertValid("", dog, {type:"object", isInstanceOf:"Dog"});
633  assertValid("", dog, {type:"object", isInstanceOf:"Animal"});
634  assertValid("", dog, {type:"object", isInstanceOf:"Object"});
635  assertValid("", num, {type:"object", isInstanceOf:"Number"});
636  assertValid("", num, {type:"object", isInstanceOf:"Object"});
637
638  assertNotValid("", cat, {type:"object", isInstanceOf:"Dog"},
639                 [formatError("notInstance", ["Dog"])]);
640  assertNotValid("", dog, {type:"object", isInstanceOf:"Cat"},
641                 [formatError("notInstance", ["Cat"])]);
642  assertNotValid("", cat, {type:"object", isInstanceOf:"String"},
643                 [formatError("notInstance", ["String"])]);
644  assertNotValid("", dog, {type:"object", isInstanceOf:"String"},
645                 [formatError("notInstance", ["String"])]);
646  assertNotValid("", num, {type:"object", isInstanceOf:"Array"},
647                [formatError("notInstance", ["Array"])]);
648  assertNotValid("", num, {type:"object", isInstanceOf:"String"},
649                [formatError("notInstance", ["String"])]);
650}
651
652// Tests exposed to schema_unittest.cc.
653exports.testFormatError = testFormatError;
654exports.testComplex = testComplex;
655exports.testEnum = testEnum;
656exports.testExtends = testExtends;
657exports.testObject = testObject;
658exports.testArrayTuple = testArrayTuple;
659exports.testArrayNonTuple = testArrayNonTuple;
660exports.testString = testString;
661exports.testNumber = testNumber;
662exports.testIntegerBounds = testIntegerBounds;
663exports.testType = testType;
664exports.testTypeReference = testTypeReference;
665exports.testGetAllTypesForSchema = testGetAllTypesForSchema;
666exports.testIsValidSchemaType = testIsValidSchemaType;
667exports.testCheckSchemaOverlap = testCheckSchemaOverlap;
668exports.testInstanceOf = testInstanceOf;
669
Full Screen

reporter.spec.js

Source: reporter.spec.js Github

copy
1'use strict'
2
3const EventEmitter = require('events').EventEmitter
4const loadFile = require('mocks').loadFile
5const path = require('path')
6const _ = require('lodash')
7const sinon = require('sinon')
8
9const File = require('../../lib/file')
10
11describe('reporter', () => {
12  let m
13
14  beforeEach(() => {
15    m = loadFile(path.join(__dirname, '/../../lib/reporter.js'))
16  })
17
18  describe('formatError', () => {
19    let emitter
20    let formatError = emitter = null
21    let sandbox
22
23    beforeEach(() => {
24      emitter = new EventEmitter()
25      formatError = m.createErrorFormatter({ basePath: '', hostname: 'localhost', port: 8080 }, emitter)
26      sandbox = sinon.createSandbox()
27    })
28
29    it('should call config.formatError if defined', () => {
30      const spy = sandbox.spy()
31      formatError = m.createErrorFormatter({ basePath: '', formatError: spy }, emitter)
32      formatError()
33
34      expect(spy).to.have.been.calledOnce
35    })
36
37    it('should not call config.formatError if not defined', () => {
38      const spy = sandbox.spy()
39      formatError()
40
41      expect(spy).not.to.have.been.calledOnce
42    })
43
44    it('should pass the error message as the first config.formatError argument', () => {
45      const ERROR = 'foo bar'
46      const spy = sandbox.spy()
47      formatError = m.createErrorFormatter({ basePath: '', formatError: spy }, emitter)
48      formatError(ERROR)
49
50      expect(spy.firstCall.args[0]).to.equal(ERROR)
51    })
52
53    it('should display the exact error returned by config.formatError', () => {
54      const formattedError = 'A new error'
55      formatError = m.createErrorFormatter({ basePath: '', formatError: () => formattedError }, emitter)
56
57      expect(formatError('Something', '\t')).to.equal(formattedError)
58    })
59
60    it('should indent', () => {
61      expect(formatError('Something', '\t')).to.equal('\tSomething\n')
62    })
63
64    it('should handle empty message', () => {
65      expect(formatError(null)).to.equal('\n')
66    })
67
68    it('should handle arbitrary error objects', () => {
69      expect(
70        formatError({ hello: 'world' })
71      ).to.equal(
72        JSON.stringify({ hello: 'world' }) + '\n'
73      )
74    })
75
76    it('should handle error objects', () => {
77      expect(
78        formatError(new Error('fail'))
79      ).to.equal(
80        'fail\n'
81      )
82    })
83
84    it('should remove specified hostname from files', () => {
85      expect(formatError('file http://localhost:8080/base/usr/a.js and http://127.0.0.1:8080/absolute/home/b.js')).to.be.equal('file usr/a.js and http://127.0.0.1:8080/home/b.js\n')
86    })
87
88    it('should remove shas', () => {
89      const ERROR = 'file http://localhost:8080/base/usr/file.js?6e31cb249ee5b32d91f37ea516ca0f84bddc5aa9 and http://127.0.0.1:8080/absolute/home/file.js?6e31cb249ee5b32d91f37ea516ca0f84bddc5aa9'
90      expect(formatError(ERROR)).to.be.equal('file usr/file.js and http://127.0.0.1:8080/home/file.js\n')
91    })
92
93    it('should indent all lines', () => {
94      expect(formatError('first\nsecond\nthird', '\t')).to.equal('\tfirst\n\tsecond\n\tthird\n')
95    })
96
97    it('should restore base paths', () => {
98      formatError = m.createErrorFormatter({ basePath: '/some/base', hostname: 'localhost', port: 123 }, emitter)
99      expect(formatError('at http://localhost:123/base/a.js?123')).to.equal('at a.js\n')
100    })
101
102    it('should restore urlRoot paths', () => {
103      formatError = m.createErrorFormatter({ urlRoot: '/__karma__', basePath: '/some/base', hostname: 'localhost', port: 123 }, emitter)
104      expect(formatError('at http://localhost:123/__karma__/base/sub/a.js?123')).to.equal('at sub/a.js\n')
105    })
106
107    it('should restore absolute paths', () => {
108      const ERROR = 'at http://localhost:8080/absolute/usr/path.js?6e31cb249ee5b32d91f37ea516ca0f84bddc5aa9'
109      expect(formatError(ERROR)).to.equal('at /usr/path.js\n')
110    })
111
112    it('should preserve line numbers', () => {
113      const ERROR = 'at http://localhost:8080/absolute/usr/path.js?6e31cb249ee5b32d91f37ea516ca0f84bddc5aa9:2'
114      expect(formatError(ERROR)).to.equal('at /usr/path.js:2\n')
115    })
116
117    it('should preserve absolute word', () => {
118      const ERROR = 'contains absolute'
119      expect(formatError(ERROR)).to.equal('contains absolute\n')
120    })
121
122    it('should preserve base word', () => {
123      const ERROR = 'contains base'
124      expect(formatError(ERROR)).to.equal('contains base\n')
125    })
126
127    describe('source maps', () => {
128      let originalPositionForCallCount = 0
129      let sourceMappingPath = null
130
131      class MockSourceMapConsumer {
132        constructor (sourceMap) {
133          this.source = sourceMap.content.replace('SOURCE MAP ', sourceMappingPath)
134        }
135
136        originalPositionFor (position) {
137          originalPositionForCallCount++
138          if (position.line === 0) {
139            throw new TypeError('Line must be greater than or equal to 1, got 0')
140          }
141
142          return {
143            source: this.source,
144            line: position.line + 2,
145            column: position.column + 2
146          }
147        }
148      }
149
150      beforeEach(() => {
151        originalPositionForCallCount = 0
152        sourceMappingPath = '/original/'
153      })
154
155      MockSourceMapConsumer.GREATEST_LOWER_BOUND = 1
156      MockSourceMapConsumer.LEAST_UPPER_BOUND = 2
157
158      it('should rewrite stack traces', (done) => {
159        formatError = m.createErrorFormatter({ basePath: '/some/base', hostname: 'localhost', port: 123 }, emitter, MockSourceMapConsumer)
160        const servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
161        servedFiles[0].sourceMap = { content: 'SOURCE MAP a.js' }
162        servedFiles[1].sourceMap = { content: 'SOURCE MAP b.js' }
163
164        emitter.emit('file_list_modified', { served: servedFiles })
165
166        _.defer(() => {
167          const ERROR = 'at http://localhost:123/base/b.js:2:6'
168          expect(formatError(ERROR)).to.equal('at /original/b.js:4:8 <- b.js:2:6\n')
169          done()
170        })
171      })
172
173      it('should rewrite stack traces (when basePath is empty)', (done) => {
174        formatError = m.createErrorFormatter({ basePath: '', hostname: 'localhost', port: 123 }, emitter, MockSourceMapConsumer)
175        const servedFiles = [new File('/a.js'), new File('/b.js')]
176        servedFiles[0].sourceMap = { content: 'SOURCE MAP a.js' }
177        servedFiles[1].sourceMap = { content: 'SOURCE MAP b.js' }
178
179        emitter.emit('file_list_modified', { served: servedFiles })
180
181        _.defer(() => {
182          const ERROR = 'at http://localhost:123/base/b.js:2:6'
183          expect(formatError(ERROR)).to.equal('at /original/b.js:4:8 <- b.js:2:6\n')
184          done()
185        })
186      })
187
188      it('should rewrite stack traces to the first column when no column is given', (done) => {
189        formatError = m.createErrorFormatter({ basePath: '/some/base', hostname: 'localhost', port: 123 }, emitter, MockSourceMapConsumer)
190        const servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
191        servedFiles[0].sourceMap = { content: 'SOURCE MAP a.js' }
192        servedFiles[1].sourceMap = { content: 'SOURCE MAP b.js' }
193
194        emitter.emit('file_list_modified', { served: servedFiles })
195
196        _.defer(() => {
197          const ERROR = 'at http://localhost:123/base/b.js:2'
198          expect(formatError(ERROR)).to.equal('at /original/b.js:4:3 <- b.js:2\n')
199          done()
200        })
201      })
202
203      it('should rewrite relative url stack traces', (done) => {
204        formatError = m.createErrorFormatter({ basePath: '/some/base', hostname: 'localhost', port: 123 }, emitter, MockSourceMapConsumer)
205        const servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
206        servedFiles[0].sourceMap = { content: 'SOURCE MAP a.js' }
207        servedFiles[1].sourceMap = { content: 'SOURCE MAP b.js' }
208
209        emitter.emit('file_list_modified', { served: servedFiles })
210
211        _.defer(() => {
212          const ERROR = 'at /base/b.js:2:6'
213          expect(formatError(ERROR)).to.equal('at /original/b.js:4:8 <- b.js:2:6\n')
214          done()
215        })
216      })
217
218      it('should resolve relative urls from source maps', (done) => {
219        sourceMappingPath = 'original/' // Note: relative path.
220        formatError = m.createErrorFormatter({ basePath: '/some/base' }, emitter, MockSourceMapConsumer)
221        const servedFiles = [new File('/some/base/path/a.js')]
222        servedFiles[0].sourceMap = { content: 'SOURCE MAP a.fancyjs' }
223
224        emitter.emit('file_list_modified', { served: servedFiles })
225
226        _.defer(() => {
227          const ERROR = 'at /base/path/a.js:2:6'
228          expect(formatError(ERROR)).to.equal('at path/original/a.fancyjs:4:8 <- path/a.js:2:6\n')
229          done()
230        })
231      })
232
233      it('should fall back to non-source-map format if originalPositionFor throws', (done) => {
234        formatError = m.createErrorFormatter({ basePath: '/some/base', hostname: 'localhost', port: 123 }, emitter, MockSourceMapConsumer)
235        const servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
236        servedFiles[0].sourceMap = { content: 'SOURCE MAP a.js' }
237        servedFiles[1].sourceMap = { content: 'SOURCE MAP b.js' }
238
239        emitter.emit('file_list_modified', { served: servedFiles })
240
241        _.defer(() => {
242          const ERROR = 'at http://localhost:123/base/b.js:0:0'
243          expect(formatError(ERROR)).to.equal('at b.js\n')
244          done()
245        })
246      })
247
248      it('should not try to use source maps when no line is given', (done) => {
249        formatError = m.createErrorFormatter({ basePath: '/some/base', hostname: 'localhost', port: 123 }, emitter, MockSourceMapConsumer)
250        const servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
251        servedFiles[0].sourceMap = { content: 'SOURCE MAP a.js' }
252        servedFiles[1].sourceMap = { content: 'SOURCE MAP b.js' }
253
254        emitter.emit('file_list_modified', { served: servedFiles })
255
256        _.defer(() => {
257          const ERROR = 'at http://localhost:123/base/b.js'
258          expect(formatError(ERROR)).to.equal('at b.js\n')
259          expect(originalPositionForCallCount).to.equal(0)
260          done()
261        })
262      })
263
264      it('should not try to match domains with spaces', (done) => {
265        formatError = m.createErrorFormatter({ basePath: '/some/base', hostname: 'localhost', port: 9876 }, emitter, MockSourceMapConsumer)
266        const servedFiles = [new File('/some/base/a.js'), new File('/some/base/b.js')]
267        servedFiles[0].sourceMap = { content: 'SOURCE MAP a.js' }
268        servedFiles[1].sourceMap = { content: 'SOURCE MAP b.js' }
269
270        emitter.emit('file_list_modified', { served: servedFiles })
271
272        _.defer(() => {
273          const ERROR = '"http://localhost:9876"\n at /base/b.js:2:6'
274          expect(formatError(ERROR)).to.equal('"http://localhost:9876"\n at /original/b.js:4:8 <- b.js:2:6\n')
275          done()
276        })
277      })
278
279      describe('Windows', () => {
280        formatError = null
281        let servedFiles = null
282
283        beforeEach(() => {
284          formatError = m.createErrorFormatter({ basePath: '/some/base', hostname: 'localhost', port: 123 }, emitter, MockSourceMapConsumer)
285          servedFiles = [new File('C:/a/b/c.js')]
286          servedFiles[0].sourceMap = { content: 'SOURCE MAP b.js' }
287        })
288
289        it('should correct rewrite stack traces without sha', (done) => {
290          emitter.emit('file_list_modified', { served: servedFiles })
291
292          _.defer(() => {
293            const ERROR = 'at http://localhost:123/absoluteC:/a/b/c.js:2:6'
294            expect(formatError(ERROR)).to.equal('at c:/original/b.js:4:8 <- C:/a/b/c.js:2:6\n')
295            done()
296          })
297        })
298
299        it('should correct rewrite stack traces with sha', (done) => {
300          emitter.emit('file_list_modified', { served: servedFiles })
301
302          _.defer(() => {
303            const ERROR = 'at http://localhost:123/absoluteC:/a/b/c.js?da39a3ee5e6:2:6'
304            expect(formatError(ERROR)).to.equal('at c:/original/b.js:4:8 <- C:/a/b/c.js:2:6\n')
305            done()
306          })
307        })
308      })
309    })
310  })
311})
312
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)